A Brief Introduction to JDBC


 

A Brief Introduction to JDBC

In this tutorial you will learn JDBC Architecture,API, Types of Drivers and their advantages and disadvantages

In this tutorial you will learn JDBC Architecture,API, Types of Drivers and their advantages and disadvantages

A Brief Introduction to JDBC

What is JDBC?

JDBC ( Java Database connectivity ) is an API ( Application Programming Interface ), which consists of set of java classes, interfaces and exceptions. It allows the programmer to connect java application to the database. It provides methods for querying and updating data into the database. It is relational database oriented.

Why Use JDBC?

JDBC helps the java developers to develop a data access application without having knowledge of complex API’s of different database vendors. You only need to learn JDBC to develop java Data Access Application to different database to different database applications using different JDBC drivers.

JDBC Architecture

JDBC attempts to create a platform neutral interface between database and java. JDBC API defines a set of interface that encapsulates major database functionality such as running query, updating results. A database vendor writes JDBC driver, which consists a set of classes and interfaces that implements interfaces of their database application.

JDBC API supports both 2-tier architecture and 3-tier architecture for accessing relational database model.

JDBC 2- Tier Architecture

In 2-tier architecture java application communicates directly with database. The database may reside on same machine or other machine in the network. The client will send the statement to database and result will communicated back to the client.

JDBC 2-Tier Architecture

JDBC 3- Tier Architecture
In 3-tier architecture a ‘middle-tier’ is used between java application and database. A java application sends a command to middle-tier, which then further sends to database. The database processes the query and returns the result to the middle-tier, which further returns to the application. This approach has the many advantages such as better maintainability, easier deployment, scalability, etc.

JDBC 3-Tier Architecture

JDBC APIs
As you knew that JDBC API is a collection of java classes and Interfaces. java.sql.*; and javax.sql.*; package contains the collection of JDBC APIs.
Following are the important classes and interfaces of java.sql.*; package.

    DriverManager – It is very important class, which defines object to open a connection to the database. It is also used to load JDBC driver in memory.

    Connection – The java database application manages the connection to a database. It represents a collection to the database. It is also used for creating Statement, PreparedStatement and CollableStatement Objects.

    Statement – Statement object is used to retrieve a result into ResultSet. It represents a static SQL statement.

    PreparedStatemet- It represents a precompiled SQL statement. It is alternative to Statement.

    CallableStatement- It represents a stored procedures execute stored them in RDBMS.

    ResultSet –It represents a set of results generated by SELECT Sql statement.

    SQLException- JDBC provides SQLException class, which contains the database access errors. It is a core JDBC class that provides database access errors. Many JDBC API’s throws SQLException.

JDBC Driver Types
There are 4 types of JDBC drivers available.

Type 1: JDBC-ODBC Bridge driver (Bridge) – They are JDBC-ODBC bridge drivers. They are slowest among all the four types. They delegate the work to ODBC API, i.e. they translates the all the JDBC calls into ODBC calls and send them to ODBC driver.

Type 1: JDBC-ODBC Bridge

    Advantage
    It allows the access to almost all the database, because the ODBC driver of database is already available.

    Disadvantage
    Since this driver translate all the JDBC calls to ODBC calls and then send them to ODBC driver and reverse is the same, there fore it becomes slower. The client requires ODBC installation on the client. This driver is not good for web.

Type 2: Native-API/partly Java driver (Native)- The type 2 driver mainly uses native API for data access. They provides java wrapper classes to be able to invoke JDBC driver. The type 2 drivers are specific to particular database application.

Type 2: Native-API/partly Java driver

    Advantage
    The type 2 driver offers better performance then JDBC-ODBC bridge driver. The type 2 driver uses native API which is database specific.

    Disadvantage
    The native API must be installed on client system to use type 2 driver, if database changes the native API must be change. They are not threadsafe.

Type 3: AllJava/Net-protocol driver (Middleware)- Type 3 driver are written purely in java. The type 3 database request are passes throw the network to the middle-tier server and this middle-tier server translates the request to the database.

0

Type 3: AllJava/Net-protocol driver

    Advantage
    Since this driver is fully written in java hence it portable and suitable for the web. This driver is server based therefore there is no need of any vendor specific database library present on the client machine. There are many opportunity to provide portability , scalability and performance.

    Disadvantage
    Type 3 driver requires another server application to install and maintain on the network. Traversing of data may require a long time since it comes from the backend server.

Type 4: All Java/Native-protocol driver (Pure)- The most commonly used driver is Type 4 driver. They are also written purely in java.

1

Type 4: All Java/Native-protocol driver

    Advantage
    Type 4 drivers are written purely in java to achieve platform independence. It is most suitable for the web. The numbers of translation layer are very less. They doesn’t translate the JDBC call to ODBC or they doesn’t pass request to any middle-tier server. There is no need to install any software install on the client.

    Disadvantage
    Every database application has its own type 4 driver. Therefore to use any database application you must have its type 4 drivers.
2

Ads