31 jdbc查询,javaBean引入
jsbc 数据库的查询
与增删改不一样,查询会返回具体的记录集合,我们使用ResultSet类的实例来接收,接收后,使用实例.next()获取下一条记录,使用对应的getInt()、getDouble()方法来获取一条记录对应的字段值。如下:
关键代码:
conn = DriverManager.getConnection(url,username,password); //3.设置自动提交设置为false conn.setAutoCommit(false); //3.获取sql命令对象 stmt = conn.createStatement(); //4.创建SQL命令 String sql2 = "select * from emp"; //5.执行SQL命令 ResultSet executeQuery = stmt.executeQuery(sql2); while(executeQuery.next()) { System.out.println(executeQuery.getInt("empno")+"\t"+executeQuery.getString("ename")+"\t"+executeQuery.getString("job")+"\t\t"+executeQuery.getInt("mgr")+"\t"+executeQuery.getDate("hiredate")+"\t"+executeQuery.getDouble("sal")+"\t"+executeQuery.getDouble("comm")+"\t"+executeQuery.getInt("deptno")); }
结果:
引入javaBean
在上面的例子中,我们可以知道,返回的对象中包含了不同类型的对象,为了规范这些对象,我们获取的结果抽象为一个类,将结果封装到类的实例里,在这个例子中,我们查询的是emp表,emp就是员工表。
于是我们创建一个javaBean包,包里面用来存放javaBean类,它们只有属性,无参构造方法和getter/setter,这里对应emp我们创建一个Employee类(不能是复数形式)。
注意:最好实现序列化
package cn.xiaohei.bean; import java.io.Serializable; import java.sql.Date; public class Employee implements Serializable{ private static final long serialVersionUID = 1L; int eid; String name; String job; int mgr; Date edate;//入职日期 double sal; double comm; int deptno;//部门编号 public Employee(int eid, String name, String job, int mgr, Date edate, double sal, double comm, int deptno) { super(); this.eid = eid; this.name = name; this.job = job; this.mgr = mgr; this.edate = edate; this.sal = sal; this.comm = comm; this.deptno = deptno; } public Employee() { } public int getEid() { return eid; } public void setEid(int eid) { this.eid = eid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public int getMgr() { return mgr; } public void setMgr(int mgr) { this.mgr = mgr; } public Date getEdate() { return edate; } public void setEdate(Date edate) { this.edate = edate; } public double getSal() { return sal; } public void setSal(double sal) { this.sal = sal; } public double getComm() { return comm; } public void setComm(double comm) { this.comm = comm; } public int getDeptno() { return deptno; } public void setDeptno(int deptno) { this.deptno = deptno; } @Override public String toString() { return "Employee [eid=" + eid + ", name=" + name + ", job=" + job + ", mgr=" + mgr + ", edate=" + edate + ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + "]"; } }
接下来,我们就可以修改上面的代码,将查询到的结果封住为Bean实例,然后,存放到一个数据结构中,通常我们选择ArrayList。
修改上面代码:
ArrayList<Employee> list = new ArrayList<>(); while(executeQuery.next()) { Employee emp = new Employee(); emp.setEid(executeQuery.getInt("empno")); emp.setName(executeQuery.getString("ename")); emp.setJob(executeQuery.getString("job")); emp.setMgr(executeQuery.getInt("mgr")); emp.setEdate(executeQuery.getDate("hiredate")); emp.setSal(executeQuery.getDouble("sal")); emp.setComm(executeQuery.getDouble("comm")); emp.setDeptno(executeQuery.getInt("deptno")); list.add(emp); }