delegate sql manipulation to a DAO Util
public static final BankRoutineCodeList getBankRoutineCodeList(String searchBankName, String hidReason, int curSize) throws BaseException {
CachedRowSet rs = null;
BankRoutineCodeList bankRoutineCodeList = null;
BankRoutineCode bankRoutineCode = null;
try {
bankRoutineCodeList = new BankRoutineCodeList();
String sqlQuery = "select BANK_NAME,CITY,ROUTING_CODE,EXT_ROUTING_CODE,BKBR_SEQNO,BRANCH_CODE";
sqlQuery += " from " + tableName;
BaseParam params[] = new BaseParam[1];
rs = BaseDAO.getResultSet(sqlQuery, params);
while (rs.next()) {
bankRoutineCode = new BankRoutineCode();
bankRoutineCode.setBankName(rs.getString("BANK_NAME"));
bankRoutineCodeList.addBankRoutineCode(bankRoutineCode);
}
}catch(){
} finally {
try {
if (rs != null)
rs.close();
} catch (java.sql.SQLException sqe) {
throw new BaseException(sqe, "SQL Exception while closing connection : " + sqe);
}
}
return bankRoutineCodeList;
}
In BaseDAO:
public static CachedRowSet getResultSet(String sSql, Object[] objParams) throws SQLException,BaseException
{
Connection connection= null;
PreparedStatement ps = null;
Statement stmt = null;
ResultSet rs = null;
CachedRowSet _cachedRowSet = null;
try
{
connection = getDB2Connection();
if(objParams == null || objParams.length == 0){
stmt = connection.createStatement();
rs = stmt.executeQuery(sSql);
}else{
ps = connection.prepareStatement(sSql);
for(int i=0 ; i < objParams.length; i++) {
BaseParam prepareParams = (BaseParam)objParams[i];
int datatype = prepareParams.getDataType();
switch(datatype){
case BaseConstants.INTEGER:
ps.setInt((i + 1),Integer.parseInt(prepareParams.getValue()));
break;
case BaseConstants.STRING:
ps.setString((i + 1),prepareParams.getValue());
break;
case BaseConstants.LONG:
ps.setLong((i + 1),Long.parseLong(prepareParams.getValue()));
break;
case BaseConstants.FLOAT:
ps.setFloat((i + 1),Float.parseFloat(prepareParams.getValue()));
break;
case BaseConstants.DOUBLE:
ps.setDouble((i + 1),Double.parseDouble(prepareParams.getValue()));
break;
}
}
rs =ps.executeQuery();
}
_cachedRowSet = new CachedRowSet();
_cachedRowSet.populate(rs);
}catch(java.sql.SQLException se)
{
throw new BaseException(se,"SQLException occured in getResultSet () method of BaseDao while fetching records: " + se.getMessage () );
}
finally
{
try
{
if(rs != null)
rs.close();
if(ps != null)
ps.close();
if(connection != null)
connection.close();
}catch(java.sql.SQLException se)
{
se.printStackTrace();
}
}
return _cachedRowSet;
}