package com.szpg.db.dao.impl; import java.sql.Connection; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.DbUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.ColumnListHandler; import org.apache.log4j.Logger; import com.szpg.db.dao.PgDeviceDao; import com.szpg.db.util.ConnectionManager; public class PgDeviceDaoImpl implements PgDeviceDao { private final Logger logger = Logger.getLogger(this.getClass().getName()); @Override public String findAcuCodeByCode(String code) { Connection conn = null; String queryStr = "SELECT PARTITION FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); List<String> tempList = (List<String>) runner.query(conn, queryStr, new ColumnListHandler<String>(), param); if (null != tempList && tempList.size() == 1) { String partition = tempList.get(0); logger.debug("根据设备代码查询所在ACU分区代码成功[" + partition + "]"); return partition; } else return null; } catch (Exception ex) { logger.error("根据设备代码查询所在ACU分区代码异常", ex); } finally { try { DbUtils.commitAndClose(conn); } catch (SQLException e) { e.printStackTrace(); } } return null; } @Override public Integer findDeviceIdByCode(String code) { Connection conn = null; String queryStr = "SELECT ID FROM PG_DEVICE WHERE ASSETCODE = ?"; Object[] param = new Object[1]; param[0] = code; try { conn = ConnectionManager.getConnectionFromC3P0(); conn.setAutoCommit(false); QueryRunner runner = new QueryRunner(); List<Integer> tempList = (List<Integer>) runner.query(conn, queryStr, new ColumnListHandler<Integer>(), param); if (null != tempList && tempList.size() == 1) { int id = ((Number) tempList.get(0)).intValue(); logger.debug("根据代码查询设备ID成功[ID=" + id + ", CODE=" + code + "]"); return id; } else { return -1; } } catch (Exception ex) { logger.error("根据代码查询设备ID异常", ex); } finally { try { DbUtils.commitAndClose(conn); } catch (SQLException e) { e.printStackTrace(); } } return -1; } }