
Automatic SQL Driver Loading
JDBC 4.0 has facilitated the loading of a JDBC driver. In JDBC 3.0, a JDBC driver is loaded with the Class.forName(String)
method. The Oracle JDBC driver is loaded in the following manner:
Class.forName("oracle.jdbc.OracleDriver");
In JDBC 4.0, a JDBC driver is loaded automatically with the Java Standard Edition Service Provider mechanism. The JDBC driver is loaded when the java.sql.DriverManager.getConnection()
method is invoked. To load a JDBC driver with the Service Provider mechanism, JDBC 4.0 drivers should include the META-INF/services/java.sql.Driver
file. In the java.sql.Driver
file, specify the JDBC driver class to load. If the oracle.jdbc.OracleDriver
is to be loaded then specify the following line in the java.sql.Driver
file:
oracle.jdbc.OracleDriver
Multiple driver classes can be specified in a java.sql.Driver
file, each on a separate line. A list of JDBC drivers available to a DriverManager
can be obtained with the getDrivers()
method:
Enumeration<Driver> drivers=DriverManager.getDrivers();
A JDBC connection can be obtained using the getConnection()
method of the DriverManager
class:
String url="jdbc:oracle:thin:@localhost:1521:ORCL"; Connection connection = DriverManager.getConnection(url,"oe", "pw");