org.pojava.persistence.util
Class SqlTool

java.lang.Object
  extended by org.pojava.persistence.util.SqlTool

public class SqlTool
extends Object

Utilities for performing SQL tasks. POJava keeps things simple by using prepared statements. This reduces your application's footprint on the cache your database uses to store unique SQL statements. It bypasses issues arising from variations in supported date formats between different databases. It also reduces your exposure to SQL injection vulnerabilities.

Author:
John Pile

Constructor Summary
SqlTool()
           
 
Method Summary
static
<POJO> TableMap<POJO>
autoGenerateTableMap(Class<POJO> javaClass, String tableName, String dsName)
          Generate TableMap from database.
static void close(Connection conn)
           
static void close(ResultSet rs, Statement stmt)
          Close a result set and prepared statement.
static void close(ResultSet rs, Statement stmt, Connection conn)
          Close spent JDBC resources.
static void close(Statement stmt)
          Close a statement or prepared statement.
static int executeQuery(PreparedSql query, Connection conn, ResultSetProcessor processor)
          Execute a select query, processing its ResultSet.
static int executeUpdate(PreparedSql query, Connection conn)
          Execute an insert/update/delete query returning row count.
static Properties fetchProperties(String propertyFile)
           
static int intQuery(Connection conn, PreparedSql query)
          Return an integer value (such as a count) from your query.
static void prepareBindings(PreparedStatement pstmt, List<org.pojava.lang.UncheckedBinding> list)
          Bind bindings to a prepared statement
static DataSource readDataSource(Properties props, String dsName)
          Read DataSource from a properties object
static void registerDataSource(Properties props, String dsName)
          Registers a DataSource into the InitialContext.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SqlTool

public SqlTool()
Method Detail

executeUpdate

public static int executeUpdate(PreparedSql query,
                                Connection conn)
                         throws SQLException
Execute an insert/update/delete query returning row count.

Parameters:
query -
conn -
Returns:
row count applicable to statement executed
Throws:
SQLException

executeQuery

public static int executeQuery(PreparedSql query,
                               Connection conn,
                               ResultSetProcessor processor)
                        throws SQLException
Execute a select query, processing its ResultSet.

Parameters:
query -
conn -
Returns:
ResultSetProcessor return value, typically number of rows processed
Throws:
SQLException

intQuery

public static final int intQuery(Connection conn,
                                 PreparedSql query)
Return an integer value (such as a count) from your query. This assumes your query yields a single integer result and returns the intValue of the first column of the ResultSet.

Parameters:
conn -
query -
Returns:
intValue of first column of first row of ResultSet

prepareBindings

public static void prepareBindings(PreparedStatement pstmt,
                                   List<org.pojava.lang.UncheckedBinding> list)
                            throws SQLException
Bind bindings to a prepared statement

Parameters:
pstmt - PreparedStatement needing bindings
list - Bindings to bind to the prepared statement
Throws:
SQLException

autoGenerateTableMap

public static <POJO> TableMap<POJO> autoGenerateTableMap(Class<POJO> javaClass,
                                                         String tableName,
                                                         String dsName)
Generate TableMap from database.

Parameters:
javaClass -
tableName -
dsName -
Returns:
TableMap retrieved from database MetaData

close

public static void close(Statement stmt)
Close a statement or prepared statement.

Parameters:
stmt - Statement or Prepared statement ready to be closed

close

public static void close(ResultSet rs,
                         Statement stmt,
                         Connection conn)
Close spent JDBC resources. Ensures call to close on all three even if one throws an exception during its closing.

Parameters:
rs - ResultSet ready to be closed
stmt - Statement ready to be closed
conn - Connection ready to be closed

close

public static void close(ResultSet rs,
                         Statement stmt)
Close a result set and prepared statement.

Parameters:
rs - ResultSet ready to be closed
stmt - Statement ready to be closed

close

public static void close(Connection conn)
                  throws SQLException
Throws:
SQLException

fetchProperties

public static Properties fetchProperties(String propertyFile)

registerDataSource

public static void registerDataSource(Properties props,
                                      String dsName)
                               throws NamingException,
                                      ClassNotFoundException
Registers a DataSource into the InitialContext.

Parameters:
props - A Properties object pre-populated with data
dsName - The name of the DataSource to register in JNDI.
Throws:
NamingException
ClassNotFoundException

readDataSource

public static DataSource readDataSource(Properties props,
                                        String dsName)
                                 throws NamingException,
                                        ClassNotFoundException
Read DataSource from a properties object

Parameters:
props - Properties collected from file
dsName - Name identifying DataSource
Returns:
DataSource
Throws:
NamingException
ClassNotFoundException


Copyright © 2011. All Rights Reserved.