JDBC Tutorial

JDBC Tutorial

JDBC Tutorial

Introduction to Java JDBC

JDBC stands for Java Database Connectivity. JDBC API is a Java API which is used to access tabular data, especially which are stored in the relational database. JDBC works with Java on platforms like Windows, MacOS, and UNIX.

Java Applications

What is JDBC?

JDBC is used for connecting java programming language with databases.

JDBC library has APIs for each task, which is mentioned below.

  1. Creating a connection to a database.
  2. Creating statements for SQL or MySQL.
  3. Executing queries using SQL or MySQL in the database.
  4. Viewing & modifying the resulting records.

JDBC provides a complete set of an interface which allows access to the database. It can also be used by different types of executables, they are as follows:

  1. Java Applications
  2. Java Applets
  3. Java Servlets
  4. Java ServerPages (JSPs)
  5. Enterprise JavaBeans (EJBs).

All these executables are able to use a JDBC driver to access a database.

The java.sql package contains classes and interfaces for JDBC API. Following is a list of some interfaces of JDBC API:

  1. Driver interface
  2. Connection interface
  3. Statement interface
  4. PreparedStatement interface
  5. CallableStatement interface
  6. ResultSet interface
  7. ResultSetMetaData interface
  8. DatabaseMetaData interface
  9. RowSet interface

Following is a list of some interfaces of JDBC API:

  1. DriverManager class
  2. Blob class
  3. Clob class
  4. Types class

JDBC Architecture

JDBC Architecture consists of two layers:

  1. JDBC API: It provides the application-to-JDBC Manager connection.
  2. JDBC Driver API: It supports the JDBC Manager-to-Driver Connection.

For transparent connectivity, JDBC API uses a driver manager and database-specific drivers.

JDBC driver manager checks that the correct driver is used to access each data source. Driver manager can support multiple concurrent drivers which may be connected to multiple databases.

JDBC Driver

JDBC drivers implement the defined interfaces in the JDBC API, for interacting with your database server.

Java.sql package contains classes and their behaviors and their main implementation is done in third-party drivers. The Third-party vendor implements the java.sql.Driver interface in their database driver.

There are four types of drivers as following:

Type 1: JDBC-ODBC Bridge Driver

In Type 1 driver, a JDBC bridge accesses ODBC driver which is installed on each client machine. Data Source Name (DSN) is to be configured on the client machine which represents the target database. This type of driver is only for experimental use.

JDBC-ODBC Bridge Driver

Type 2: JDBC-Native API

In Type 2 driver, JDBC API calls are converted into native C/C++ API calls, these calls are unique to the database. These types of drivers are provided by database vendors and it is used in the same manner as the JDBC-ODBC Bridge. This driver which is provided by vendors must be installed on each client machine. If the database is changed then the native API also must be changed, because native API is specific to the database.


Type 3: JDBC-Net pure Java

In Type 3 driver, to access the database, a three-tier approach is used. Standard network sockets are used by JDBC clients to communicate with the middleware application server. These middleware application servers translate socket information into call format which is required by the DBMS and then forward it to the database server. As it does not requires any code installation on the client that is why this kind of drivers are extremely flexible.

JDBC-Net pure Java

Type 4: 100% Pure Java

In Type 4 driver, pure Java-based driver communicates with the vendor's database directly through a socket connection. Type 4 driver has the highest performance and it is provided by the vendor itself. It does not require special software on the client or server that is why it is extremely flexible and also these drivers can be downloaded dynamically.

Pure Java-based driver communicates

Advantages of JDBC in Java

  1. If proper drivers are installed, any database can be read.
  2. Creates XML structure of data from the database automatically.
  3. No content conversion required.
  4. Query and Stored procedure supported.
  5. Can be used for both Synchronous and Asynchronous processing.
  6. Supports modules.

Disadvantages of JDBC in Java

  1. Correct drivers need to be deployed for each type of database.
  2. Cannot update or insert multiple tables with a sequence.