LinkedHashMap 记录插入顺序
1 statement = conn.createStatement(); 2 resultSet = statement.executeQuery(sql); 3 ResultSetMetaData rsmd = resultSet.getMetaData(); 4 int count = rsmd.getColumnCount(); 5 while (resultSet.next()) { 6 LinkedHashMap<String,Object> hm=new LinkedHashMap<String,Object>(count); 7 for (int i = 1; i <= count; i++) { 8 String key = rsmd.getColumnLabel(i); 9 Object value = resultSet.getObject(i); 10 hm.put(key, value); 11 } 12 item.add(hm); 13 }
需求是执行输入的sql,返回查询结果。结果在页面上数据列显示很随意,没有和数据库表字段保持一致。
解决方案:把HashMap替换为LinkedHashMap