Created
November 29, 2012 02:47
-
-
Save need4spd/4166456 to your computer and use it in GitHub Desktop.
[Java] Class.forName(String className) 그리고 Service Provider Interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class.forName("oracle.jdbc.driver.OracleDriver"); | |
Connection conn = null; | |
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORA92", "scott", "tiger"); | |
Statement.. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package serviceprovider; | |
public interface Connection { | |
} | |
// | |
package serviceprovider; | |
public interface Driver { | |
Connection getConnection(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package serviceprovider; | |
import java.util.Map; | |
import java.util.concurrent.ConcurrentHashMap; | |
public class DriverManager { | |
private DriverManager() {} | |
private static final Map<String,Driver> drivers = new ConcurrentHashMap<String,Driver>(); | |
public static final String DEFAULT_DRIVER_NAME = "default"; | |
public static void registerDefaultPrivider(Driver d) { | |
System.out.println("Driver 등록"); | |
registerDriver(DEFAULT_DRIVER_NAME, d); | |
} | |
public static void registerDriver(String name, Driver d) { | |
drivers.put(name,d); | |
} | |
public static Connection getConnection() { | |
return getConnection(DEFAULT_DRIVER_NAME); | |
} | |
public static Connection getConnection(String name) { | |
Driver d = drivers.get(name); | |
if(d==null) | |
throw new IllegalArgumentException(); | |
return d.getConnection(); | |
} | |
} | |
//Effective Java SE P11 참고 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static | |
{ | |
defaultDriver = null; | |
Timestamp timestamp = Timestamp.valueOf("2000-01-01 00:00:00.0"); | |
try | |
{ | |
if(defaultDriver == null) | |
{ | |
defaultDriver = new OracleDriver(); | |
DriverManager.registerDriver(defaultDriver); | |
} | |
} | |
catch(RuntimeException runtimeexception) { } | |
catch(SQLException sqlexception) { } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package serviceprovider; | |
public class MyDriver implements Driver { | |
private Log logger = LogFactory.getLog(MyDriver.class); | |
private static Driver defaultDriver; | |
static { | |
defaultDriver = new MyDriver(); | |
serviceprovider.DriverManager.registerDefaultPrivider(defaultDriver); | |
} | |
@Override | |
public Connection getConnection() { | |
System.out.println("MyDriver's Connection return"); | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package serviceprovider; | |
public class Test { | |
private Log logger = LogFactory.getLog(Test.class); | |
public static void main(String[] args) throws ClassNotFoundException { | |
Class.forName("MyDriver"); | |
Connection connection = DriverManager.getConnection(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment