JDBC Session
JDBC Session
Classes)
Yatin Niimesh
GS, Associate Consultant
October 2013
What is JDBC
JDBC Architecture
Components of JDBC
DriverManager
Driver
Connection
Statement
Resultsets
SQL Exception
Import the packages . Requires that you include the packages containing the JDBC classes
needed for database programming. Most often, using import java.sql.* will suffice.
Register the JDBC driver . Requires that you initialize a driver so you can open a
communications channel with the database.
Open a connection . Requires using the DriverManager.getConnection() method to create a
Connection object, which represents a physical connection with the database.
Execute a query . Requires using an object of type Statement for building and submitting
an SQL statement to the database.
Extract data from result set . Requires that you use the
appropriate ResultSet.getXXX() method to retrieve the data from the result set.
Clean up the environment . Requires explicitly closing all database resources versus
relying on the JVM's garbage collection.
JDBC Drivers
Type 1, Type 2, Type 3, Type 4
import java.sql.* ;
Register JDBC Driver: Two Approaches
Approach (I) - Class.forName():
10
JDBC Statements
The JDBC Statement, CallableStatement and PreparedStatement interfaces define
the methods and properties that enable you to send SQL or PL/SQL commands and
receive data from your database.
11
JDBC Statements
Statement object is used to execute a SQL statement with one of its three execute
methods.
boolean execute(String SQL)
int executeUpdate(String SQL)
12
JDBC Statements
The CallableStatement Objects:
It creates the CallableStatement object which would be used to execute a
call to a database stored procedure.
13
JDBC Statements
14
The SQL statements that read data from a database query return the data
in a result set.
The SELECT statement is the standard way to select rows from a database
and view them in a result set.
Thejava.sql.ResultSet interface represents the result set of a database
query.
A ResultSet object maintains a cursor that points to the current row in the
result set. The term "result set" refers to the row and column data
contained in a ResultSet object.
15
ResultSet interface
Navigational methods: used to move the cursor around.
Get methods: used to view the data in the columns of the current row
being pointed to by the cursor.
Update methods: used to update the data in the columns of the current
row. The updates can then be updated in the underlying database as well.
JDBC provides following connection methods to create statements with
desired ResultSet:
createStatement(int RSType, int RSConcurrency);
prepareStatement(String SQL, int RSType, int RSConcurrency);
prepareCall(String sql, int RSType, int RSConcurrency);
16
17
18
19
20
21
JDBC - Transactions
Transactions enable you to control if, and when, changes are applied to the
database.
22
JDBC - Transactions
Commit & Rollback
23
JDBC - Transactions
Using Savepoints:
setSavepoint(String savepointName): defines a new savepoint. It also returns a Savepoint object.
releaseSavepoint(Savepoint savepointName): deletes a savepoint. Notice that it requires a Savepoint object as a
parameter. This object is usually a savepoint generated by the setSavepoint() method.
There is one rollback ( String savepointName ) method which rolls back work to the specified savepoint.
24
25