* 练习:
* 定义一个方法,查询emp表的数据将其封装为对象,然后装载集合,返回。
1. 定义Emp类
2. 定义方法 public List<Emp> findAll(){}
3. 实现方法 select * from emp;
desc emp查询emp的表结构
Emp类 :是建立的一个类来封装数据
package cn.itcast.domain;
import java.util.Date;
/*
实体类 用来封装数据的
封装Emp表数据的javaBean
*/
public class Emp {
private int id;
private String ename;
private int job_id;
private int mgr;
private Date joindate;
private double salary;
private double bonus;
private int dept_id;
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", ename='" + ename + '\'' +
", job_id=" + job_id +
", mgr=" + mgr +
", joindate=" + joindate +
", salary=" + salary +
", bounds=" + bonus +
", dept_id=" + dept_id +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public int getJob_id() {
return job_id;
}
public void setJob_id(int job_id) {
this.job_id = job_id;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public Date getJoindate() {
return joindate;
}
public void setJoindate(Date joindate) {
this.joindate = joindate;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getBouns() {
return bonus;
}
public void setBouns(double bouns) {
this.bonus = bouns;
}
public int getDept_id() {
return dept_id;
}
public void setDept_id(int dept_id) {
this.dept_id = dept_id;
}
}
方法:
package cn.itcast.jdbc;
import cn.itcast.domain.Emp;
import java.sql.*;
import java.sql.Date;
import java.util.*;
/*
需求:定义一个方法,查询emp表的数据将其封装为对象,然后装载集合,返回
*/
public class JdbcDemo08 {
public static void main(String[] args) {
List<Emp> list = new JdbcDemo08().findAll();
System.out.println(list);
}
/*
查询所有Emp对象
*/
public List<Emp> findAll() { //这里是定义了一个方法
//1.jdbc的查询步骤
//1.注册驱动
Connection conn = null; //抽取出来下面直接引用
ResultSet rs = null;
Statement stmt = null;
List<Emp> list=null;
try {
Class.forName("com.mysql.jdbc.Driver");
//2.sql语句
String sql = "select*from emp";
//3.获取连接
conn = DriverManager.getConnection("jdbc:mysql:///db3", "root", "root");
stmt = conn.createStatement();
//执行sql 用查询的方法.executeQuery(sql);
rs = stmt.executeQuery(sql);
//遍历结果集,封装对象,装载集合
Emp emp = null; //这里是创建emp引用 后面可以直接复用
list = new ArrayList<>();
while (rs.next()) {
//获取数据
int id = rs.getInt("id");
String ename = rs.getString("ename");
int job_id = rs.getInt("job_id");
int mgr = rs.getInt("mgr");
Date joindate = rs.getDate("joindate");
double salary = rs.getDouble("salary");
double bonus = rs.getDouble("bonus");
int dept_id = rs.getInt("dept_id");
//创建emp对象
emp = new Emp(); //注意这里不是Emp emp = new Emp(); 这里是一种复用
emp.setId(id); //这里是Emp类中 geter seter 方法在起作用
emp.setEname(ename);
emp.setJob_id(job_id);
emp.setMgr(mgr);
emp.setJoindate(joindate);
emp.setSalary(salary);
emp.setBouns(bonus);
emp.setDept_id(dept_id); //这都是类中的set方法加数据
//装载集合
list.add(emp); //集合中装了一个emp类
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
if (rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return list ;
}
}
输出:14条数据