**使用List(map)来封装多条记录**
public static void main(String[] args) {
Connection conn=jdbcutil.getMysqlConn();
PreparedStatement ps=null;
ResultSet rs=null;
List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
try {
ps = conn.prepareStatement("select empname,salary,age from emp where id=?");
ps.setInt(1, 1);
rs=ps.executeQuery();
while(rs.next())
{
Map<String,Object> map=new HashMap<String,Object>();
map.put("empname", rs.getObject(1));
map.put("salary", rs.getObject(2));
map.put("age", rs.getObject(3));
list.add(map);
}
for(Map<String,Object> map:list)
{
for(String key:map.keySet())
{
System.out.println(key+"--"+map.get(key));
}
}
} catch (SQLException e) {
e.printStackTrace();
}finally
{
jdbcutil.close(rs, ps, conn);
}
}
**使用Map(Map)来封装多条记录**
public static void main(String[] args) {
Connection conn=jdbcutil.getMysqlConn();
PreparedStatement ps=null;
ResultSet rs=null;
Map<String,Map<String,Object>> maps=new HashMap<String,Map<String,Object>>();
try {
ps = conn.prepareStatement("select empname,salary,age from emp where id=?");
ps.setInt(1, 1);
rs=ps.executeQuery();
while(rs.next())
{
Map<String,Object> map=new HashMap<String,Object>();
map.put("empname", rs.getObject(1));
map.put("salary", rs.getObject(2));
map.put("age", rs.getObject(3));
maps.put(rs.getString(1), map);
}
for(String empname:maps.keySet())
{
Map<String,Object> map=maps.get(empname);
for(String key:map.keySet())
{
System.out.println(key+"--"+map.get(key));
}
}
} catch (SQLException e) {
e.printStackTrace();
}finally
{
jdbcutil.close(rs, ps, conn);
}
}
java JDBC-使用List(map)和Map(map)来封装多条msql记录
最新推荐文章于 2023-06-30 11:26:49 发布