JDBC-5 CallableStatement and Transactions
JDBC-5 CallableStatement and Transactions
and
Transactions
Objectives
At the end of this module, you will be able to:
A CallableStatement object is used for calling the stored procedure from JDBC program
A callable statement can contain variables that you supply each time you execute the call
When the stored procedure returns, computed values (if any) are retrieved through the
CallableStatement object
The CallableStatement Object
The way to access stored procedures using JDBC is through the CallableStatement class which is
inherited from the PreparedStatement class. CallableStatement is like PreparedStatement in that you
can specify parameters using the question mark (?) notation, but it contains no SQL statements.
Both functions and procedures take parameters represented by identifiers. A function executes some
procedural logic and it returns a value that can be any data type supported by the database. The
parameters supplied to the function do not change after the function is executed.
A procedure executes some procedural logic but does not return any value. However, some of the
parameters supplied to the procedure may have their values changed after the procedure is executed.
Note: Calling a stored procedure is the same whether the stored procedure was written originally in Java
or in any other language supported by the database, such as PL/SQL. Indeed, a stored procedure written
in Java appears to the programmer as a PL/SQL stored procedure.
How to Create a CallableStatement?
Register the driver and create the database connection
On connection object prepareCall() method is used to call the stored procedure
Create the callable statement, identifying variables with a question mark (?)
CallableStatement cstmt =
conn.prepareCall("{call " + ADDITEM + "(?,?,?)}");
cstmt.registerOutParameter(2,Types.INTEGER);
cStmt.registerOutParameter(3,Types.DOUBLE);
How to Create a CallableStatement?
Creating a Callable Statement
First you need an active connection to the database in order to obtain a CallableStatement
object.
Next, you create a CallableStatement object using the prepareCall() method of the
Connection class. This method typically takes a string as an argument. The syntax for the
string has two forms. The first form includes a result parameter and the second form does
not:
{? = call proc (…) } // A result is returned into a variable
{call proc (…) } // Does not return a result
In the example in the slide, the second form is used, where the stored procedure in question
is ADDITEM.
How to Create a CallableStatement?
Note that the parameters to the stored procedures are specified using the question mark
notation used earlier in PreparedStatement. You must register the data type of the
parameters using the registerOutParameter() method of CallableStatement if you expect a
return value, or if the procedure is going to modify a variable (also known as an OUT
variable). In the example in the slide, the second and third parameters are going to be
computed by the stored procedure, whereas the first parameter is an input (the input is
specified in the next slide). Parameters are referred to sequentially, by number. The first
parameter is 1.
To specify the data type of each OUT variable, you use parameter types from the Types
class. When the stored procedure successfully returns, the values can be retrieved from the
CallableStatement object.
How to execute a CallableStatement?
cstmt.setXXX(index, value);
2. Which of the following statements can be used to turn autocommit off (where conn is the reference
to the Connection object) ?
a) conn.setAutoCommit(“true”); Answers :
b) conn.setAutoCommit(true);
c) conn.setAutoCommit(“false”); 1:a
d) conn.setAutoCommit(false); 2:d
.
Summary of JDBC Classes
DriverManager
Connection DatabaseMetaData
Statement
ResultSet
ResultSetMetaData
Summary of JDBC Classes
DriverManager - DriverManager provides access to registered JDBC drivers.
DriverManager hands out connections to a specified data source through its
getConnection() method.
Connection - The Connection class is provided by the JDBC driver, as are all subsequent
classes mentioned. A Connection object represents a session with a database and is used to
create a Statement object, using Connection.createStatement().
Statement - The Statement class executes SQL statements. For example, queries can be
executed using the executeQuery() method and the results are wrapped up in a ResultSet
object.
Summary of JDBC Classes
ResultSet - JDBC returns the results of a query in a ResultSet object. A ResultSet object
maintains a cursor pointing to its current row of data. The next() method moves the cursor
to the next row. The ResultSet class has getXXX() methods to retrieve the columns in the
current row.
DatabaseMetaData and ResultSetMetaData - The DatabaseMetaData and
ResultSetMetaData classes return metadata about the database and ResultSet, respectively.
Call getMetaData() on the Connection object or the ResultSet object.
Summary
In this module, you were able to: