Esempio n. 1
0
 /**
  * 根据Select查询产生Object对象
  *
  * @param sql
  * @param map
  * @param params
  * @return
  */
 public <T> T queryForObject(String sql, IRowMap<T> map, Object... params) {
   this.Debug(String.format("executeSQL:" + sql.replace("?", "%s"), params));
   T obj = null;
   PreparedStatement ps = null;
   ResultSet rs = null;
   try {
     ps = conn.prepareStatement(sql);
     if (params != null && params.length > 0) {
       for (int i = 0; i < params.length; i++) {
         ps.setObject(i + 1, params[i]);
       }
     }
     rs = ps.executeQuery();
     if (rs.next()) {
       obj = map.mapRow(rs);
     }
   } catch (SQLException e) {
     this.Debug(
         String.format("executeSQL:" + sql.replace("?", "%s"), params)
             + " Error Code : "
             + e.getErrorCode(),
         e);
   } finally {
     close(rs, ps, conn);
   }
   return obj;
 }
Esempio n. 2
0
 /**
  * 执行insert update delete SQl
  *
  * @param sql SQL语句
  * @param generatedKeys 当执行insert操作的时候,返回字段生产的key
  * @param params 参数列表
  * @param <T> 类型
  * @return
  */
 public <T> T executeSQL(String sql, IRowMap<T> generatedKeys, Object... params) {
   this.Debug(String.format("executeSQL:" + sql.replace("?", "%s"), params));
   PreparedStatement ps = null;
   ResultSet rs = null;
   try {
     ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
     if (params != null && params.length > 0) {
       for (int i = 0; i < params.length; i++) {
         ps.setObject(i + 1, params[i]);
       }
     }
     ps.executeUpdate();
     // 检索由于执行此 Statement 对象而创建的所有自动生成的键
     rs = ps.getGeneratedKeys();
     if (rs.next()) {
       return generatedKeys.mapRow(rs);
     }
   } catch (SQLException e) {
     this.Debug(
         String.format("executeSQL:" + sql.replace("?", "%s"), params)
             + " Error Code : "
             + e.getErrorCode(),
         e);
   } finally {
     close(rs, ps, conn);
   }
   return null;
 }