When a database is updated with each Statement object, by default, it will automatically commit the changes (permanently) to the database immediately. In general, to complete a transaction, it is required that one or more statements be executed. Thus, there are situations where a commit statement is needed after execution of more than one statement. In Java, the Connection object performs transaction control. The Connection object provides a method setAutoCommit (boolean value) to specify whether a transaction (or a set of transactions) should commit automatically or manually. By default, it is set to auto-commit mode. The syntax for this is the following:
dbcon.setAutoCommit (true);
This auto-commit feature can be turned off with the statement:
dbcon.setAutoCommit (false);
To commit a transaction, when the auto-commit feature is off, the statement to be used is the following:
dbcon.commit ();
If a transaction is to be rolled back at any point, this can be specified as follows:
dbcon.rollback ();
In general, the rollback statement is used in exception handlers to recover from errors that may have occurred during execution of a transaction.
Table Methods of the class SQLException.
Name of method | Description |
public int getErrorCode () | Returns the integer value containing the vendor-specific code. |
public SQLException getNextException () | Returns the next exception in the exception chain. |
public String getSQLState () | Returns the string containing the SQL state. |
The version JDBC 3.0 API allows the programmer to create save points to be specified during a transaction, after which a rollback may be issued: this transaction can rollback the changes that have taken place after the save point.