Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Text lesson

Overview of JDBC

[post-views]

 

In this lesson, you will learn.

  • Background
  • Architecture of Java Database Connectivity(JDBC)
  • Types of JDBC Driver

 

Background

Consider a scenario where you have to develop an application for a login system that maintains a record of user credentials. You need to install MySQL Server as an RDBMS that will store the data of each user.

Here is the table design

 

create table User (Username varchar(100),
                  Password varchar(100));

 

The following figure shows the login system developed in Java that interacts with the user database using the JDBC API.

 

 

Java Database Connectivity(JDBC) Architecture

Java programs cannot send and receive data and query results directly from a database. This is because a database can only understand SQL statements—not the statements written in Java. Consequently, a method for converting Java statements into SQL statements is required.

 

This type of translation is enabled by the JDBC architecture.

 

 

JDBC Application Layer: It is responsible for managing database connections and provides the ing a standard database access API.

JDBC Driver Layer: It is responsible for interacting with a specific database, translating requests into an SQL statement, and packaging the execution result into a Java-recognized data format.

Database vendors provide different drivers, but all must implement standard JDBC APIs.

 

Types of JDBC Driver

JDBC drivers are categorized into four types, each with different mechanisms and architectures.

 

1. Type-1: JDBC-ODBC Bridge Driver

  • The Type 1 driver uses the ODBC (Open Database Connectivity) driver to connect to the database.
  • JDBC calls are translated into ODBC calls, which are then handled by the ODBC driver.

 

Advantages:

  • Easy to set up and use.
  • Can connect to any database that provides an ODBC driver, offering a broad range of database compatibility.

 

Disadvantages:

  • Performance overhead due to the translation from JDBC to ODBC.
  • Requires the ODBC driver to be installed and configured on the client machine.
  • Platform-dependent as it relies on native ODBC libraries, making it less suitable for web applications.

 

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

  • The Type 2 driver converts JDBC calls into native database calls using database-specific native libraries (usually written in C/C++).
  • These native libraries must be installed on the client machine.

 

Advantages:

  • Generally offers better performance than Type 1 drivers as it uses native API calls directly.
  • Can utilize the full capabilities of the database through the native API.

 

Disadvantages:

  • Requires native libraries to be installed and maintained on each client machine.
  • Platform-dependent, meaning a separate driver is needed for each operating system.

 

Type 3: Network Protocol Driver (pure Java driver for database middleware)

  • The Type 3 driver communicates with a middleware server, which in turn communicates with the database.
  • The middleware server handles the database-specific protocols, freeing the client from needing database-specific drivers.

 

Advantages:

  • Pure Java implementation, making it platform-independent.
  • Provides flexibility in connecting to multiple databases through the middleware.
  • Suitable for web applications and networked environments where the middleware can be centrally managed.

 

Disadvantages:

  • Performance depends on the efficiency of the middleware server.
  • Additional network overhead due to the extra communication layer with the middleware.

 

Type 4: Thin Driver (pure Java driver)

  • The Type 4 driver directly converts JDBC calls into the database-specific protocol without requiring native code or middleware.
  • Communicates directly with the database server using the database’s proprietary protocol.

 

Advantages:

  • Pure Java implementation, ensuring platform independence.
  • Direct database communication provides the best performance among all driver types.
  • Simplified deployment as no additional software (like ODBC or native libraries) is needed on the client.

 

Disadvantages:

  • Database-specific, requiring a different Type 4 driver for each database.

 

 

 


 

End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review