Example #1
0
  public static List<Map<String, Object>> executeQuery(
      Connection conn, String sql, List<Object> parameters) throws SQLException {
    List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();

    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
      stmt = conn.prepareStatement(sql);

      setParameters(stmt, parameters);

      rs = stmt.executeQuery();

      ResultSetMetaData rsMeta = rs.getMetaData();

      while (rs.next()) {
        Map<String, Object> row = new LinkedHashMap<String, Object>();

        for (int i = 0, size = rsMeta.getColumnCount(); i < size; ++i) {
          String columName = rsMeta.getColumnLabel(i + 1);
          Object value = rs.getObject(i + 1);
          row.put(columName, value);
        }

        rows.add(row);
      }
    } finally {
      JdbcUtils.close(rs);
      JdbcUtils.close(stmt);
    }

    return rows;
  }
Example #2
0
  public static void execute(Connection conn, String sql, List<Object> parameters)
      throws SQLException {
    PreparedStatement stmt = null;

    try {
      stmt = conn.prepareStatement(sql);

      setParameters(stmt, parameters);

      stmt.executeUpdate();
    } finally {
      JdbcUtils.close(stmt);
    }
  }