Professional Documents
Culture Documents
CHAPTER 1
JDBC
1.1. Basic JDBC Program Concepts
1.2. Design of JDBC
1.3. JDBC Driver
1.4. JDBC Architecture
1.5. Making the Connection in JDBC
1.6. Statement Objects
1.7. ResultSet
1.8. Executing SQL Commands
1.9. Executing Queries
Introduction
JDBC stands for Java Database Connectivity. It is set of Java API’s (application programming
interface) used for executing SQL statements. This API consists of a set of classes and interfaces
to enable programmers to write pure Java Database applications.
JDBC is a software layer that allows developers to write real client –server projects in Java. JDBC
does not concern itself with specific DBMS functions. JDBC API defines how an application
opens a connection, communicates with a database, executes SQL statements, and retrieves
query result.
JDBC was designed to be very compact, simple interface focusing on the execution of raw SQL
statements and retrieving the results. The goal of creating JDBC is to create an interface that
keeps simple tasks, while ensuring the more difficult and uncommon tasks are at least made
possible.
Advantages of using Java with JDBC:
Some of the advantages of using Java with JDBC are as follows:
Easy and economical.
Continued usage of already installed databases.
Development time is short.
Installation and version control simplified.
Design of JDBC
The JDBC consists of two layers. The top layer is the JDBC API. This API communicates with the JDBC
manager driver API, sending it the various SQL statements. The manager should (transparently to the
programmer) communicate with the various third-party drivers that actually connect to the database and
return the information from the query or perform the action specified by the query. The JDBC
specification will actually allow you to pass any string to the underlying driver. The driver can pass this
string to the database.
Java Application
JDBC API
JDBC Driver Manager
JDBC Driver API
Vendor
JDBC/ODBC
Supplied JDBC
Bridge
Driver
ODBC Driver
Database
Database
Common JDBC Components:
The JDBC API provides the following interfaces and classes:
DriverManag
er
SQLExceptio Driver
n
ResultSet Connection
Statement
Common JDBC Components:
1) DriverManager:
This class manages a list of database drivers. Matches connection requests from the java application
with the proper database driver using communication subprotocol.
2) Driver:
This interface handles the communications with the database server. You will interact directly with
Driver objects very rarely. Instead, one can use DriverManager objects, which manages objects of this
type. It also abstracts the details associated with working with Driver objects
3) Connection:
This interface with all methods for contacting a database. The connection object represents
communication context, i.e., all communication with database is through connection object only.
4) Statement:
One uses objects created from this interface to submit the SQL statements to the database. Some
derived interfaces accept parameters in addition to executing stored procedures.
5) ResultSet:
These objects hold data retrieved from a database after you execute an SQL query using Statement
objects. It acts as an iterator to allow you to move through its data.
6) SQLException:
This class handles any errors that occur in a database application.
Drivers
Why do we need a DB driver?
We can’t directly talk to the Data base server.
Driver converts JDBC calls to DB specific calls
For every database, we need a driver
Types of JDBC Drivers
• Type-1: JDBC-ODBC Bridge Driver
• Type-2: Native API Driver
• Type-3: Network Protocol Driver
• Type-4: Thin Driver
Type-1: JDBC-ODBC Bridge Driver
• JDBC-ODBC converts JDBC calls to ODBC calls and give it
to ODBC Driver.
• ODBC Driver interconverts the calls to native calls of the
DB API.
• Then Native calls will be understood by Vendor DB
Library and then Vendor DB Library will interconvert
calls to Vendor specific DB protocol.
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.
Type-2: Native API Driver
• Native API Driver converts JDBC calls to native calls of the DB API
Advantage:
• performance upgraded than JDBC-ODBC bridge driver.
Disadvantage:
• The Native driver needs to be installed on the each client machine.
• The Vendor client library needs to be installed on client machine.
Type-3: Network Protocol Driver
• This Driver does not directly converts JDBC call,it
uses a middleware between the calling program
and Database.
• Middleware converts JDBC calls to Vendor specific
DB Protocol
Advantage:
No client side library is required because of
application server that can perform many tasks like
auditing, load balancing, logging etc.
Disadvantages:
1.Network support is required on client machine.
2.Requires database-specific coding to be done in the
middle tier.
3.Maintenance of Network Protocol driver becomes
costly because it requires database-specific coding to
be done in the middle tier.
Type-4: Thin Driver
• It converts JDBC calls directly to vendor specific DB
protocol.
• It is fastest Driver because it has minimum layer of
communication.
Advantage:
Better performance than all other drivers.
No software is required at client side or server side.
Disadvantage:
Drivers depend on the Database.
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:
Java Application
JDBC API
3.Create statement:
The statement object is used to execute the query against the database. Connection interface acts as a factory
for statement object. A statement object can be any one of the Statement,
CallableStatement, and PreparedStatement types .To create a statement object we have to call createStatement()
method of Connection interface. Connection interface also provides the transaction management methods like
commit() and rollback() etc.
Syntax:
Statement stmt=conn.createStatement();
Methods of Statement Object:
Once user creates a Statement object, they can then use it to execute a SQL statement with one of its three
execute methods.
OUT A parameter whose value is supplied by the SQL statement it returns. You retrieve
values from the OUT parameters with the getXXX() methods.
INOUT A parameter that provides both input and output values. You bind variables with
the setXXX() methods and retrieve values with the getXXX() methods.
4. Execute statement:
Statement interface provides the methods to execute a statement.
To execute a statement for select query use below:
Syntax:
ResultSet rs = stmt.executeQuery(selectQuery);
Methods of ResultSet:
The methods of the ResultSet interface can be broken down into three categories
1) Navigational Methods:
These are used to move the cursor around.
2) Get Methods: Update Methods
These are used to view the data in the columns of the current row being pointed to by the cursor.
3) Update Methods:
These are used to update the data in the columns of the current row. The updates can then be updated in the
underlying database as well.
Navigating a Result Set:
There are several methods in the ResultSet interface that involve moving the cursor, including:
S.N. Methods & Description
1 public void beforeFirst() throws SQLException
Moves the cursor to just before the first row
2 public void afterLast() throws SQLException
Moves the cursor to just after the last row
3 public boolean first() throws SQLException
Moves the cursor to the first row
4 public void last() throws SQLException
Moves the cursor to the last row.
5 public boolean absolute(int row) throws SQLException
Moves the cursor to the specified row
Navigating a Result Set:
The where clause can use the comparison operators such as =,!=,<,>,<= and >=, as well as the BETWEEN and LIKE
operators
e.g.
select sid, name from Student where name LIKE ‘%Ash%’;
This sql statement selects sid and name column from the student table, where name column contain Ash;
Update data
used to update data
Syntax:
update table_nm set colu_nm=value,colu_nm=value…… where conditions;
e.g.
update Student set name=‘Anu’ where sid=1;
Delete data
used to delete data from tables
Syntax:
delete from table_nm where condition;
e.g.
delete from Student where sid=1;