/** * 按主键查找一条数据 * * @param lineCode 线路名称 * @return LineLossDto * @throws Exception */ public LineLossDto findByPrimaryKey(String lineCode) throws Exception { StringBuffer buffer = new StringBuffer(200); // 拼SQL语句 buffer.append("SELECT "); buffer.append("LineCode,"); buffer.append("R,"); buffer.append("LineLong,"); buffer.append("Volt,"); buffer.append("T,"); buffer.append("ValidStatus,"); buffer.append("Flag,"); buffer.append("Remark "); buffer.append("FROM LineLoss "); if (logger.isDebugEnabled()) { StringBuffer debugBuffer = new StringBuffer(buffer.length() * 4); debugBuffer.append(buffer.toString()); debugBuffer.append("WHERE "); debugBuffer.append("LineCode=").append("'").append(lineCode).append("'"); logger.debug(debugBuffer.toString()); } buffer.append("WHERE "); buffer.append("LineCode = ?"); dbManager.prepareStatement(buffer.toString()); // 设置条件字段; dbManager.setString(1, lineCode); ResultSet resultSet = dbManager.executePreparedQuery(); LineLossDto lineLossDto = null; if (resultSet.next()) { lineLossDto = new LineLossDto(); lineLossDto.setLineCode(dbManager.getString(resultSet, 1)); lineLossDto.setR(dbManager.getDouble(resultSet, 2)); lineLossDto.setLineLong(dbManager.getDouble(resultSet, 3)); lineLossDto.setVolt(dbManager.getDouble(resultSet, 4)); lineLossDto.setT(dbManager.getDouble(resultSet, 5)); lineLossDto.setValidStatus(dbManager.getString(resultSet, 6)); lineLossDto.setFlag(dbManager.getString(resultSet, 7)); lineLossDto.setRemark(dbManager.getString(resultSet, 8)); } resultSet.close(); return lineLossDto; }
/** * 按条件查询多条数据 * * @param conditions 查询条件 * @param pageNo 页号 * @param rowsPerPage 每页的行数 * @return Collection * @throws Exception */ public Collection findByConditions(String conditions, int pageNo, int rowsPerPage) throws Exception { StringBuffer buffer = new StringBuffer(200); // 拼SQL语句 buffer.append("SELECT "); buffer.append("LineCode,"); buffer.append("R,"); buffer.append("LineLong,"); buffer.append("Volt,"); buffer.append("T,"); buffer.append("ValidStatus,"); buffer.append("Flag,"); buffer.append("Remark "); buffer.append("FROM LineLoss WHERE "); buffer.append(conditions); boolean supportPaging = false; // 数据库是否支持分页 if (pageNo > 0) { // 对Oracle优化 if (dbManager .getConnection() .getMetaData() .getDatabaseProductName() .equalsIgnoreCase("Oracle")) { buffer.insert(0, "SELECT * FROM ( SELECT row_.*, rownum rownum_ FROM ("); buffer.append( ") row_ WHERE rownum <= " + rowsPerPage * pageNo + ") WHERE rownum_ > " + rowsPerPage * (pageNo - 1)); supportPaging = true; } else if (dbManager .getConnection() .getMetaData() .getDatabaseProductName() .equalsIgnoreCase("DB2")) { String sql = buffer.toString(); buffer.setLength(0); buffer.append("select * from ( select rownumber() over("); int orderByIndex = sql.toLowerCase().indexOf("order by"); if (orderByIndex > 0) { buffer.append(sql.substring(orderByIndex)); } buffer.append(") as rownumber_,"); buffer.append(sql.substring(6)); buffer.append(" ) as temp_ where rownumber_"); buffer.append( " between " + (rowsPerPage * (pageNo - 1) + 1) + " and " + rowsPerPage * pageNo); supportPaging = true; } } if (logger.isDebugEnabled()) { logger.debug(buffer.toString()); } ResultSet resultSet = dbManager.executeQuery(buffer.toString()); int count = 0; if (supportPaging == false && pageNo > 1) { dbManager.locate(resultSet, rowsPerPage * (pageNo - 1)); } // 定义返回结果集合 Collection collection = new ArrayList(rowsPerPage); LineLossDto lineLossDto = null; while (resultSet.next()) { if (supportPaging == false && pageNo > 0) { count++; if (count > rowsPerPage) { break; } } lineLossDto = new LineLossDto(); lineLossDto.setLineCode(dbManager.getString(resultSet, "LineCode")); lineLossDto.setR(dbManager.getDouble(resultSet, "R")); lineLossDto.setLineLong(dbManager.getDouble(resultSet, "LineLong")); lineLossDto.setVolt(dbManager.getDouble(resultSet, "Volt")); lineLossDto.setT(dbManager.getDouble(resultSet, "T")); lineLossDto.setValidStatus(dbManager.getString(resultSet, "ValidStatus")); lineLossDto.setFlag(dbManager.getString(resultSet, "Flag")); lineLossDto.setRemark(dbManager.getString(resultSet, "Remark")); collection.add(lineLossDto); } resultSet.close(); return collection; }