Home >>Java JDBC Tutorial >JDBC Database Connections

JDBC Database Connections

JDBC Database Connections

The JDBC database connections are established after the user has installed the appropriate driver in the system. The programming that takes place on order to get the JDBC connection established basically comprises of the four steps that are depicted below:

  • Import JDBC Packages : In this step- the user has to add import statements to their Java program in order to import the required classes in the user’s Java code.
  • Register JDBC Driver : In this step, the driver is registered and it causes the JVM to load the favorable driver implementation into the memory to make it able to fulfill the user’s JDBC requests.
  • Database URL Formulation : In this step, in order to create a properly formatted address that basically points to the database to which the user has the intentions to connect.
  • Create Connection Object : This is the last step that basically involves the code that is a call to the DriverManager object's getConnection( ) method in order to establish the actual database connection.

Let's get to know about these steps in detail

1. Import JDBC Packages

The Import statements are basically known to tell the Java compiler that where the classes that have been referenced in the code by the user and then these are placed at the very beginning of the user's source code. In order to use the standard JDBC package that basically permits the user to select, insert, update, and delete data in the SQL tables, the user have to add the following depicted imports to their source code:

import java.sql.* ;  // for standard JDBC programs
import java.math.* ; // for BigDecimal and BigInteger support

2. Register JDBC Driver

In order to use the driver the user must have to register the driver in the user's program before they use it. Registering the driver is basically known as the process by which the Oracle driver's class file gets loaded into the memory; hence, it can be utilized as a form of an implementation of the JDBC interfaces.

The need of registration is only of one time in the program and there are two ways to do it, it depends on the user which one to choose.

a. Class.forName()

This method is known as the most common approach in order to register a driver and that is to use the Java's Class.forName() method. This is used to dynamically load the driver's class file into the memory that basically automatically registers it. This method is generally known as the most preferable one as of the fact that it permits the user to make the driver registration configurable and portable.

Here is an example

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

The user can also use getInstance() method in order to work around the noncompliant JVMs, and then the user will have to code for two extra Exceptions.

b. DriverManager.registerDriver()

The second method or we can say the approach the users can use to register a driver is basically to use the static DriverManager.registerDriver() method in the Java JDBC. The user should be using the registerDriver() method in case the user is using a non-JDK compliant JVM, like the one that has been provided by the Microsoft.

Here is an example

try {
   Driver myDriver = new oracle.jdbc.driver.OracleDriver();
   DriverManager.registerDriver( myDriver );
}
catch(ClassNotFoundException ex) {
   System.out.println("Error: unable to load driver class!");
   System.exit(1);
}

3. Database URL Formulation

This step can only be performed when the user have performed the above mentioned step. Moving ahead in the process of establishing the connections the user can establish a connection just by the help of the DriverManager.getConnection() method. For the ease of the users here are the list of the three overloaded DriverManager.getConnection() methods that are depicted below:

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

Please note that the each form that has been mentioned above requires a database URL. Now,a database URL is basically an address that is used to points to the user’s database. Formulating a database URL is the place or step where the most of the errors that are known generally occurs while trying to establish a connection.

Here is the list that is depicted below of the most widely used 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

Note : that all the highlighted part in the above mentioned table’s URL format is static and the user have to change only the remaining part as per their database setup.

4. Create Connection Object

There are generally three ways for creating a connection object by the help of the DriverManager.getConnection() method that has been depicted below:

a. Using a Database URL with a username and password

The most widely used form of getConnection() generally needs the user to pass a database URL, a username, and a password. Let’s suppose that the user is using Oracle’s thin driver then the user have to specify a host:port:databaseName value for the database portion of the URL.

In case the user have a host at TCP/IP address 192.0.0.1 along with a host name of aam, and the user’s Oracle listener is basically configured to listen on port 1521, and the user’s database name is JMP then complete database URL would look like the address that is depicted below:

jdbc:oracle:thin:@aam:1521:JMP

After this the user have to call getConnection() method that too with appropriate username and password in order to get a Connection object that will look like something that is depicted below:

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

b. Using Only a Database URL

This is the second form of the DriverManager.getConnection( ) method that generally requires only a database URL that looks something like the URL that is mentioned below:

DriverManager.getConnection(String url);

But, in this case, the database URL will also include the username and the password and it's general form will look something like the syntax depicted below:

jdbc:oracle:driver:username/[email protected]

Now, the above connection can be created as mentioned below:

String URL = "jdbc:oracle:thin:username/[email protected] aam:1521:JMP ";
Connection conn = DriverManager.getConnection(URL);

c. Using a Database URL and a Properties Object

This is the third method that can be used to creating a connection and it involves the DriverManager.getConnection( ) method that generally requires a database URL followed by a properties object and the code of this method looks something like the code that has been depicted below:

DriverManager.getConnection(String url, Properties info);

A Properties object is basically known as the object that generally holds a set of keyword-value pairs. This is generally used in order to pass the driver properties to the driver while a call to the getConnection() method.

Here is an example

import java.util.*;
String URL = " jdbc:oracle:thin:@aam:1521:JMP";
Properties info = new Properties( );
info.put( "user", "username" );
info.put( "password", "password" );
Connection conn = DriverManager.getConnection(URL, info);

Close the connection object

Closing the connection is very important as by closing the connection the object statement and the ResultSet will be automatically closed. The close () method of the connection interface is generally used in order to close the connection.

Syntax

public void close()throws SQLException  

Here is an example

conn.close();