5.3 JDBC Drivers - connectivity
5.3 JDBC Drivers - connectivity
• JDBC (Java Database Connectivity) drivers enable Java applications to interact with
databases.
• These drivers act as a bridge between the Java application and the database.
• They convert the high-level JDBC API calls into database-specific calls that can be
understood by the underlying database.
• There are four types of JDBC drivers, each offering different methods of
communication between Java applications and databases.
• Below is a detailed explanation of each JDBC driver types:
The Type 1 driver translates JDBC calls into ODBC (Open Database Connectivity) calls.
ODBC is a standard API for accessing database management systems (DBMS).
• Example:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:DSNName",
"username", "password");
The Type 2 driver converts JDBC calls into database-specific native API calls. The
communication between the Java application and the database happens through the database's
native protocol.
• Example:
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"username", "password");
The Type 3 driver uses a middleware server that translates JDBC calls into the database's native
protocol. The middleware server allows the client (Java application) to communicate with any
type of database over a network using a standardized network protocol.
• Example:
Class.forName("com.informix.jdbc.IfxDriver");
Connection conn = DriverManager.getConnection("jdbc:informix-
sqli://localhost:1526/testdb:INFORMIXSERVER=myserver", "username",
"password");
4. Type 4: Thin Driver (Pure Java Driver)
The Type 4 driver is a pure Java driver that directly translates JDBC calls into the database's
native protocol. There is no intermediary layer or native code involved, which makes it a high-
performance option.
• Example:
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb",
"root", "password");
Java provides several APIs and methods for database interaction, with JDBC (Java Database
Connectivity) being the most common and widely used approach.
In Java, database connectivity is achieved through JDBC, which provides a standard interface
for interacting with relational databases.
It allows Java applications to send SQL queries to the database and process the results.
• Before establishing a connection to the database, the JDBC driver for the specific
database must be loaded into the application.
• This driver is responsible for translating Java commands into commands that the
database can understand.
For example:
• Once the connection is established, a Statement object is created to send SQL queries
to the database.
Example:
Statement stmt = conn.createStatement();
• You can execute SQL queries using the Statement object. There are three main
methods for executing queries:
o executeQuery(): Used for executing SELECT queries that return a result set.
o executeUpdate(): Used for executing INSERT, UPDATE, DELETE queries, and other
SQL commands that do not return a result set.
o execute(): Used for executing a general SQL query (returns a boolean indicating
whether the query is a result set or not).
• When executing a SELECT query, the results are returned as a ResultSet object. This
object allows you to iterate through the result and access the data.
Example:
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println("ID: " + id + ", Name: " + name);
}
• The next() method moves the cursor to the next row of the result set, and you can
access the column values using methods like getInt(), getString(), etc.
6. Close the Connection
• Once the database operations are completed, you should close the ResultSet,
Statement, and Connection objects to release the database resources.
Example:
rs.close();
stmt.close();
conn.close();
Example Code: Connecting to MySQL Database
Below is a complete example that connects to a MySQL database, retrieves some data, and
processes the result:
import java.sql.*;
• When dealing with dynamic queries, such as when input from users is involved, it's
recommended to use a PreparedStatement.
• This helps prevent SQL injection attacks and also allows the reuse of SQL queries.
Example:
• JDBC supports transaction management, allowing you to commit or roll back database
changes to ensure data consistency and integrity.
To begin a transaction:
conn.setAutoCommit(false); // Disable auto-commit
Don't forget to set auto-commit back to true once the transaction is complete:
conn.setAutoCommit(true);
Handling Exceptions in JDBC