其他分享
首页 > 其他分享> > JDBC13 ORM02 Map封装

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();
            }

 

标签:Map,rs,ps,getObject,ORM02,put,JDBC13,row
来源: https://www.cnblogs.com/code-fun/p/11452343.html