JDBC Tutorial - JDBC Connections








Register JDBC Driver

In order to make a connection to the database system we need to register the driver for the database system in your program.

The driver name is database system dependent.

Example

The most common approach to register a driver is to use Java's Class.forName() method to load the driver's class file into memory, which automatically registers it.

try {
   Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException ex) {
   System.out.println("Error: unable to load driver class!");
   System.exit(1);
}




Database URL

After loading the driver, we can open a connection using the DriverManager.getConnection() method.

There are three overloaded DriverManager.getConnection() methods:

  • getConnection(String url)

  • getConnection(String url, Properties prop)

  • getConnection(String url, String user, String password)

A database URL is an address that points to the database.

Following table lists some popular JDBC driver names and database URL.

RDBMS JDBC driver name URL format
MySQL com.mysql.jdbc.Driver jdbc:mysql://hostname/databaseName
ORACLE oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@hostname:port Number:databaseName
DB2 COM.ibm.db2.jdbc.net.DB2Driver jdbc:db2:hostname:port Number/databaseName
Sybase com.sybase.jdbc.SybDriver jdbc:sybase:Tds:hostname: port Number/databaseName




Example 2

The following code shows how to use Oracle's thin driver and its corresponding URL.

The code assumes that there is Oracle database running on 192.0.0.1 IP address whose port is 1521.

The database name is EMP.

jdbc:oracle:thin:@192.0.0.1:1521:EMP

The following code shows how to pass in URL value and user name and password to getConnection() method to get a Connection object.

String URL = "jdbc:oracle:thin:@192.0.0.1:1521:EMP";
String USER = "username";
String PASS = "password"
Connection conn = DriverManager.getConnection(URL, USER, PASS);

Embed user name and password in a database URL

The second form of the DriverManager.getConnection( ) method requires only a database URL:

DriverManager.getConnection(String url);

To use the method above we have to embed the username and password to the database URL. Here is a general form:

jdbc:oracle:driver:username/password@database

We can rewrite the example as follows:

String URL = "jdbc:oracle:thin:username/password@192.0.0.1:1521:EMP";
Connection conn = DriverManager.getConnection(URL);

Using a database URL and a Properties object

The third form of the DriverManager.getConnection() method requires a database URL and a Properties object:

DriverManager.getConnection(String url, Properties info);

A Properties object holds a set of keyword-value pairs.

The following code shows how to use URL and Properties object to make the same connection to Oracle database.


String URL = "jdbc:oracle:thin:@192.0.0.1:1521:EMP";
Properties info = new Properties( );
info.put( "user", "username" );
info.put( "password", "password" );

Connection conn = DriverManager.getConnection(URL, info);

Closing JDBC connections

Before exiting JDBC application, we are required to explicitly close all the connections to the database to end each database session.

To ensure that a connection is closed, put the close() method in a finally block since a finally block always executes regardless if an exception occurs or not.

To close above opened connection, call close() method from Connection object as follows:

} finally {
  // finally block used to close resources
  try {
    if (stmt != null)
      stmt.close();
  } catch (SQLException se2) {
  }
  try {
    if (conn != null)
      conn.close();
  } catch (SQLException se) {
    se.printStackTrace();
  }
}