use CachedRowSet to delegate sql manipulation

本文介绍了一个用于查询银行名称、城市、路由代码等信息的方法。通过使用DAO模式,该方法能够从数据库中检索相关信息并将其封装到BankRoutineCodeList对象中返回。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值