as 2
as 2
as 2
JDBC (Java Database Connec vity) is a Java API for connec ng and execu ng queries on a database. The
design of JDBC is based on the following key components:
2. **Driver**: JDBC driver is a so ware component that enables Java applica ons to interact with the
database. There are four types of JDBC drivers: Type 1 (JDBC-ODBC bridge), Type 2 (Na ve API), Type 3
(Network Protocol), and Type 4 (Thin driver).
3. **Connec on**: The Connec on interface represents a connec on to a database. It is used to create
Statement, PreparedStatement, and CallableStatement objects for execu ng SQL queries.
4. **Statement**: The Statement interface is used to execute SQL queries against the database. It can be
a simple statement, prepared statement, or callable statement.
5. **ResultSet**: The ResultSet interface represents the result of a SQL query. It provides methods for
itera ng over the result set and retrieving data from it.
JDBC drivers are used to connect Java applica ons to different types of databases. The JDBC architecture
consists of the following layers:
1. **JDBC API**: This is the topmost layer of the JDBC architecture. It consists of interfaces and classes
that define the JDBC API. The API provides methods for connec ng to a database, execu ng queries, and
processing results.
2. **Driver API**: This layer defines the interfaces and classes that a JDBC driver must implement. It
includes interfaces such as Driver, Connec on, Statement, ResultSet, etc.
3. **Driver Manager**: The DriverManager class manages the JDBC drivers. It loads the drivers
dynamically based on the connec on URL provided by the applica on.
4. **JDBC-ODBC Bridge**: The JDBC-ODBC bridge is a special type of JDBC driver that allows Java
applica ons to interact with ODBC (Open Database Connec vity) drivers. It is used when a direct JDBC
driver is not available for a par cular database.
4. **Processing the ResultSet**: If the query returns a result set, use the ResultSet object to iterate over
the results and retrieve data.
5. **Closing Resources**: Close the ResultSet, Statement, and Connec on objects when they are no
longer needed to free up resources.
**Q4: Write the program of the following with explana on: [CO2]**
- The program connects to a MySQL database and performs the following opera ons:
- Finally, it closes the ResultSet, Statement, and Connec on objects to release resources.
import java.sql.*;
try {
// Establish connec on
while (resultSet.next()) {
// B) Inser on of data
// C) Upda on of data
// D) Dele on of data
// Close resources
resultSet.close();
statement.close();
connec on.close();
} catch (SQLExcep on e) {
e.printStackTrace();
```