Types of JDBC Drivers in Java

A Java API called Java Database Connectivity (JDBC) enables Java programs to access and work with data stored in databases. JDBC offers a common set of classes and interfaces to connect to databases, run SQL commands, and retrieve data. The software elements known as JDBC drivers enable Java programs to connect to particular kinds of databases. This article will discuss the various JDBC driver types in Java.

JDBC-ODBC Bridge Driver, Type 1 JDBC Driver

The JDBC-ODBC bridge driver is another name for the Type 1 JDBC driver. ODBC drivers connect Java programs to databases using this obsolete driver. To connect to the database, the driver translates JDBC calls into ODBC calls, which are then forwarded to the ODBC driver via the driver. This driver should not be used in production situations and is only advised for usage in small-scale applications.

Native-API/partially Java Type 2 JDBC Driver

The Native-API/Partially Java driver is another name for the Type 2 JDBC driver. To connect to databases, this driver employs native database client libraries. A Java component of the driver interacts with a native component via a vendor-specific API. Although this driver performs better than the Type 1 driver, it is still not advised to use it in production settings.

Network-Protocol/Java Middleware Driver, Type 3 JDBC Driver

The Network Protocol/Java Middleware Driver is another name for the Type 3 JDBC driver. In order to connect to databases, this driver employs a middleware server. A Java client component of the driver uses a vendor-specific protocol to connect with a middleware server. Then, the middleware server uses a vendor-specific protocol to connect to the database server. Compared to the Type 2 driver, this driver offers improved performance and scalability.

Pure Java Driver, Type 4 JDBC Driver

The Pure Java driver is another name for the Type 4 JDBC driver. This driver utilizes a vendor-specific protocol to interface directly with the database server and is entirely Java-based. The client system does not need to have any native components or middleware servers installed to use this driver. It is advised to use this driver in production applications since it offers optimum performance and scalability.

Steps for Connecting to JDBC

Step 1: Load the JDBC driver class

The JDBC driver class must be loaded first. Setting up a connection to the database is the responsibility of the driver class. The JDBC driver class may be loaded using the Class.forName() function.

Step 2: Establish a connection to the database

Setting up a connection to the database is the next step. Use of DriverManager is possible.To connect to the database, use the getConnection() function.

The database's URL is specified by the URL option. The user is authenticated using the username and password parameters.

Step 3: Create a statement object

You may build a statement object once you've established a connection to the database. SQL statements are carried out using the statement object.

Step 4: Carry out a SQL command

You may use a statement object to execute SQL statements once you've created one. SELECT statements are carried out using the executeQuery() function, whereas INSERT, UPDATE, and DELETE statements are carried out using the executeUpdate() method.

The SQL statement to be performed is specified by the sql argument. The SELECT statement's output is included in a ResultSet object that is returned by the executeQuery() function.

Step 5: Process the result set

A while loop can be used to process the result set. The cursor is advanced to the following entry in the result set using the next() function. The values of the columns in the current row can be retrieved using the getString(), getInt(), or getDouble() functions.

The id, name, and balance values for each row in the result set are retrieved by this code, which then outputs them to the console.

Filename: JdbcExample.java

Customer Table

idnamebalance
1John Doe1000.00
2Jane Doe2000.00

Output:

ID: 1, Name: John Doe, Balance: 1000.0
ID: 2, Name: Jane Doe, Balance: 2000.0