Java Reference
In-Depth Information
Note that the commit() method is called only if all transaction statements were
processed successfully. If any of them fail, the successFlag is equal to false ,
which would cause the rollback() method to be called instead. In the solution to
this recipe, the second call to insertRecord() attempts to insert a NULL value into
the RECIPE.ID column, which is not allowed. Therefore, that insert fails and
everything, including the previous insert, gets rolled back.
13-8. Creating a Scrollable ResultSet
Problem
You have queried the database and obtained some results. You want to store those res-
ults in an object that will allow you to traverse forward and backward through the res-
ults, updating values as needed.
Solution
Create a scrollable ResultSet object and then you will have the ability to read the
next, first record, last, and previous record. Using a scrollable ResultSet al lows
the results of a query to be fetched in any direction so that the data can be retrieved as
needed. The following example method demonstrates the creation of a scrollable Res-
ultSet object:
private static void queryDbRecipes(){
String sql = "SELECT ID, RECIPE_NUMBER, RECIPE_NAME,
DESCRIPTION " +
"FROM RECIPES";
try(PreparedStatement pstmt
=conn.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = pstmt.executeQuery()) {
rs.first();
System.out.println(rs.getString(2) + ": "
Search WWH ::




Custom Search