JDBC14 ORM03 JavaBean封装
Javabean对象封装一条信息(推荐)
让JavaBean的属性名和类型尽量和数据库保持一致
一条记录对应一个对象,将这些查询到的对象放到容器中(List)
表信息如下
List封装多条信息
Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; List<Emp> list=new ArrayList<>(); try { conn=Utils.getConn(); ps=conn.prepareStatement("select Empname,birthday,salary from emp where id>?"); ps.setObject(1, 1); rs=ps.executeQuery(); while(rs.next()) { Emp emp=new Emp(rs.getString(1),rs.getDate(2),rs.getDouble(3)); list.add(emp); } for(Emp emp:list) { System.out.println(emp.getName()+"--"+emp.getBirthday()+"--"+emp.getSalary()); } System.out.println(); } catch (SQLException e) { e.printStackTrace(); }
Emp类
class Emp{ private Integer id; private String name; private Date birthday; private Double salary; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public Emp(Integer id, String name, Date birthday, Double salary) { this.id = id; this.name = name; this.birthday = birthday; this.salary = salary; } public Emp(String name, Date birthday, Double salary) { super(); this.name = name; this.birthday = birthday; this.salary = salary; } public Emp() { } }