YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES BCA 2017 ONWARS BATCH
PRORAMMING
What is JDBC?
JDBC or Java Database Connectivity is a specification from Sun microsystems that provides a
standard abstraction (that is API or Protocol) for java applications to communicate with various
databases. It provides the language with java database connectivity standard. It is used to write
programs required to access databases. JDBC along with the database driver is capable of accessing
databases and spreadsheets.
JDBC Architecture
The JDBC API supports both two-tier and three-tier processing models for database access but in
general, JDBC Architecture consists of two layers −
JDBC API: This provides the application-to-JDBC Manager connection.
JDBC Driver API: This 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.
Following is the architectural diagram, which shows the location of the driver manager with respect
to the JDBC drivers and the Java application −
1
Page
, BCA - JAVA
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES BCA 2017 ONWARS BATCH
PRORAMMING
Common JDBC Components
The JDBC API provides the following interfaces and classes −
Driver Manager: This class manages a list of database drivers. Matches connection requests
from the java application with the proper database driver using communication sub protocol.
The first driver that recognizes a certain sub protocol under JDBC will be used to establish a
database Connection.
Driver: This interface handles the communications with the database server. You will interact
directly with Driver objects very rarely. Instead, you use Driver Manager objects, which
manages objects of this type. It also abstracts the details associated with working with Driver
objects.
Connection: This 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: You use objects created from this interface to submit the SQL statements to the
database. Some derived interfaces accept parameters in addition to executing stored
procedures.
ResultSet: These objects hold data retrieved from a database after you execute an SQL query
using Statement objects. It acts as an iterator to allow you to move through its data.
SQLException: This class handles any errors that occur in a database application.
JDBC Connectivity Model
2
Page
, BCA - JAVA
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES BCA 2017 ONWARS BATCH
PRORAMMING
Types of JDBC Architecture
The JDBC architecture consists of two-tier and three-tier processing models to access a database. They
are as described below:
1. Two-tier model: A java application communicates directly to the data source. The
JDBC driver enables the communication between the application and the data source.
When a user sends a query to the data source, the answers for those queries are sent
back to the user in the form of results.The data source can be located on a different
machine on a network to which a user is connected. This is known as a client/server
configuration, where the user’s machine acts as a client and the machine having the
data source running acts as the server.
2. Three-tier model: In this, the user’s queries are sent to middle-tier services, from
which the commands are again sent to the data source. The results are sent back to the
middle tier, and from there to the user. This type of model is found very useful by
management information system directors.
Creating JDBC Application
There are following six steps involved in building a JDBC application −
Import the packages: Requires that you include the packages containing the JDBC
classes needed for database programming. Most often, using import java.sql.* will
suffice.
Register the JDBC driver: Requires that you initialize a driver so you can open a
communication channel with the database.
Open a connection: Requires using the DriverManager.getConnection() method to
create a Connection object, which represents a physical connection with the database.
Execute a query: Requires using an object of type Statement for building and
submitting an SQL statement to the database.
Extract data from result set: Requires that you use the appropriate method to retrieve
the data from the result set.
Clean up the environment: Requires explicitly closing all database resources versus
relying on the JVM's garbage collection.
3 Page