How Does JDBC Connection Actually Work?

Filed Under: Java

The JDBC Connection interface is a part of java.sql package. The java.sql.Connection interface defines the contract for relational database vendors. The vendor connection classes must implement these methods. An instance of Connection is used to communicate with the Database.

The implementation of the Connection interface depends on the database vendor. So we don’t need to worry about implementation details as long as the vendor follows the contract.

How to Establish JDBC Connection

Let’s have a look at the below steps to establish JDBC Connection.

1. Register JDBC Driver

JDBC driver is registered using below method.

Class.forName(String className): This method is used to load the JDBC driver class into JVM memory. The string argument is the fully classified name of the Connection implementation class.


// for MySQL
Class.forName("com.mysql.jdbc.Driver");

// for PostgreSQL
Class.forName("org.postgresql.Driver");

Let’s understand what is happening behind the scene. How JDBC Driver gets registered by using this syntax.

First of all, the JDBC driver’s jar file is included in the classpath of the java application. The DriverManager class uses service provider mechanism to search for jar files having a file named java.sql.Driver. This file must be present in the META-INF/services folder of the driver’s jar.

This is a simple text file that contains the full name of the class that implements the java.sql.Driver interface. For example, full name for MySQL Driver is com.mysql.jdbc.Driver. For PostgreSQL Driver, it is org.postgresql.Driver.

Jdbc Connection Implementation Jar

Jdbc Connection Implementation Jar

When the Driver class is loaded into memory, its static initialization block is executed. The static method of the DriverManager class is called to register the driver.


public class VendorDriver implements java.sql.Driver
{ 
static {
	VendorDriver driver = new VendorDriver();
	DriverManager.registerDriver(driver);
          }
}
Jdbc Connection Register Driver

Jdbc Connection Register Driver

This will register an instance of Driver class into the DriverManager.

2. Create JDBC Connection

We can get the JDBC connection using DriverManager.getConnection() method.


Connection conn = DriverManager.getConnection(url, user, password);

Let’s understand the parameters that we have passed to get the JDBC connection.

  • url: The JDBC URL which is used to determine which driver implementation to use for a given connection.
    
    jdbc:postgresql://hostname:port/databasename
    
    jdbc:postgresql://127.0.0.1:5432/javadb
    

    The first part indicates that this is the JDBC URL.

    The second part indicates the driver vendor which is PostgreSQL in our case.

    The third part indicates the hostname or IP address with the port of the database.

    The last part indicates the name of the database.

  • user: user of the database for which we get the connection.
  • password: password to access the given database.
Creating Jdbc Connection

Creating Jdbc Connection

JDBC Connection Example

Let’s have a look at the below example program. In below program, I have used PostgreSQL database so I have used the respective jar for PostgreSQL.


package com.journaldev.examples;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * JDBC Connection Example
 * 
 * @author pankaj
 *
 */
public class JDBCConnection {

	public static void main(String[] args) {
		Connection connection = null;
		try {
			System.out.println("------JDBC Connection Example--------");
			Class.forName("org.postgresql.Driver");
			connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/journaldev", "postgres", "admin");
			System.out.println("----JDBC Connection Established------");
			System.out.println("Catalog: "+connection.getCatalog());
			System.out.println("Schema: "+connection.getSchema());
			System.out.println("Info: "+connection.getClientInfo());
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (connection!=null) {
					connection.close();
					System.out.println("Connection is Closed.");
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		
	}
}

Output:

Establish Jdbc Connection Program Output

Establish Jdbc Connection Program Output

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages