com.googlecode.jdbw.util
Class SQLWorker

java.lang.Object
  extended by com.googlecode.jdbw.util.SQLWorker

public class SQLWorker
extends Object

This utility class can be very helpful when sending simple queries to the database and you don't want to get too involved in the details. It will help you to send queries and get result back in simple and familiar formats, as well as grabbing only limited parts of the result set. You normally create an SQLWorker on top of an auto-executor, but you can also use a normal transaction.

Author:
Martin Berglund

Constructor Summary
SQLWorker(SQLExecutor executor)
          Creates a new SQLWorker with a specified underlying SQLExecutor to use for the actual database communication.
 
Method Summary
 Object insert(String SQL, Object... parameters)
          Sends a query to the database server and returns any auto-generated value the database is telling us about.
 List<Object> leftColumn(String SQL, Object... parameters)
          Sends a query to the database and returns the first column of every row
 List<String> leftColumnAsString(String SQL, Object... parameters)
          Sends a query to the database and returns the first column of every row as a list of Strings
 List<Object[]> query(String SQL, Object... parameters)
          Sends a query to the database and returns the whole ResultSet as a list of Object arrays.
 List<String[]> queryAsStrings(String string, Object... parameters)
          Sends a query to the database and returns the whole ResultSet as a list of String arrays.
 Object[] top(String SQL, Object... parameters)
          Sends a query to the database and returns the first row of the result set as an Object array.
 String[] topAsString(String string, Object... parameters)
          Sends a query to the database and returns the first row of the result set as a String array.
 Object topLeftValue(String SQL, Object... parameters)
          Sends a query to the database and returns the first column of the first row
 BigInteger topLeftValueAsBigInteger(String SQL, Object... parameters)
          Sends a query to the database and returns the first column of the first row, as a BigInteger
 Integer topLeftValueAsInt(String SQL, Object... parameters)
          Sends a query to the database and returns the first column of the first row, as an Integer
 Long topLeftValueAsLong(String SQL, Object... parameters)
          Sends a query to the database and returns the first column of the first row, as a Long
 String topLeftValueAsString(String SQL, Object... parameters)
          Sends a query to the database and returns the first column of the first row, as a String
 void write(String SQL, Object... parameters)
          Sends a query to the database server and expects nothing to return.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SQLWorker

public SQLWorker(SQLExecutor executor)
Creates a new SQLWorker with a specified underlying SQLExecutor to use for the actual database communication.

Parameters:
executor - SQLExecutor send the queries to
Method Detail

query

public List<Object[]> query(String SQL,
                            Object... parameters)
                     throws SQLException
Sends a query to the database and returns the whole ResultSet as a list of Object arrays.

Parameters:
SQL - SQL to send to the database server
parameters - Parameters to substitute ?:s for in the SQL string
Returns:
The entire result set, converted into a list of Object arrays, where each array in the list is one row in the result set
Throws:
SQLException - If any database error occurred

queryAsStrings

public List<String[]> queryAsStrings(String string,
                                     Object... parameters)
                              throws SQLException
Sends a query to the database and returns the whole ResultSet as a list of String arrays.

Parameters:
SQL - SQL to send to the database server
parameters - Parameters to substitute ?:s for in the SQL string
Returns:
The entire result set, converted into a list of String arrays, where each array in the list is one row in the result set and each element in the String arrays are the .toString() call on the underlying result set object.
Throws:
SQLException - If any database error occurred

write

public void write(String SQL,
                  Object... parameters)
           throws SQLException
Sends a query to the database server and expects nothing to return.

Parameters:
SQL - SQL to send to the database server
parameters - Parameters to substitute ?:s for in the SQL string
Throws:
SQLException - If any database error occurred

insert

public Object insert(String SQL,
                     Object... parameters)
              throws SQLException
Sends a query to the database server and returns any auto-generated value the database is telling us about. Please note that not all database servers supports this feature.

