Using savepoints in JDBC
JDBC drivers available in DB2 provide the setSavepoint()
method to create a savepoint. In this recipe, we will use savepoints in a JDBC application.
Getting ready…
We will use the same table
TEST_SVPT
created in the previous example. The following command can be used to create it:
CREATE TABLE TEST_SVPT (
EMPNO INTEGER,
EMPNAME VARCHAR(20),
MGRNO INTEGER);
The methods used will be:
Connection.setSavepoint(String name)
Connection.rollback(Savepoint savepointName)
Connection.releaseSavepoint(Savepoint savepointName)
How to do it…
In this example, we will create a transaction having multiple savepoints and we will see how to use them.
1. Creating a connection object: A database connection is required before we execute any SQL statement against the database. To create a connection object, we first need to load the database driver and then create a connection object.
Use the
Class.forName()
method to load the database driver:Class.forName("COM.ibm.db2.jdbc.app.DB2Driver...