Tuesday, 29 March 2016

How can you load the JDBC driver ?


Loading the driver or drivers

Example Load JDBC-ODBC Bridge driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Vendors of the driver will give the class name to use with the documentation.
If the class name is jdbc.DriverXYZ, load the driver using :
Class.forName("jdbc.DriverXYZ");

What will Class.forName do while loading drivers ?
It is used to create an instance of a driver and register it with the DriverManager.
When you have loaded a driver, it is available for making a connection with a DBMS.


Get Connection of different databases
MySQL
Class.forName("com.mysql.jdbc.Driver");
Connection connection = connection = DriverManager.getConnection("jdbc:mysql /hostname ort/dbname", 
                            "username",  "password");
//..........
connection.close();

Oracle
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@hostname:port:SID", 
                            "username", "password");
//..........
connection.close();

PostgreSQL
Class.forName("org.postgresql.Driver");
Connection connection = DriverManager.getConnection("jdbc:postgresql://hostname:port/dbname",
                            "username",  "password");
//..........
connection.close();

No comments:

Post a Comment

Note: only a member of this blog may post a comment.