JDBC13 ORM02 Map封装
用Map封装一条信息
conn=Utils.getConn(); ps=conn.prepareStatement("select Empname,birthday,salary from emp where id=?"); ps.setObject(1, 1); rs=ps.executeQuery(); while(rs.next()) { row.put("Empname",rs.getObject(1) ); row.put("birthday", rs.getObject(2)); row.put("salary", rs.getObject(3)); } for(String key:row.keySet()) { System.out.print(row.get(key)); }
用List盛放Map 存储多条信息
conn=Utils.getConn(); ps=conn.prepareStatement("select Empname,birthday,salary from emp where id>?"); ps.setObject(1, 1); rs=ps.executeQuery(); while(rs.next()) { Map<String,Object>row=new HashMap<>(); row.put("Empname",rs.getObject(1) ); row.put("birthday", rs.getObject(2)); row.put("salary", rs.getObject(3)); list.add(row); } for(Map<String,Object> map:list) { for(String key:map.keySet()) { System.out.print(map.get(key)); } System.out.println(); }