Unit 1: JDBC (Java Database Connectivity)
Unit 1: JDBC (Java Database Connectivity)
Unit 1: JDBC (Java Database Connectivity)
JDBC is a standard Java API for a database-independent connectivity between the Java
programming language and a wide range of databases. This application program interface lets
you encode the access request statements, in Structured Query Language (SQL). They are
then passed to the program that manages the database. It mainly involves opening a
connection, creating a SQL Database, executing SQL queries and then arriving at the output.
We can use JDBC API to access tabular data stored in any relational database. By the help of
JDBC API, we can save, update, delete and fetch data from the databases.
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 driver manager is capable of supporting
multiple concurrent drivers connected to multiple heterogeneous databases.
When Java first came out, this was a useful driver because most databases only supported
ODBC access but now this type of driver is recommended only for experimental use or when
no other alternative is available.
Type 2: JDBC-Native API
In a Type 2 driver, JDBC API calls are converted into native C/C++ API calls, which are
unique to the database. These drivers are typically provided by the database vendors and used
in the same manner as the JDBC-ODBC Bridge. The vendor-specific driver must be installed
on each client machine.
This kind of driver is extremely flexible since it requires no code installed on the client and a
single driver can actually provide access to multiple databases. You can think of the
application server as a JDBC “proxy,” meaning that it makes calls for the client application.
As a result, you need some knowledge of the application server’s configuration in order to
effectively use this driver type. Your application server might use a Type 1, 2, or 4 drivers to
communicate with the database.
Type 4: 100% Pure Java
In a Type 4 driver, a pure Java-based driver communicates directly with the vendor’s
database through a socket connection. This is the highest performance driver available for the
database and is usually provided by the vendor itself.
Example:
2) PreparedStatement
PreparedStatement is used to execute dynamic or parameterized SQL queries.
PreparedStatement extends Statement interface. You can pass the parameters to SQL query at
run time using this interface
//Creating PreparedStatement object
PreparedStatement pstmt = con.prepareStatement("update STUDENT set NAME = ? where
ID = ?");
//Setting values to place holders using setter methods of PreparedStatement object
pstmt.setString(1, "MyName"); //Assigns "MyName" to first place holder
pstmt.setInt(2, 111); //Assigns "111" to second place holder
//Executing PreparedStatement
pstmt.executeUpdate();
3) CallableStatement
ResultSet
A ResultSet is a Java object that contains the results of executing an SQL query. In other
words, it contains the rows that satisfy the conditions of the query. The data stored in a
ResultSet object is retrieved through a set of get methods that allows access to the various
The general form of a result set is a table with column headings and the corresponding values
returned by a query.
Commonly used methods of ResultSet interface