4. Unit Outcomes
Choose JDBC or ODBC depending on the given application requirements
Explain the function of given tier of JDBC architecture for two tier/ three tier
models
Use the relevant type of JDBC driver for the specified environment
Elaborate steps with example to established connectivity with the specified
database
5. Introduction to JDBC
Full form of JDBC is Java Database Connectivity
JDBC is a java API, which is used for connecting various types of databases with
java programming language
API defines how different software components can interact with each other
API defines how how and which function calls and requests to make and which
data to be used when interacting with different software componets
API (Application Programming Interface) - represents classes and interfaces
required for communication with other software programs or components
7. Introduction to JDBC
With the help of JDBC we can save, delete, edit and fetch data and execute
various SQL queries on the database
JDBC is a java API, which is used for connecting various types of databases with
java programming language
JDBC API is used to get any kind of data stored in relational (tabular) database
JDBC API uses JDBC drivers to connect with the database
JDBC API works with java on various operating systems such as - windows, Mac OS,
UNIX
8. Introduction to JDBC
Database driver - A database driver is a computer program that implements a
protocol (ODBC or JDBC) for a database connection.
To connect with individual databases, JDBC requires drivers for each specific
database type.
Various types of java programs can use JDBC to use data from the database like :
Java Applications
Java Applets
Java Servlets
Java Server Pages
10. JDBC Driver is a software component that enables various types java programs to
interact with the database
JDBC Drivers
There are four types of JDBC drivers as shown below :
JDBC-ODBC
bridge driver
Native-API driver
(partially java
driver)
Network Protocol
driver (fully java
driver)
Thin driver (fully
java driver)
11. 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 (function) calls into the ODBC
function calls.
However, we do not use this driver as pure java driver are available
Database vendors - these are companies which develop, sell, install, or maintains
database products, ex :- Oracle, Microsoft, IBM, SAP, etc.
Library - collection of files, programs, routines, scripts, functions that are used to
develop software
12. JDBC-ODBC Bridge Driver
Advantages :
Any database for which an ODBC driver is installed can be used
Performance slows down as function calls go through JDBC bridge to
ODBC and then to the native database connectivity interface
Disadvantages :
The ODBC driver needs to be installed on the client machine.
Not suitable for applets because ODBC driver needs to be installed on client
machine
No support from JDK 1.8 (Java 8).
13. JDBC-ODBC Bridge Driver
JDBC API
Java Application
JDBC Driver
Database
Vendor (
company )
database
library
Client Machine
14. Native-API Driver
The Native API driver uses the client-side libraries of the database
The driver converts JDBC method calls into native (C/C++) calls of the database API
It is not written completely in java
If we change the database then we have to change the native API also as it is specific
to database
Advantages :
This driver works faster than JDBC-ODBC bridge driver
15. Native-API Driver
Disadvantages :
The vendor client library needs to be installed on the client machine
Not all databases have a client-side library
This driver is platform dependent
This driver supports all Java applications except applets
17. Network Protocol Driver
This driver is fully written in java language
The Network Protocol driver uses middleware (application server) that converts
JDBC calls directly or indirectly into the vendor-specific database protocol
The JDBC clients use standard network sockets to communicate with a
midddleware application server
The socket information is then translated by middleware (application server) in
function calls understood by the database server
This driver is platform-independant because the middleware adjusts with
different platforms
18. Network Protocol Driver
This driver follows three-tier communication method
It works with multiple databases
Client driver to middleware communication is database independent
Advantages :
No need for the database vendor library on the client
No need to make changes in the client for new database
The middleware (application sercer) can provide various services like - caching,
load balancing, auditing, logging, security and firewall access
If middleware supports, then one drive can work with any database
19. Network Protocol Driver
Disdvantages :
Client machine requires network support
Requires database-specific coding to be done in the middleware (applicatio
server) which also increases maintenance cost of network protocol driver
The middleware (application sercer) can provide various services like - caching,
load balancing, auditing, logging, security and firewall access
Additional layer of middleware (application server) may increase processing
time
20. Network Protocol Driver
JDBC API
Java Application
Native
Protocol Driver Database
Middleware
(application
server )
Client Machine
Server side
21. Thin Driver (Database-Protocol Driver )
This protocol is fully written in java language
This driver converts JDBC function calls directly into a vendor-specific database
protocol
It is platform independent
As this driver is vendor specific, JDBC client requires separate drivers for different
types of database
Advantages :
Better performance than all other drivers
Client applications directly connects with server, hence addition layer of
middleware (application server) is not require and also improves performance
Disdvantages :
Drivers depends on database, for different database requires different drivers
23. JDBC Architecture
There are two types of JDBC architectures or processing models for database
access as follow :
Two-tier architecture Three-tier architecture
25. In two tier architecture the java application directly communicate with the
database or data source
JDBC Two-Tier Architecture
JDBC driver communicates with database or data source
In this architecure user commands are sent to the database and the required data
is sent back to the user
The database or data source are located on a machine somewhere in the network to
which our client machine is connected
This type of architecture can also be called as client-server architecture
User's machine act as a client and database or data source act as a server
26. JDBC Three-Tier Architecture
Java Applet or HTML
web browser
Database
(DBMS)
Client Machine / Graphical
User Interface (GUI)
Server Machine (Business Logic)
Database Server
Application Server (Java)
JDBC
HTML , RMI or other calls
DBMS proprietary protocol
27. In three tier architecture one additional middle tier called as middleware or
application server is added
JDBC Three -Tier Architecture
In this architecture java application commands are sent to the middle tier which
forwards the command to the data source or database
The data source processes the commands and sents the required data to middle
tier which forwards back the data to java application or user
The data source processes the commands and sents the required data to middle
tier which forwards back the data to java application or user
28. In many cases, the three-tier architecture can provide performance advantages.
JDBC Three -Tier Architecture
Earlier C / C++ language was used to write middle tier software but now java is
also used for writing middle tier prgram
29. Introduction to ODBC
The full form of ODBC is Open Database Connectivity
ODBC is an API (Application Programming Interface) which is used for accessing
different databases
ODBC does not depend on database, platform or operating system
Once we develop application using ODBC, it can run on any platform, both on the
client and server side, with few changes to the data access code
ODBC API uses ODBC driver to communicate between application and DBMS
ODBC was developed by Microsoft and Simba technologies in early 1990s
ODBC works with all types of languages such as C, C++, java etc whereas JDBC
works with only java language
30. Important Components of ODBC Architecure
Application - An ODBC architecture is any software or program that
communicates with database using ODBC function calls
ODBC Driver Manager - ODBC driver manager takes function calls from application
and forwards it to the ODBC driver, the driver then takes data from the database
and gives it back to the driver manager
ODBC Driver - ODBC driver accepts function calls from driver manager and takes
the required data from connected data source or database and returns it back to
driver manager
Data Source - Data source contains data and environment associated with it such
as software required to access that database, operating system and network
platform
32. ODBC vs JDBC
JDBC
ODBC
ODBC works with all types of languages such
as C, C++, java etc.
JDBC works with only java language
ODBC is platform dependant - used with
windows OS only
JDBC is platform independant - used with any
operating system
ODBC is procedural JDBC is object oriented
Performance of ODBC is faster than JDBC Performance of JDBC is slower than ODBC
ODBC drivers are implemented in native
languages like C/C++.
JDBC drivers are implemented in Java.
ODBC was introduced by Microsoft JDBC was introduced by Sun MicroSystems
33. Which type of JDBC driver should be used in your software ?
If your are using only type of database in your software like Microsoft, Oracle,
Sybase, IBM etc then you should use type-4 (Thin Driver) JDBC driver
If your java application is using many databases at a same time then you should
use type-3 (Network Protocol Driver) JDBC driver
If type-3 (Network Protocol Driver) and type-4 (Thin Driver) are not available for
database you selected, then you should use type-2 (Native Driver) JDBC driver
Type-1 (JDBC-ODBC Bridge Driver) is used only for development and testing
purpose and not for deployment
35. DriverManager Class
The DriverManager class in java acts as an interface between user and drivers
It keeps track of how many and which drivers are available for connecting with
the database
It helps in 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(function) DriverManager.registerDriver().
36. Functions of DriverManager Class
public static void registerDriver(Driver driver) - this function registers the given
driver with DriverManager class
public static void deregisterDriver(Driver driver) - it de-registers the given driver
(drop the driver from the list) with DriverManager
public static Connection getConnection(String url) - it is used to establish the
connection with the given url
public static Connection getConnection(String url,String userName,String password) -
it is used to establish the connection with the given url, and given username and
password
37. Connection Interface
The Connection interface in java helps to establish connection with the database
A Connection is the session between java application and database
The Connection interface is a factory of Statement, PreparedStatement, and
DatabaseMetaData - it means, object of Connection interface can be used to get the
object of Statement and DatabaseMetaData
The Connection interface has many methods (functions) for transaction
management like - rollback( ), commit( ), close( ) etc.
38. Connection Interface
The Connection interface in java helps to establish connection with the database
A Connection is the session between java application and database
The Connection interface is a factory of Statement, PreparedStatement, and
DatabaseMetaData - it means, object of Connection interface can be used to get the
object of Statement and DatabaseMetaData
The Connection interface has many methods (functions) for transaction
management like - rollback( ), commit( ), close( ) etc.
39. Functions of Connection Interface
public Statement createStatement() - this function creates object of Statement
interface which can be use to execute SQL queries
public Statement createStatement(int resultSetType,int resultSetConcurrency) -
creates object of Statement interface that generates object of ResultSet interface
with given type and concurrency
public void setAutoCommit(boolean status) - this function sets the commit status,
by default it's value is true
public void commit() - this function permanently saves the changes done since
previous commit or rollback
40. Functions of Connection Interface
public void rollback( ) - drops or undo all changes done since the previous commit or
rollback
public void close( ) - closes the connection and releases all JDBC resources
immediately
41. Statement Interface
The Statement interface provides methods(functions) which are used to execute
SQL queries with the database
Object of Statement interface can work with only static query
The statement interface is a factory of ResultSet interface i.e. it provides factory
method to get the object of ResultSet interface
The statement interface is a factory of ResultSet interface i.e. it provides factory
method to get the object of ResultSet interface
42. Functions of Statement Interface
public ResultSet executeQuery(String sql) - it is used to execute SELECT SQL query
which retrieves (gives) data from the database. It returns the object of ResultSet
interface
public int executeUpdate(String sql) - this function is used to make modification in
your database like create, drop, insert, update, delete etc
public boolean execute(String sql) - this method is used for all type of SQL statement
(eg.Select, Insert, Update etc.), it is used to execute queries that may return
multiple results
public int[] executeBatch(): this function is used to execute batch (group) of
commands. It returns a array of integer type
43. ResultSet Interface
ResultSet interface represents the set of results of a database query
The object of ResultSet maintains a cursor pointing to a row of a table, initially,
cursor points to before the first row
The methods of the ResultSet interface can be divided into three categories -
Navigational methods - these methods are used to move the cursor around
Get methods: used to see the data in the columns of the current row being
pointed by the cursor
Update methods: used to update the data in the columns of the current row
By default, ResultSet object can be moved forward only and it is not updatable
44. ResultSet Interface
Types of Resultset - denoted by three constants as below
ResultSet.TYPE_FORWARD_ONLY - The cursor can only move forward in the result set
ResultSet.TYPE_SCROLL_INSENSITIVE - The cursor can scroll forward and backward and if
changed are made in database after result set was created then result remains
unchanged (i.e not sensitive)
ResultSet.TYPE_SCROLL_SENSITIVE - The cursor can scroll forward and backward and if
changed are made in database after result set was created then result remains also
changes (i.e sensitive)
Concurrency of ResultSet - means updatable or not
ResultSet.CONCUR_READ_ONLY - creates a read-only result set, this is the default value
ResultSet.CONCUR_UPDATABLE - creates an updateable result set
45. ResultSet Interface
Fig: - concept of ResultSet
Datbase
data data
data data
data data
ResultSet
object
cursor
46. Functions of ResultSet Interface
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 row) - is used to move the cursor to the specified row number in
the ResultSet object
public boolean relative(int row) - is used to move the cursor to the relative row number in
the ResultSet object, it may be positive or negative
47. Functions of ResultSet Interface
public int getInt(int columnIndex) - is used to return the data of specified column index of the
current row as int
public int getInt(String columnName) - is used to return the data of specified column name of
the current row as int
public String getString(int columnIndex) - is used to return the data of specified column index
of the current row as String
public String getString(String columnName) - is used to return the data of specified column
name of the current row as String
48. PreparedStatement Interface
The PreparedStatement interface is a subinterface (child) of Statement interface and it is
used to execute SQL queries with parameters
The PreparedStatement interface accepts input parameters at runtime
If we use PreparedStament interface then performance of application will be faster because
query is compiled only once
PreparedStament interface is used for the queries which are to be executed multiple times
We can used Preparedstatement interface for reading and writing binary data
Object of PreparedStatement interface is returned by prepareStatement() method of
Connection interface
Performance of PreparedStatement is better than Statement interface
49. Function of PreparedStatement Interface
public void setInt(int paramIndex, int value) - this function sets the integer value to the given
parameter index
public void setString(int paramIndex, String value) - sets the String value to the given
parameter index
public void setFloat(int paramIndex, float value) - sets the float value to the given parameter
index
public void setDouble(int paramIndex, double value) - sets the double value to the given
parameter index
public int executeUpdate() - executes the query. It is used for create, drop, insert, update,
delete etc
public ResultSet executeQuery() - executes the SELECT query. It returns an instance (object) of
ResultSet interface
50. JDBC Environment Setup
JDBC environment means what software components are require to develop a JDBC
application
Following components are require to develop a java database applications :
Install Java - JDK 5.0 or greater versions with JDBC packages ( java.sql and javax.sql
packages )
Install Database - open source databases like MySQL DB, postgreSQL or commercial
database like Oracle DB
Install database drivers - JDBC-ODBC bridge drivers, thin driver etc.
51. Steps for creating JDBC application
Open connection with database - create Connection object using
DriverManager.getConnection( ) method
Extract data from the result set - use functions of ResultSet interface to get data
from result set
Import the packages for JDBC classes - java.sql.* or javax.sql.*
Register the JDBC driver - for opening communication channel with database
Execute Query - create object of Statement interface
Clean up the environment - close all database connections and resources