|
Loading a Driver
|
|
The first step to develop a JDBC application is to load and register the required driver using the driver manager. |
|
We can load and register a driver: |
|
1. Using the forName() method |
|
2. Using the registerDriver() method |
|
3. By setting system property |
|
Using the forName ( ) Method |
|
The forName ( ) method is available in the java.lang.Class class. The forName ( ) method loads the JDBC driver and registers the driver with the driver manager. |
|
The syntax to load a JDBC driver to ACCESS a database is: |
|
Class.forName(“”); |
|
Ex: Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); |
|
Using the registerDriver ( ) Method |
|
Here we can create an instance of the Driver class to load a JDBC driver. |
|
The syntax to declare an instance of the Driver class is: |
|
Driver d =new ; |
|
Ex: Driver d = new sun.jdbc.odbc.JdbcOdbcDriver ( ); |
|
Once we have created the Driver object, call the registerDriver ( ) method to register it with the DriverManager. We can register the JDBC-ODBC Bridge driver using the following method call to registerDriver ( ) method: |
|
DriverManager.registerDriver(d); |
|
Setting System Property |
|
Driver can also be loaded by setting System property for JDBC drivers. We add the driver name to the jdbc.driver System property to load a JDBC driver. We use the –D command line option to set the system property on the command line. The command to set the system property is: |
|
Java -Djdbc.drivers=sun.jdbc.odbc.JdbcOdbcDriver SampleApplication |
|
In the preceding command, jdbc.driver is the property name and sun.jdbc.odbc.JdbcOdbcDriver is the value that we need to set for the property. |
|
After we load the driver, we need to establish the connection with the database |