Demo类
package com.lzy.obj;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Demo13 {
public static void main(String[] args) {
test02();
}
public static void test01() {//封装一条记录
Connection conn=JDBCUtil.getMysqlConn();
PreparedStatement ps=null;
ResultSet rs=null;
Emp emp=null;
try {
ps=conn.prepareStatement("select empname,salary,age from emp where id=?");
ps.setObject(1, 1);
rs=ps.executeQuery();
while(rs.next()) {
emp = new Emp(rs.getString(1),rs.getDouble(2),rs.getInt(3));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtil.close(rs,ps,conn);
}
System.out.println(emp.getEmpname()+"-"+emp.getSalary()+"-"+emp.getAge());
}
public static void test02() {//封装多条记录
Connection conn=JDBCUtil.getMysqlConn();
PreparedStatement ps=null;
ResultSet rs=null;
List<Emp>list=new ArrayList<>();
try {
ps=conn.prepareStatement("select empname,salary,age from emp where id>?");
ps.setObject(1, 1);
rs=ps.executeQuery();
while(rs.next()) {
Emp emp = new Emp(rs.getString(1),rs.getDouble(2),rs.getInt(3));
list.add(emp);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtil.close(rs,ps,conn);
}
///遍历List,就是遍历这一行的多列的信息
for(Emp emp:list){
System.out.println(emp.getEmpname()+"-"+emp.getSalary()+"-"+emp.getAge());
}
}
}
封装Emp和Dept类(类名要和数据库表名相同,属性要和数据库表里的属性对应一致)
Emp类
package com.lzy.obj;
import java.sql.Date;
public class Emp {//表结构和类对应
private Integer id;
private String empname;
private Integer age;
private Double salary;
private Date birthday;
private Integer deptId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
public Emp(Integer id, String empname, Integer age, Double salary, Date birthday, Integer deptId) {
super();
this.id = id;
this.empname = empname;
this.age = age;
this.salary = salary;
this.birthday = birthday;
this.deptId = deptId;
}
public Emp(String empname, Integer age, Double salary, Date birthday, Integer deptId) {
super();
this.empname = empname;
this.age = age;
this.salary = salary;
this.birthday = birthday;
this.deptId = deptId;
}
public Emp(String empname, Double salary, Integer age) {
this.empname = empname;
this.salary = salary;
this.age = age;
}
public Emp() {
}
}
Dept类
package com.lzy.obj;
public class Dept {
private Integer id;
private String dname;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Dept(Integer id, String dname, String address) {
super();
this.id = id;
this.dname = dname;
this.address = address;
}
public Dept(String dname, String address) {
super();
this.dname = dname;
this.address = address;
}
public Dept() {
}
}
JDBCUtil类和文件db.properties与前面文章的一致