Javaでsql文を直接実行し、Listセットに戻る 2168 ワード java Javaでsql文を直接実行し、Listセットに戻るpublic static List> getResultToList(String sql, Object... params) { PreparedStatement ps = null; Connection conn = getConnection(); ResultSet rs = null; List> rows = new ArrayList>(); if (null == sql || "".equals(sql)) { return new ArrayList>(); } try { ps = conn.prepareStatement(sql); if (null != params && params.length > 0) { for (int i = 0, len = params.length; i < len; i++) { Object arg = params[i]; ps.setObject(i + 1, arg); } } rs = ps.executeQuery(); if (null != rs) { ResultSetMetaData rsm = rs.getMetaData(); int count = rsm.getColumnCount(); Map record = null; if (count > 0) { while (rs.next()) { record = new HashMap(); for (int j = 0; j < count; j++) { Object obj = rs.getObject(j + 1); String columnName = rsm.getColumnName(j + 1); record.put(columnName.toLowerCase(),(obj == null) ? "" : obj); } rows.add(record); } } } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { release(rs, ps, conn); } return rows; } // public static void release(Object...obj){ if(null!=obj){ for(Object o:obj){ try { if (o instanceof ResultSet) { ((ResultSet) o).close(); } else if (o instanceof Statement) { ((Statement) o).close(); } else if (o instanceof Connection) { ((Connection) o).close(); }else if (o instanceof PreparedStatement) { ((PreparedStatement) o).close(); } } catch (Exception ex) { ex.printStackTrace(); } } } }実行文getResultToList("select * from user where us_name where us_name=?"," ") getResultToList("select * from user where us_name where us_name=' '") Pytorch入門——Tensor Flaskフレームワーク(4)——要求コンテキストソース分析、gオブジェクト、サードパーティプラグイン(flask_session、flask_script、wtforms)、信号