com.jaeksoft.pojodbc
Class Query

java.lang.Object
  extended by com.jaeksoft.pojodbc.Query

public class Query
extends java.lang.Object

Represents an SQL query. In JDBC view, a query contains at least a PreparedStatement. It can also contains a ResultSet. Statement and ResultSet are automatically closed when Query or Transaction is closed.

The most important behavior is to return a list of Pojo instead of a ResultSet.

The example show how to use it.

 Transaction transaction = null;
 try {
   // Obtain a new transaction from the ConnectionManager
   transaction = connectionManager.getNewTransaction(false,
                             javax.sql.Connection.TRANSACTION_READ_COMMITTED);
   // Start a new Query
   Query query = transaction.prepare("SELECT * FROM MyTable WHERE status=?");
   query.getStatement().setString(1, "open");
   query.setFirstResult(0);
   query.setMaxResults(10);
   
   // Get the result
   List<MyPojo> myPojoList = query.getResultList(MyPojo.class));
   
   // do everything you need
   
 } finally {
   // Release the transaction
   if (transaction != null)
     transaction.close();
 }
 

Author:
Emmanuel Keller

Nested Class Summary
private  class Query.MethodColumnIndex
           
 
Field Summary
private  int firstResult
           
protected static java.util.logging.Logger logger
           
private  int maxResults
           
private  java.util.List<?> resultList
           
private  java.sql.ResultSet resultSet
           
private  java.sql.PreparedStatement statement
           
 
Constructor Summary
protected Query(java.sql.PreparedStatement statement)
           
 
Method Summary
private  void checkResultSet()
           
protected  void closeAll()
          Close all component of that query (ResultSet and Statement)
private  java.util.List<?> createBeanList(java.lang.Class<?> beanClass)
           
private  java.util.List<Row> createRowList()
           
private static java.util.List<Row> createRowList(java.sql.ResultSet resultSet, int limit)
           
 java.util.List<Row> getGeneratedKeys()
          Returns the generated keys after an insert statement
 int getResultCount()
          FirstResult and MaxResults parameters are ignored.
 java.util.List<Row> getResultList()
           
 java.util.List<?> getResultList(java.lang.Class<?> beanClass)
          Returns the list of POJO.
 java.sql.ResultSet getResultSet()
          Get the ResultSet used by that Query.
 java.sql.PreparedStatement getStatement()
          Get the PreparedStatement used by that Query
private  void moveToFirstResult()
           
 void reUse()
          Release the last ResultSet (if any) and the last ResultList.
 void setFirstResult(int firstResult)
          Set the first position of the result
 void setMaxResults(int maxResults)
          Set the maximum number of rows
 int update()
          Do a PreparedStatement.executeUpdate().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

resultSet

private java.sql.ResultSet resultSet

resultList

private java.util.List<?> resultList

statement

private java.sql.PreparedStatement statement

firstResult

private int firstResult

maxResults

private int maxResults

logger

protected static java.util.logging.Logger logger
Constructor Detail

Query

protected Query(java.sql.PreparedStatement statement)
Method Detail

setFirstResult

public void setFirstResult(int firstResult)
Set the first position of the result

Parameters:
firstResult -

setMaxResults

public void setMaxResults(int maxResults)
Set the maximum number of rows

Parameters:
maxResults -

closeAll

protected void closeAll()
Close all component of that query (ResultSet and Statement)


createBeanList

private java.util.List<?> createBeanList(java.lang.Class<?> beanClass)
                                  throws java.lang.Exception
Throws:
java.lang.Exception

moveToFirstResult

private void moveToFirstResult()
                        throws java.sql.SQLException
Throws:
java.sql.SQLException

createRowList

private static java.util.List<Row> createRowList(java.sql.ResultSet resultSet,
                                                 int limit)
                                          throws java.sql.SQLException
Throws:
java.sql.SQLException

createRowList

private java.util.List<Row> createRowList()
                                   throws java.sql.SQLException
Throws:
java.sql.SQLException

getStatement

public java.sql.PreparedStatement getStatement()
Get the PreparedStatement used by that Query

Returns:
a PreparedStatement

reUse

public void reUse()
Release the last ResultSet (if any) and the last ResultList.


checkResultSet

private void checkResultSet()
                     throws java.sql.SQLException
Throws:
java.sql.SQLException

getResultList

public java.util.List<?> getResultList(java.lang.Class<?> beanClass)
                                throws java.lang.Exception
Returns the list of POJO. The list is cached. Every subsequent call returns the same list.

Parameters:
beanClass - The class name of POJO returned in the list
Returns:
a list of POJO
Throws:
java.sql.SQLException
java.lang.Exception

getResultList

public java.util.List<Row> getResultList()
                                  throws java.sql.SQLException
Returns:
a list of Row object.
Throws:
java.sql.SQLException

update

public int update()
           throws java.sql.SQLException
Do a PreparedStatement.executeUpdate(). A convenient way to execute an INSERT/UPDATE/DELETE SQL statement.

Returns:
a row count
Throws:
java.sql.SQLException

getGeneratedKeys

public java.util.List<Row> getGeneratedKeys()
                                     throws java.sql.SQLException
Returns the generated keys after an insert statement

Returns:
the list of generated keys
Throws:
java.sql.SQLException

getResultCount

public int getResultCount()
                   throws java.sql.SQLException
FirstResult and MaxResults parameters are ignored.

Returns:
the number of row found for a select
Throws:
java.sql.SQLException

getResultSet

public java.sql.ResultSet getResultSet()
                                throws java.sql.SQLException
Get the ResultSet used by that Query.

Returns:
the JDBC ResultSet
Throws:
java.sql.SQLException