Java Database Connectivity: Object Oriented and Event Driven Programming
Java Database Connectivity: Object Oriented and Event Driven Programming
1
Objectives
• In this lecture, we will
– JDBC Concepts
– Steps during execution of JDBC Application
– JDBC Component Interaction
– Steps Involved in Basic JDBC Operations
– Two-Tier Database Access Model
– JDBC Driver Types
– Driver Manager,Connection Object,DSN-less Connection
– Statement Object
– ResultSet Object
– Accessing Data in a ResultSet
– Getting MetaData for a ResultSet
Java Database Connectivity (JDBC)
• JDBC library provides the means for executing SQL statements to access
and operate on a relational database
Java Application
JDBC
• Driver Manager
– Loads database drivers, and manages the connection between the
application and the driver
• Driver
– Translates API calls into operations for a specific data source
• Connection
– A session between an application and a database
JDBC Concepts (contd.)
• Statement
– An SQL Statement to perform a query or update operation
• Metadata
– Information about returned data, the database and the driver
• ResultSet
– Logical set of columns and rows returned by executing an SQL
statement (resulting tuples)
Steps during execution
• The following steps are executed when running a JDBC
application
– Import the necessary classes
– Load the JDBC driver
– Identify the database source
– Allocate a “connection” object (create)
– Allocate a “Statement” object (create)
– Execute a query using the “Statement” object
– Retrieve data from the returned “ResultSet” object
– Close the “ResultSet” object
– Close the “Statement” object
– Close the “Connection” object
JDBC Component Interaction
SQL
Driver
Establish
Link to DB Result
(tuples)
Database
Steps Involved in Basic JDBC
OperationsDriver Manager
SQL Result
Command Set
Database
JDBC Driver Types
• JDBC-ODBC Bridge, plus ODBC driver (Type 1)
– Simplest
– JDBC methods -> Translate JDBC methods to ODBC methods -> ODBC
to native methods -> Native methods API
SQL Result
• ODBC driver must be configured for Command Set
the bridge to work
ODBC Driver
• Only solution if no JDBC driver
available for the DBMS Proprietary
Protocol
Database
Type 2: Native-API, Partly Java Driver
• Native-API driver converts JDBC
Application Space
commands into DBMS-specific
native calls
Java Application
Proprietary
Protocol
Database
Type 3: JDBC-Net, Pure Java Driver
• Translates JDBC calls into a database- Application Space
independent network protocol and
sent to a middleware server. Java Application
Proprietary
Protocol
Database
Type 4: Native-Protocol, Pure
Java Driver
• Pure Java drivers that communicate Application Space
directly with the vendor’s database
Java Application
• JDBC commands converted to
database engine’s native protocol
directly
Type 4 JDBC Driver
• Advantage: no additional translation
or middleware layer
SQL Command Result Set
• Improves performance Using Proprietary Using Proprietary
Protocol Protocol
Database
Driver Manager
• The DriverManager class is responsible for establishing connections to
the data sources, accessed through the JDBC drivers
• Should be exactly the same as shown below except the name of the
database and its path
String source =
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=movies_vj.mdb";
con = DriverManager.getConnection(source);
Q&A