Parameters:
SQL - SQL to send to the database server
parameters - Parameters to substitute ?:s for in the SQL string
Returns:
An auto-generated value created by the database as a result of this query, or null
Throws:
SQLException - If any database error occurred

top

public Object[] top(String SQL,
                    Object... parameters)
             throws SQLException
Sends a query to the database and returns the first row of the result set as an Object array.

Parameters:
SQL - SQL to send to the database server
parameters - Parameters to substitute ?:s for in the SQL string
Returns:
The first row of the result set, converted into an Object array
Throws:
SQLException - If any database error occurred

topAsString

public String[] topAsString(String string,
                            Object... parameters)
                     throws SQLException
Sends a query to the database and returns the first row of the result set as a String array.

Parameters:
SQL - SQL to send to the database server
parameters - Parameters to substitute ?:s for in the SQL string
Returns:
The first row of the result set, converted into an String array where each element in the array is the .toString() result of each object in the result set
Throws:
SQLException - If any database error occurred

leftColumn

public List<Object> leftColumn(String SQL,
                               Object... parameters)
                        throws SQLException
Sends a query to the database and returns the first column of every row

Parameters:
SQL - SQL to send to the database server
parameters - Parameters to substitute ?:s for in the SQL string
Returns:
The first column of every row as a list, where the order is maintained from the result set
Throws:
SQLException - If any database error occurred

leftColumnAsString

public List<String> leftColumnAsString(String SQL,
                                       Object... parameters)
                                throws SQLException
Sends a query to the database and returns the first column of every row as a list of Strings

Parameters:
SQL - SQL to send to the database server
parameters - Parameters to substitute ?:s for in the SQL string
Returns:
The first column of every row as a list of Strings, where the order of the list is maintained from the result set and each element in the list is the result of calling .toString() on the object in the result set.
Throws:
SQLException - If any database error occurred

topLeftValue

public Object topLeftValue(String SQL,
                           Object... parameters)
                    throws SQLException
Sends a query to the database and returns the first column of the first row

Parameters:
SQL - SQL to send to the database server
parameters - Parameters to substitute ?:s for in the SQL string
Returns:
The first column of the first row in the result set
Throws:
SQLException - If any database error occurred

topLeftValueAsString

public String topLeftValueAsString(String SQL,
                                   Object... parameters)
                            throws SQLException
Sends a query to the database and returns the first column of the first row, as a String

Parameters:
SQL - SQL to send to the database server
parameters - Parameters to substitute ?:s for in the SQL string
Returns:
The first column of the first row in the result set, as a String
Throws:
SQLException - If any database error occurred

topLeftValueAsInt

public Integer topLeftValueAsInt(String SQL,
                                 Object... parameters)
                          throws SQLException
Sends a query to the database and returns the first column of the first row, as an Integer

Parameters:
SQL - SQL to send to the database server
parameters - Parameters to substitute ?:s for in the SQL string
Returns:
The first column of the first row in the result set, as an Integer
Throws:
SQLException - If any database error occurred

topLeftValueAsLong

public Long topLeftValueAsLong(String SQL,
                               Object... parameters)
                        throws SQLException
Sends a query to the database and returns the first column of the first row, as a Long

Parameters:
SQL - SQL to send to the database server
parameters - Parameters to substitute ?:s for in the SQL string
Returns:
The first column of the first row in the result set, as a Long
Throws:
SQLException - If any database error occurred

topLeftValueAsBigInteger

public BigInteger topLeftValueAsBigInteger(String SQL,
                                           Object... parameters)
                                    throws SQLException
Sends a query to the database and returns the first column of the first row, as a BigInteger

Parameters:
SQL - SQL to send to the database server
parameters - Parameters to substitute ?:s for in the SQL string
Returns:
The first column of the first row in the result set, as a BigInteger
Throws:
SQLException - If any database error occurred


Copyright © 2012. All Rights Reserved.