UNIT 6
INTERACTING WITH
DATABASE
ODBC
ODBC stands for Open Database
Connectivity
A standard or open application
programming interface (API) for
accessing a database.
ODBC provides a C interface for
database access on Windows
environment.
JDBC
JDBC stands
for Java Database Connectivity.
It is a standard Java API for
connecting programs written in Java
to the data in relational databases.
JDBC works with Java on a variety of
platforms, such as Windows, Mac
OS, and the various versions of
UNIX.
JDBC DRIVER
JDBC Driver is a software component that enables
java application to interact with the database. There
are 4 types of JDBC drivers:
JDBC-ODBC bridge driver
Native-API driver (partially java driver)
JDBC-Net pure Java/ Network-Protocol driver (fully java
driver)
Pure Java Driver /Thin driver / Database-Protocol
driver(fully java driver)
JDBC-ODBC BRIDGE DRIVER
The JDBC type 1 driver, also known as the JDBC-ODBC
bridge driver.
The JDBC-ODBC bridge driver uses ODBC driver to
connect to the database. The JDBC-ODBC bridge driver
converts JDBC method calls into the ODBC function calls.
Oracle does not support the JDBC-ODBC Bridge from Java 8.
Oracle recommends that you use JDBC drivers provided by the
vendor of your database instead of the JDBC-ODBC Bridge.
Advantages:
easy to use.
can be easily connected to any database.
Disadvantages:
Performance degraded because JDBC method call is converted
into the ODBC function calls.
The ODBC driver needs to be installed on the client machine.
NATIVE API DRIVER
The JDBC type 2 driver, also known as
the Native-API driver
The Native API driver uses the client-side
libraries of the database. The driver converts
JDBC method calls into native calls of the
database API. It is not written entirely in java.
Advantage:
•performance upgraded than JDBC-ODBC bridge driver.
Disadvantage:
•TheNative driver needs to be installed on the each client
machine.
•TheVendor client library needs to be installed on client
machine.
JDBC-NET PURE JAVA DRIVER
The JDBC type 3 driver, also known as the Pure Java
driver for database middleware. It is a database
driver implementation which makes use of a middle
tier between the calling program and the database.
The middle-tier (application server)
converts JDBC calls directly or indirectly into a vendor-
specific database protocol. It is fully written in java.
Advantage:
No client side library is required because of application
server that can perform many tasks like auditing, load
balancing, logging etc.
Disadvantages:
Network support is required on client machine.
Requires database-specific coding to be done in the
middle tier.
Maintenance of Network Protocol driver becomes costly
because it requires database-specific coding to be done in
the middle tier.
THIN DRIVER
The JDBC type 4 driver, also known as the Direct to Database Pure
Java Driver, is a database driver implementation that
converts JDBC calls directly into a vendor
specific database protocol.
That is why it is known as thin driver. It is fully written in Java
language.
Advantage:
Better performance than all other drivers.
No software is required at client side or server side.
Disadvantage:
Drivers depend on the Database.
JDBC TWO TIER MODEL
In a two-tier model, a Java application communicates
directly with the database, via the JDBC driver.
JDBC THREE TIER MODEL
In a three-tier model, a Java application
communicates with a middle tier component that
functions as an application server. The application
server talks to a given database using JDBC.
COMMON JDBC COMPONENTS
The JDBC API provides the following interfaces and
classes −
DriverManager Class
Connection Interface
Statement Interface
ResultSet Interface
COMMON JDBC COMPONENTS
DriverManager Class
The DriverManager class acts as an interface
between user and drivers.
It keeps track of the drivers that are available and
handles establishing a connection between a
database and the appropriate driver.
The DriverManager class maintains a list of Driver
classes that have registered themselves by calling
the method DriverManager.registerDriver().
Commonly used methods of DriverManager class
Method Description
public static void registerDriver( Driver is used to register the given
driver with DriverManager.
driver);
is used to deregister the
public static void deregisterDriver( Driver given driver (drop the driver
from the list) with
driver); DriverManager.
public static Connection getConnection is used to establish the
connection with the
specified url.
( String url);
public static Connection is used to establish the
connection with the
getConnection( String url, String specified url, username and
password.
userName, String password);
CONNECTION INTERFACE
A Connection is the session between java
application and database.
The Connection interface provide many methods for
transaction management like commit(), rollback()
etc.
When getConnection() method is called, it returns a
connection object.
Connection
con=DriverManager.getConnection(URL);
Commonly used methods of Connection interface
Method Description
public Statement creates a statement
object that can be used to
createStatement(); execute SQL queries.
public void It is used to set the
setAutoCommit(boole commit status. By default
it is true.
an status);
It saves the changes
made since the previous
public void commit(); commit/rollback
permanent.
Drops all changes made
public void rollback(); since the previous
commit/rollback.
closes the connection and
Releases a JDBC resources
public void close(); immediately.
STATEMENT INTERFACE
The Statement interface provides methods to
execute queries with the database.
It provides factory method to get the object of
ResultSet.
Commonly used methods of Statement interface
Method Description
public ResultSet
used to execute SELECT query.
executeQuery(String It returns the object of ResultSet.
sql);
public int used to execute specified query,
executeUpdate(String it may be create, drop, insert,
sql); update, delete etc.
public boolean used to execute queries that may
execute(String sql); return multiple results.
public int[] used to execute batch of
executeBatch(); commands.
void close() Close the statement object
RESULTSET INTERFACE
A ResultSet object provides access to a table of
data.
ResultSet object is usually generated by executing
a statement.
The object of ResultSet maintains a cursor pointing
to a particular row of data.
Initially, cursor points before the first row.
Commonly used methods of ResultSet interface
Method Description
public boolean next(); is used to move the cursor to the one row next
from the current position.
public boolean previous(); is used to move the cursor to the one row
previous from the current position.
public boolean first(); is used to move the cursor to the first row in
result set object.
public boolean last(); is used to move the cursor to the last row in
result set object.
public boolean absolute(int is used to move the cursor to the specified row
number in the ResultSet object.
row);
public int getInt(int is used to return the data of specified column
index of the current row as int.
columnIndex);
public int getInt(String is used to return the data of specified column
name of the current row as int.
columnName);
public String getString(int is used to return the data of specified column
index of the current row as String.
columnIndex);
public String is used to return the data of specified column
name of the current row as String.
getString(String