Home | | Internet Programming | JDBC drivers

Chapter: Web or internet Programming : Server Side Programming

JDBC drivers

JDBC is an API for the Java programming language that defines how a client may access a database. It provides methods for querying and updating data in a database.

JDBC drivers


JDBC is an API for the Java programming language that defines how a client may access a database. It provides methods for querying and updating data in a database. JDBC is oriented towards relational databases. JDBC was first introduced in the Java 2 Platform, together with a reference implementation JDBC-to-ODBC bridge, enabling connections to any ODBC-accessible data source in the JVM host environment.


JDBC Driver Types


JDBC drivers are divided into four types.

Type 1: JDBC-ODBC Bridge driver (Bridge)

Type 2: Native-API/partly Java driver (Native)

Type 3: All Java/Net-protocol driver (Middleware)


Type 4: All Java/Native-protocol driver (Pure)


Type 1 JDBC-ODBC Bridge driver:


       JDBC-ODBC bridge driver provided by Sun


       Translates query obtained by JDBC into corresponding ODBC query, which is then handled by the ODBC driver.

       Client -> JDBC Driver -> ODBC Driver -> Database

       There is some overhead associated with the translation work to go from JDBC to ODBC.

       Bridge driver is recommended only for experimental use or when no other alternative is available.

Type 2: Native-API/partly Java driver


       This converts JDBC calls into database-specific calls i.e. this driver is specific to a particular database.

       The driver converts JDBC method calls into native calls of the database API.


       The type 2 driver is not written entirely in Java as it interfaces with non-Java code that makes the final database calls.

         Client -> JDBC Driver -> Vendor Client DB Library -> Database


Example: Oracle will have oracle native api.


Type 3: All Java/Net-protocol driver


       The JDBC type 3 driver, also known as the network-protocol driver is a database driver implementation which makes use of a middle-tier between the calling program and the database.


       The middle-tier (application server) converts JDBC calls directly or indirectly into the vendor-specific database protocol


       This differs from the type 4 driver in that the protocol conversion logic resides not at the client, but in the middle-tier.

Type 4: All Java/Native-protocol driver


       The JDBC type 4 driver, also known as the native-protocol driver is a database driver implementation that converts JDBC calls directly into the vendor-specific database protocol.


       The Type 4 uses java networking libraries to communicate directly with the database server.


       Type 4 drivers are all Java drivers. This means that there is no client installation or configuration.

JDBC Steps

Before create a java jdbc connection to the database, you must first import the java.sql package.


1. Loading a database driver


In this step of the jdbc connection process, we load the driver class by calling Class.forName() with the Driver class name as an argument. Once loaded, the Driver class creates an instance of itself. A client can connect to Database Server through JDBC Driver. Since most of the Database servers support ODBC driver therefore JDBC-ODBC Bridge driver is commonly used.


The return type of the Class.forName (String ClassName) method is ―Class‖. Class is a class in java.lang package.


try {


Class.forName(‖sun.jdbc.odbc.JdbcOdbcDriver‖); //Or any other driver



catch(Exception x){

System.out.println( ―Unable to load the driver class!‖ );






2. Creating a oracle jdbc Connection


The JDBC DriverManager class defines objects which can connect Java applications to a JDBC driver. DriverManager is considered the backbone of JDBC architecture. DriverManager class manages the JDBC drivers that are installed on the system. Its getConnection() method is used to establish a connection to a database. It uses a username, password, and a jdbc url to establish a connection to the database and returns a connection object.




Connection dbConnection = DriverManager. getConnection (url, ‖loginName‖, ‖Password‖ );



catch( SQLException x ){

System.out.println( ―Couldn‘t get connection!‖ );





3. Creating a jdbc Statement object,


Once a connection is obtained we can interact with the database. Connection interface defines methods for interacting with the database via the established connection. To execute SQL statements, you need to instantiate a Statement object from your connection object by using the createStatement() method.


Statement statement = dbConnection.createStatement();


A statement object is used to send and execute SQL statements to a database. Three kinds of Statements

Statement: Execute simple sql queries without parameters.


Statement createStatement() - creates an SQL Statement object. Prepared Statement: Execute precompiled sql queries with or without parameters.


PreparedStatement prepareStatement(String sql) - returns a new PreparedStatement object. PreparedStatement objects are precompiled SQL statements. Callable Statement: Execute a call to a database stored procedure.


CallableStatement prepareCall(String sql) - returns a new CallableStatement object. CallableStatement objects are SQL stored procedure call statements4. Executing a SQL statement with the Statement object, and returning a jdbc resultSet.


Statement interface defines methods that are used to interact with database via the execution of SQL statements. The Statement class has three methods for executing statements: executeQuery(), executeUpdate(), and execute(). For a SELECT statement, the method to use is executeQuery . For statements that create or modify tables, the method to use is executeUpdate. Note: Statements that create a table, alter a table, or drop a table are all examples of DDL statements and are executed with the method executeUpdate. execute() executes an SQL statement that is written as String object.


ResultSet provides access to a table of data generated by executing a Statement. The table rows are retrieved in sequence. A ResultSet maintains a cursor pointing to its current row of data. The next() method is used to successively step through the rows of the tabular results.


ResultSetMetaData Interface holds information on the types and properties of the columns in a ResultSet. It is constructed from the Connection object.


simple web application

import java.io.*;


import java.sql.*;

import javax.servlet.*;


import javax.servlet.http.*;


public class DBPhoneLookup extends HttpServlet {


public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {


Connection con = null;

Statement stmt = null;

ResultSet rs = null;


res.setContentType("text/html"); PrintWriter out = res.getWriter();


try {


//  Load (and therefore register) the Oracle Driver Class.forName("oracle.jdbc.driver.OracleDriver");


// Get a Connection to the database


con = DriverManager.getConnection( "jdbc:oracle:thin:@dbhost:1528:ORCL",


"user", "passwd");


//  Create a Statement object stmt = con.createStatement();


// Execute an SQL query, get a ResultSet


rs = stmt.executeQuery("SELECT NAME, PHONE FROM EMPLOYEES");


// Display the result set as a list




out.println("<UL>"); while(rs.next()) {

out.println("<LI>" + rs.getString("name") + " " + rs.getString("phone"));








catch(ClassNotFoundException e) {

out.println("Couldn't load database driver: " + e.getMessage());



catch(SQLException e) {


out.println("SQLException caught: " + e.getMessage());



Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Web or internet Programming : Server Side Programming : JDBC drivers |

Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.