Changing the JDBC Driver

 

 

 

If you want to change or set the JDBC driver, your first need to collect some information from the documentation from the database manufacturer and from your DBA. The first is the Java class name of the JDBC driver. The second is the format of the JDBC URL. These can be obtained from your database's documentation. Next you will most likely need the hostname, database id, etc. from your DBA. These values you will insert in the specified places in the JDBC URL.

JDBC Driver Name. The JDBC driver name is the name of the Java class that implements the driver. Some examples are:

  • Oracle thin driver - oracle.jdbc.driver.OracleDriver
  • IBM DB2 driver - COM.ibm.db2.jdbc.app.DB2Driver
  • JDBC-ODBC bridge driver - sun.jdbc.odbc.JdbcOdbcDriver

JDBC URL. The URL specifies information such as the protocol to be used, database id, host name where the database server is running, etc. For example, the URL for the JDBC-ODBC driver supplied with Java is jdbc:odbc:dsn where dsn is the data source name specified in Windows ODBC Data Source Administrator (double-click on ODBC in the Control Panel) for the ODBC data source you are using.

If your documentation says something like "open a connection to the database named, mydatabase, with the JDBC getConnection method":

Connection conn = DriverManager.getConnection ("jdbc:oracle:oci7:@mydatabase", "scott", "tiger");

Take everything inside the first quoted string, jdbc:oracle:oci7:@mydatabase and use this as the JDBC URL.

Another Oracle example using their thin driver which is a Type IV driver that doesn't require Oracle Client to be installed on each client. The Oracle Thin driver will download all of the classes it needs and allow the Java client to talk straight to the database server located on the web server:

Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@myhost:1521:orcl", "scott", "tiger");

Take everything inside the first quoted string, jdbc:oracle:thin:@myhost:1521:orcl and use this as the JDBC URL. Note: the syntax after the ":@" is host:port:sid.

Changing the generated Java code. There are few different API calls that are used in the generated Java code to setup the JDBC driver name and URL. The following is a common one:

con = new Connection();
AdoUtil.registerDriver("sun.jdbc.odbc.JdbcOdbcDriver");
con.Open("jdbc:odbc:dmv_www", null, null);

After the Java code is generated, you can change these lines to reflect the values you want to use. Change the first line above with the JDBC driver name. Change the second line with the JDBC URL as the first argument and the user id and password as the remaining arguments. For example if you moved your data to Oracle and serv12 is the host name and dmvdb is the database id then the lines would be:

con = new Connection();
AdoUtil.registerDriver("oracle.jdbc.driver.OracleDriver");
con.Open("jdbc:oracle:thin:@serv12:1521:dmvdb", null, null);

Note: another common API you might find in the generated Java code is:

con = new Connection();
con.setConnectionString("jdbc:odbc:dmv_www", null, null);
AdoUtil.registerDriver("sun.jdbc.odbc.JdbcOdbcDriver");

The setConnectionString call has the same arguments as the Connection.Open function call decribed above.

A less commonly used API is:

Connection.OpenConnection( "jdbc:odbc:dmv_www", null, null, "sun.jdbc.odbc.JdbcOdbcDriver" );

The first three arguments are the same as the arguments as the Connection.Open function call decribed above. The fourth argument is the JDBC driver name.