What is the JDBC?
Java Database Connectivity (JDBC) is a standard Java API to interact with relational databases form Java. JDBC has set of classes and interfaces which can use from Java application and talk to database without learning RDBMS details and using Database Specific JDBC Drivers.
Features of JDBC 4.0
The major features added in JDBC 4.0 include :
· Auto-loading of JDBC driver class
· Connection management enhancements
· Support for RowId SQL type
· DataSet implementation of SQL using Annotations
· SQL exception handling enhancements
· SQL XML support
Basic Steps in writing a Java program using JDBC
JDBC makes the interaction with RDBMS simple and intuitive. When a Java application needs to access database :
· Load the RDBMS specific JDBC driver because this driver actually communicates with the database (Incase of JDBC 4.0 this is automatically loaded).
· Open the connection to database which is then used to send SQL statements and get results back.
· Create JDBC Statement object. This object contains SQL query.
· Execute statement which returns resultset(s). ResultSet contains the tuples of database table as a result of SQL query.
· Process the result set.
· Close the connection.
JDBC Architecture.
The JDBC Architecture consists of two layers:
· The JDBC API, which provides the application-to-JDBC Manager connection.
· The JDBC Driver API, which supports the JDBC Manager-to-Driver Connection.
The JDBC API uses a driver manager and database-specific drivers to provide transparent connectivity to heterogeneous databases. The JDBC driver manager ensures that the correct driver is used to access each data source. The driver manager is capable of supporting multiple concurrent drivers connected to multiple heterogeneous databases. The location of the driver manager with respect to the JDBC drivers and the Java application is shown in Figure 1.
Figure 1: JDBC Architecture
Main components of JDBC
The life cycle of a servlet consists of the following phases:
- DriverManager: Manages a list of database drivers. Matches connection requests from the java application with the proper database driver using communication subprotocol. The first driver that recognizes a certain subprotocol under JDBC will be used to establish a database Connection.
- Driver: The database communications link, handling all communication with the database. Normally, once the driver is loaded, the developer need not call it explicitly.
- Connection : Interface with all methods for contacting a database.The connection object represents communication context, i.e., all communication with database is through connection object only.
- Statement : Encapsulates an SQL statement which is passed to the database to be parsed, compiled, planned and executed.
- ResultSet: The ResultSet represents set of rows retrieved due to query execution.
JDBC application works
A JDBC application can be logically divided into two layers:
1. Driver layer
2. Application layer
- Driver layer consists of DriverManager class and the available JDBC drivers.
- The application begins with requesting the DriverManager for the connection.
- An appropriate driver is choosen and is used for establishing the connection. This connection is given to the application which falls under the application layer.
- The application uses this connection to create Statement kind of objects, through which SQL commands are sent to backend and obtain the results.
Figure 2: JDBC Application
No comments:
Post a Comment