使用JDBC连接数据库,查询结果转成List或者Map(臃肿版): http://my.oschina.net/zhengweishan/blog/484377
这个感觉不是很好:然后改成下面的这种方式:
public static List<Map<String, Object>> convertList(ResultSet rs) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); try { ResultSetMetaData md = rs.getMetaData(); int columnCount = md.getColumnCount(); while (rs.next()) { Map<String, Object> rowData = new HashMap<String, Object>(); for (int i = 1; i <= columnCount; i++) { rowData.put(md.getColumnName(i), rs.getObject(i)); } list.add(rowData); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (rs != null) rs.close(); rs = null; } catch (SQLException e) { e.printStackTrace(); } } return list; } public static Map<String, Object> convertMap(ResultSet rs){ Map<String, Object> map = new TreeMap<String, Object>(); try{ ResultSetMetaData md = rs.getMetaData(); int columnCount = md.getColumnCount(); while (rs.next()) { for (int i = 1; i <= columnCount; i++) { map.put(md.getColumnName(i), rs.getObject(i)); } } } catch (SQLException e){ e.printStackTrace(); } finally { try { if (rs != null) rs.close(); rs = null; } catch (SQLException e) { e.printStackTrace(); } return map; }
时间: 2024-10-10 04:39:17