javautils.jdbc
Class JDBC

java.lang.Object
  |
  +--javautils.collections.Algs
        |
        +--javautils.jdbc.JDBC

public class JDBC
extends Algs

Static utility methods for dealing with JDBC.


Field Summary
 
Fields inherited from class javautils.collections.Algs
EMPTY_ARRAY, EMPTY_LIST, EMPTY_MAP, EMPTY_SEQUENCE, EMPTY_SET
 
Constructor Summary
JDBC()
           
 
Method Summary
static void executeTransaction(java.sql.Connection connection, Function nullaryProc)
          Performs a transaction.
static java.lang.Object fold(java.lang.Object accumulator, java.sql.ResultSet rs, Function fun)
          Folds a function into the row sequence of the result set.
static void forEach(java.sql.ResultSet rs, Function proc)
          Executes a procedure for each row of the result set.
 
Methods inherited from class javautils.collections.Algs
addAll, allSuperInterfaces, asArray, asArray, asComparator, asUnmodifiableList, collect, collectMap, collectSet, collectUnmodifiable, concat, concat, concat, copyOf, copyOf, copyOf, copyOf, copyOf, copyOf, copyOf, copyOf, copyOf, copyOfArray, drop, ensureLength, exists, exists, exists, filter, filter, filter, find, find, find, flatten, flatten, flatten, fold, fold, fold, foldRight, foldRight, foldRight, forAll, forAll, forAll, forEach, forEach, forEach, forEach, forEach, forEach, forEachInProduct, forEachInProduct, forEachInProduct, forEachInProduct, genAddAll, genConcat, genConcat, genForEach, genForEach, getOrIfNull, integersInRange, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iterator, iteratorOverArray, map, map, map, map, map, map, mapMorphism, mapMorphismTo, mapTransform, max, max, max, max, max, max, max, min, min, min, min, min, min, min, newMap, newShapedArray, newUnmodifiableList, putAll, putAll, reverseIterator, reverseIterator, select, select, select, sign, singletonIterator, sort, sort, sorted, sorted, take, transform, transform, transform
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JDBC

public JDBC()
Method Detail

executeTransaction

public static void executeTransaction(java.sql.Connection connection,
                                      Function nullaryProc)

Performs a transaction. The intention of this method is to simplify the implementation of transactions.

This method:

  1. Checks that auto commit (of the given connection) is on. If auto commit is off, throws a runtime exception, because it may indicate a nested transaction.
  2. Sets auto commit off.
  3. Calls the given procedure. The intention is that the procedure performs the necessary SQL operations on the (same) connection. If the transaction fails, the procedure should throw an exception.
  4. If the procedure returns normally, the transaction is committed. Otherwise the procedure has thrown an exception. In that case, the transaction is rolled back and the exception is rethrown.
  5. Finally auto commit is set on.

Note that this method does not automatically roll back any mutations made to the application state. The mutations made to the database through the connection are rolled back in case the transaction procedure throws an exception.


fold

public static java.lang.Object fold(java.lang.Object accumulator,
                                    java.sql.ResultSet rs,
                                    Function fun)

Folds a function into the row sequence of the result set.

The accumulator is passed as the first argument to the function. The return value from the function then replaces the accumulator. Finally the value of the accumulator is returned from fold.

The columns of the result set are matched to the arguments of the function. The second argument of the function gets the value of the first column, the third arguments gets the second column, etc... The type of the argument determines the getter used to get the value of column from the result set. Basically, if the type of an argument is a primitive type, then a getter with the same primitive type will be used. Otherwise either the getObject(int i) or the getter of the specific non-primitive type will be used.

Suppose that you have a table that maps names (strings) to ages (integers). To make a simple list of the table, you could use code like this:

 String result =
   fold(new StringBuffer("<dl>\n"),
        stmt.executeQuery(" SELECT name " +
                          "      , age " +
                          " FROM players " +
                          " ORDER BY name DESC "),
        new Function() {
          public StringBuffer with(StringBuffer accumulator,
                                   String name,
                                   int age) {
            accumulator.append("  <dt>" + name + "</dt>\n" +
                               "  <dd>" + age + "</dd>\n");
            return accumulator;
          }}) + "</dl>\n";
 


forEach

public static void forEach(java.sql.ResultSet rs,
                           Function proc)

Executes a procedure for each row of the result set.

The columns of the result set are matched to the arguments of the procedure. The first argument of the procedure gets the value of the first column, the second arguments gets the second column, etc... The type of the argument determines the getter used to get the value of column from the result set. Basically, if the type of an argument is a primitive type, then a getter with the same primitive type will be used. Otherwise either the getObject(int i) or the getter of the specific non-primitive type will be used.

Suppose that you have a table that maps names (strings) to ages (integers). To make a simple list of the table, you could use code like this:

 final StringBuffer result = new StringBuffer("<dl>\n");

 forEach(stmt.executeQuery(" SELECT name, age " +
                           " FROM players " +
                           " ORDER BY name DESC "),
         new Function() {
           public void with(String name, int age) {
             result.append("  <dt>" + name + "</dt>\n" +
                           "  <dd>" + age + "</dd>\n");
           }});

 result.append("</dl>\n");