📄 basedatatagiteratorresultset.java
字号:
package jsp.tags.dapact.tags.data;
import java.sql.*;
import jsp.tags.dapact.conf.UserClassFactory;
import jsp.tags.dapact.tags.ChainedJspException;
/**
* Title: Data Aware Processing And Control Tags
* Description: Tag library for the processing and controlling the input and output of data.
* Copyright: LGPL (http://www.gnu.org/copyleft/lesser.html)
* Compile Date: @compile_date@
* @author Allen M Servedio
* @amp_sign@version @VERSION@
*/
/**
* Data tag iterator that iterates over a result set, closing it when done.
*
* @todo implement a factory version of this...
*/
public abstract class BaseDataTagIteratorResultSet extends BaseDataTagIterator
{
/**
* Default constructor...
*/
public BaseDataTagIteratorResultSet()
{
}
/**
* Called when the starting tag is found - if an exception occurrs while processing
* the super class, this will call the close() method.
*
* @return EVAL_BODY_TAG so that it processes what ever is in the body or SKIP_BODY
* if no prepared statement is supplied or it is returns an empty result set.
*/
public int doStartTag() throws ChainedJspException
{
super.doStartTag();
int result = SKIP_BODY;
setDone(true);
// Get the prepared statement and execute it (retrieving the result set).
try
{
ps = getPreparedStatement();
if (ps != null)
{
rs = ps.executeQuery();
if ((rs != null) && (rs.next()))
{
result = EVAL_BODY_TAG;
setDone(false);
}
}
}
catch (SQLException e)
{
UserClassFactory.getLogger().log("SQL error while executing the prepared statement.", e);
try
{
close();
}
catch (DataException ex)
{
UserClassFactory.getLogger().log("Exception while closing resources because there was an SQLException.", ex);
}
}
// If the result is to skip the body, close any open resources.
if (result == SKIP_BODY)
{
try
{
close();
}
catch (DataException ex)
{
UserClassFactory.getLogger().log("Exception while closing resources because there was no data to process.", ex);
}
}
return result;
}
/**
* Returns a PreparedStatement object which will be processed and closed - it will only be
* called once (unless it returns null, then it may be called several times).
*/
protected abstract PreparedStatement getPreparedStatement() throws SQLException;
/**
* Returns a Connection pool object that will allow the system to get
* and release the connection being used for this query - this is optional, so
* you do not have to return anything.
*/
protected abstract ConnectionPoolInf getConnectionPool() throws SQLException;
/**
* Called by objects that want to request a value by its name.
*
* @param requestor the object requesting the value or the object that the value
* is being requested for (like with a lookup: you would pass the TagSupport
* object, not the lookup which is what actually calls this function).
* @param name the name of the value to retrieve.
*
* @return the value that was found by the name supplied.
*/
public Object retrieveValueByName(Object requestor, String name)
{
Object result = null;
// If there is a result set, it is not done processing, and there is a name to find,
// look up the field in the result set and return its value.
if ((rs != null) && (!isDone()) && (name != null))
{
try
{
// Look for the column in the result set.
int colNum = rs.findColumn(name);
// If found, get its type and return it.
if (colNum > 0)
{
result = getDataByColInfo(rs, colNum);
}
}
catch (SQLException e)
{
UserClassFactory.getLogger().log("SQL error while retrieving data from result set.", e);
}
}
return result;
}
/**
* This method is called by the {@link #retrieveValueByName(Object, String)} to retrieve
* the value of a column from a result set. It assumes that a valid column number is passed in.
*
* @param rs the result set to retrieve the value from.
* @param colNum the column to retrieve the data from.
*/
protected Object getDataByColInfo(ResultSet rs, int colNum) throws SQLException
{
return rs.getObject(colNum);
}
/**
* When there is an exception or the result set has been processed, this method will be called.
*/
public void close() throws DataException
{
setDone(false);
try
{
if (rs != null)
{
rs.close();
}
}
catch (SQLException e)
{
throw new DataException("Exception thrown while closing result set that was being iterated over.", e);
}
finally
{
rs = null;
try
{
if (ps != null)
{
ps.close();
}
}
catch (SQLException e)
{
throw new DataException("Exception thrown while closing statement that was being iterated over.", e);
}
finally
{
ps = null;
try
{
ConnectionPoolInf connPool = getConnectionPool();
if (connPool != null)
{
connPool.releaseConnection();
}
}
catch (SQLException e)
{
throw new DataException("Exception thrown while closing connection that was used by the statement that was being iterated over.", e);
}
}
}
}
/**
* This function is called to get the next record in the ResultSet.
*
* @return this function returns <code>true</code> until there is no more data,
* then it returns <code>false</code>.
*/
public boolean next() throws DataException
{
if ((rs != null) && (!isDone()))
{
try
{
setDone(!rs.next());
}
catch (SQLException e)
{
setDone(true);
throw new DataException("Exception thrown while moving to the next row in the result set.", e);
}
}
return !isDone();
}
/**
* Called by the container to free the tag's resources - super class calls the close()
* method from the RetrieveValueByNameIteratorInf interface to close any open resources.
*/
public void release()
{
super.release();
rs = null;
ps = null;
}
private ResultSet rs;
private PreparedStatement ps;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -