JDBC(upadated)
JDBC(upadated)
JDBC(upadated)
Type-1 driver or 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.
Type-1 driver is also called Universal driver because it can be used to connect to any of
the databases.
JDBC-ODBC bridge driver – Type 1
driver
• Advantages
• This driver software is built-in with JDK so no need to install
separately.
• It is a database independent driver.
• Disadvantages
• As a common driver is used in order to interact with different
databases, the data transferred through this driver is not so secured.
• The ODBC bridge driver is needed to be installed in individual client
machines.
• Type-1 driver isn’t written in java, that’s why it isn’t a portable
driver.
Native-API driver – Type 2 driver ( Partially Java
driver)
• The Native API driver uses the client -side libraries of the
database. This driver converts JDBC method calls into
native calls of the database API. In order to interact with
different database, this driver needs their local API, that’s
why data transfer is much more secure as compared to
type-1 driver. This driver is not fully written in Java that is
why it is also called Partially Java driver.
Advantage
• Native-API driver gives better performance than JDBC-
ODBC bridge driver.
Native-API driver – Type 2 driver ( Partially Java
driver)
Disadvantages
•Driver needs to be installed separately in individual client machines
•The Vendor client library needs to be installed on client machine.
•Type-2 driver isn’t written in java, that’s why it isn’t a portable driver
•It is a database dependent driver.
Network Protocol driver – Type 3 driver (fully
Java driver)
Disadvantages
•Network support is required on client machine.
•Maintenance of Network Protocol driver becomes costly
because it requires database-specific coding to be done in the
middle tier.
Thin driver – Type 4 driver (fully Java driver)
Advantages
• Does not require any native library and Middleware
server, so no client-side or server-side installation.
• It is fully written in Java language, hence they are
portable drivers.
Thin driver – Type 4 driver (fully
Java driver)
Disadvantage
•If the database varies, then the driver will vary
because it is database dependent.
JDBC Architecture
• The JDBC API supports both two-tier and three-tier processing
models for database access.
• In the two-tier model, a Java applet or application talks directly to
the data source. This requires a JDBC driver that can communicate
with the particular data source being accessed. A user's
commands are delivered to the database or other data source,
and the results of those statements are sent back to the user. The
data source may be located on another machine to which the user
is connected via a network. This is referred to as a client/server
configuration, with the user's machine as the client, and the
machine housing the data source as the server. The network can
be an intranet, which, for example, connects employees within a
corporation, or it can be the Internet.
JDBC Architecture
JDBC Architecture
• In the three-tier model, commands are sent to a "middle
tier" of services, which then sends the commands to the
data source. The data source processes the commands
and sends the results back to the middle tier, which
then sends them to the user. MIS directors find the
three-tier model very attractive because the middle tier
makes it possible to maintain control over access and
the kinds of updates that can be made to corporate
data. Another advantage is that it simplifies the
deployment of applications. Finally, in many cases, the
three-tier architecture can provide performance
advantages.
JDBC Architecture
JDBC Architecture
• Until recently, the middle tier has often been written in languages such as
C or C++, which offer fast performance. However, with the introduction of
optimizing compilers that translate Java bytecode into efficient machine-
specific code and technologies such as Enterprise JavaBeans™, the Java
platform is fast becoming the standard platform for middle-tier
development. This is a big plus, making it possible to take advantage of
Java's robustness, multithreading, and security features.
• Driver interface
• Connection interface
• Statement interface
• PreparedStatement interface
• CallableStatement interface
• ResultSet interface
• ResultSetMetaData interface
• DatabaseMetaData interface
• RowSet interface
Classes of JDBC API
• DriverManager:
• Description: This class manages the set of database drivers.
It is responsible for establishing connections between Java
applications and the database.
• Usage:
• Registering Drivers: Use DriverManager.registerDriver(Driver
driver) to register a driver.
• Establishing Connections: The method getConnection(String
url, String user, String password) is used to create a
connection to the database.
Classes of JDBC API
• Driver Discovery:
• When a connection is requested, DriverManager
automatically locates suitable drivers without requiring
explicit loading of driver classes in most cases
(especially since JDBC 4.0). This simplifies the process
for developers.
• Login Timeout Management:
• The DriverManager allows setting and retrieving the
maximum time a driver will wait while attempting to
connect to a database through methods like
setLoginTimeout(int seconds) and getLoginTimeout().
DriverManager in a Multithreaded
Environment
• Using the DriverManager class in a multi-threaded
environment presents several limitations and
challenges:
• Connection Sharing Issues: Single Thread Access: Most
JDBC drivers do not allow multiple threads to use the
same `Connection` object simultaneously. If one thread
is executing a query while another tries to access the
same connection, it can lead to confusion and errors. As
a result, only one thread can perform operations on a
connection at any given time, potentially causing
performance bottlenecks.
DriverManager in a Multithreaded
Environment
• Performance Bottlenecks:
• Blocking Behavior: When multiple threads attempt to use the same
connection, threads that arrive later must wait for the ongoing
operation to complete. This can significantly degrade performance,
especially in applications with high concurrency demands, such as
web servers handling multiple requests simultaneously.
• Synchronization Overhead:
• Increased Complexity: Managing access to a shared connection
requires careful synchronization, which can complicate code and
introduce potential for deadlocks or race conditions if not handled
properly. This adds overhead and complexity to the application
logic.
DriverManager in a Multithreaded
Environment
• Connection Pooling Requirement:
• Need for Connection Pools: To mitigate the limitations of DriverManager
in multi-threaded environments, developers often need to implement
connection pooling. This involves creating a pool of connections that can
be shared among threads, allowing each thread to obtain a connection
without waiting for others. However, implementing and managing a
connection pool adds additional complexity and resource overhead.
• Thread Safety Concerns:
• Driver Implementation Variability: While some JDBC drivers are designed
to be thread-safe, others may not be. This inconsistency means that
developers cannot assume that all drivers will handle concurrent access
safely, necessitating caution when designing multi-threaded
applications.
Result Sets and Cursors
• The rows that satisfy the conditions of a query are called the result set.
The number of rows returned in a result set can be zero, one, or many. A
user can access the data in a result set one row at a time, and a cursor
provides the means to do that. A cursor can be thought of as a pointer
into a file that contains the rows of the result set, and that pointer has
the ability to keep track of which row is currently being accessed. A
cursor allows a user to process each row of a result set from top to
bottom and consequently may be used for iterative processing. Most
DBMSs create a cursor automatically when a result set is generated.
• Earlier JDBC API versions added new capabilities for a result set's cursor,
allowing it to move both forward and backward and also allowing it to
move to a specified row or to a row whose position is relative to another
row.
ResultSet Interface
• ResultSet Types:
• The type of a ResultSet object determines the level of its functionality
in two areas: the ways in which the cursor can be manipulated, and
how concurrent changes made to the underlying data source are
reflected by the ResultSet object.
• } catch (SQLException e) {
• JDBCTutorialUtilities.printSQLException(e);
• }
Types of Statements in JDBC
• The Statement interface in JDBC is used to create SQL
statements in Java and execute queries with the
database. There are different types of statements used
in JDBC:
• Statement
• PreparedStatement
• CallableStatement
Statement object
• A Statement object is used for general-purpose access to databases
and is useful for executing static SQL statements at runtime.
• Statement statement = connection.createStatement();
• The object used for executing a static SQL statement and returning
the results it produces.