Author
JDBC drivers are used to connect Java applications with databases. They act as a bridge between Java code and the database by converting Java calls into database-specific commands.
? Without a JDBC driver, Java cannot communicate with the database.
This driver converts JDBC calls into ODBC calls, which are then passed to the database.
? It acts as a bridge between Java (JDBC) and ODBC-based database drivers.
Example
import java.sql.*;
public class Type1Example {
public static void main(String[] args) throws Exception {
// Load JDBC-ODBC Bridge Driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Connect using ODBC DSN
Connection con = DriverManager.getConnection(
"jdbc:odbc:mydsn", "", "");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while (rs.next()) {
System.out.println(rs.getString(1));
}
con.close();
}
}
Explanation
"sun.jdbc.odbc.JdbcOdbcDriver" → Old bridge driver"jdbc:odbc:mydsn" → Uses ODBC Data Source Name (DSN)
? This driver is deprecated and not used in modern applications because better alternatives are available.
This driver uses database-specific native libraries to communicate with the database.
? It converts JDBC calls into native database API calls.
Example
import java.sql.*;
public class Type2Example {
public static void main(String[] args) throws Exception {
// Load Native Driver (example: Oracle OCI Driver)
Class.forName("oracle.jdbc.driver.OracleDriver");
// Connect to database
Connection con = DriverManager.getConnection(
"jdbc:oracle:oci:@localhost:1521:xe", "system", "password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while (rs.next()) {
System.out.println(rs.getString(1));
}
con.close();
}
}
Explanation
"oracle.jdbc.driver.OracleDriver" → Native (OCI) driver"jdbc:oracle:oci" → Uses Oracle Call Interface (native library)
? This driver was used in older enterprise systems but is rarely used today due to portability issues.
This driver uses a middleware server to communicate with the database.
? It sends requests to a middle layer, which converts them into a database-specific protocol.
Example
import java.sql.*;
public class Type3Example {
public static void main(String[] args) throws Exception {
// Load driver (example placeholder)
Class.forName("com.middleware.Driver");
// Connect via middleware server
Connection con = DriverManager.getConnection(
"jdbc:middleware://localhost:5000/mydb", "user", "password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while (rs.next()) {
System.out.println(rs.getString(1));
}
con.close();
}
}
Explanation
"jdbc:middleware://..." → Connects to middleware server
? This driver is useful in enterprise environments but is less common today compared to Type 4 drivers.
This is a pure Java driver that directly communicates with the database.
? It does not require any middleware or native libraries.
Example
import java.sql.*;
public class Type4Example {
public static void main(String[] args) throws Exception {
// Load driver (optional in modern Java)
Class.forName("com.mysql.cj.jdbc.Driver");
// Direct connection to database
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testdb", "root", "password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while (rs.next()) {
System.out.println(rs.getString("name"));
}
con.close();
}
}
Explanation
"com.mysql.cj.jdbc.Driver" → MySQL Type 4 driver"jdbc:mysql://..." → Direct database connection URL
? This is the most commonly used driver in modern Java applications due to its simplicity, speed, and portability.
Choosing the right JDBC driver depends on your application requirements, performance needs, and system architecture.
When to Use Type 4 Driver
? Type 4 driver is preferred because it is fast, simple, and platform independent.
When to Use Type 3 Driver
? Useful when multiple databases are managed through a middle layer (middleware server).
Drivers to Avoid
? In real-world development, Type 4 driver is the best choice in almost all cases due to its performance, simplicity, and flexibility.
import java.sql.*;
public class TestJDBC {
public static void main(String[] args) throws Exception {
// Step 1: Load Driver (optional in modern Java)
Class.forName("com.mysql.cj.jdbc.Driver");
// Step 2: Create Connection
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testdb",
"root",
"password"
);
// Step 3: Create Statement
Statement stmt = con.createStatement();
// Step 4: Execute Query
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
// Step 5: Process Result
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2));
}
// Step 6: Close Connection
con.close();
}
}
Output (Example)
1 Amit
2 Rahul
Explanation
Class.forName() → Loads the JDBC driver into memoryDriverManager.getConnection() → Establishes connection with the databasecreateStatement() → Creates a SQL statement objectexecuteQuery() → Executes the SQL query and returns dataResultSet → Stores and processes the query resultcon.close() → Closes the connection and frees resourcesStatement instead of PreparedStatement (less secure)JDBC drivers play a very important role in Java database connectivity.
? Understanding JDBC drivers is essential for building efficient, scalable, and real-world backend applications and is also very important for Java interviews.