Java Reference
In-Depth Information
Example 17−4: LookupAPI.java (continued)
ResultSet r2 = t.getResultSet();
while(r2.next()) {
String m = r2.getString(1);
int isField = r2.getInt(2);
System.out.println(" "+m+((isField == 1)?"":"()"));
}
// End the class listing
System.out.println("}");
}
// Now go look for a package that matches the specified name
s.executeQuery("SELECT name FROM package " +
"WHERE name='" + target + "'" +
" OR name LIKE '%." + target + ".%' " +
" OR name LIKE '" + target + ".%' " +
" OR name LIKE '%." + target + "'");
// Loop through any matching packages
r = s.getResultSet();
while(r.next()) {
// Display the name of the package
String p = r.getString(1);
System.out.println("Package " + p + ": ");
// Get a list of all classes and interfaces in the package
t.executeQuery("SELECT class.name FROM package, class " +
"WHERE package.name='" + p + "' " +
" AND class.packageId=package.id " +
"ORDER BY class.name");
// Loop through the list and print them out.
ResultSet r2 = t.getResultSet();
while(r2.next()) System.out.println(" " + r2.getString(1));
}
// Finally, close both Statement objects
s.close(); t.close();
}
}
Atomic Transactions
By default, a newly created database Connection object is in auto-commit mode.
That means that each update to the database is treated as a separate transaction
and is automatically committed to the database. Sometimes, however, you want to
group several updates into a single atomic transaction, with the property that
either all the updates complete successfully or no updates occur at all. With a
database system (and JDBC driver) that supports it, you can take the Connection
out of auto-commit mode and explicitly call commit() to commit a batch of trans-
actions or call rollback() to abort a batch of transactions, undoing the ones that
have already been done.
Example 17-5 displays a class that uses atomic transactions to ensure database
consistency. It is an implementation of the RemoteBank interface that was devel-
oped in Chapter 16, Remote Method Invocation . As you may recall, the Remote-
BankServer class developed in that chapter didn't provide any form of persistent
Search WWH ::




Custom Search