练习:
定义一个方法,查询emp表的数据将其封装为对象,然后装载集合,返回。
1. 定义Emp类
2. 定义方法 public List findAll(){}
3. 实现方法 select * from emp;
========================================================================================
package cn.yuxiang.domain;
/*封装emp表的数据Javabean(对象) */
//格式化ctrl+alt+L
import java.util.Date;
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;
//Alt+insert
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getJob_id() {
return job_id;
}
public void setJob_id(int job_id) {
this.job_id = job_id;
}
public Date getJoindate() {
return joindate;
}
public void setJoindate(Date joindate) {
this.joindate = joindate;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
public int getDept_id() {
return dept_id;
}
public void setDept_id(int dept_id) {
this.dept_id = dept_id;
}
//Alt+insert
@Override
public String toString() {
return "emp{" +
"id=" + id +
", ename='" + ename + '\'' +
", job_id=" + job_id +
", mgr=" + mgr +
", joindate=" + joindate +
", salary=" + salary +
", bonus=" + bonus +
", dept_id=" + dept_id +
'}';
}
}
package cn.yuxiang.jdbc;
*注意:【import cn.yuxiang.domain.Emp;】
import java.sql.*;
import java.util.ArrayList;
mport java.util.List;
public class jdbc_emp_Demo {
// 【调用方法】
public static void main(String[] args) {
//测试一个非静态的方法需要创建对象 调用他的方法
List<Emp> list = new jdbc_emp_Demo().findall();
System.out.println(list);
System.out.println(list.size());
}
【定义一个方法:】
- 查询emp表的数据将其封装成对象,然后装载到集合,返回。
- 查询所有emp对象
public List<Emp> findall() {
ResultSet rs = null;
Statement stat = null;
Connection conn = null;
List<Emp> list = null;
//1.注册驱动
try {
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库连接对象
conn = DriverManager.getConnection("jdbc:mysql:///db2", "root", "yuxiang1128");
//3.定义sql语句
String sql = "select *from emp";
//4.获取执行sql语句的对象
stat = conn.createStatement();
//5.执行sql语句 返回一个结果集对象
rs = stat.executeQuery(sql);
//6.遍历结果集,封装对象,装载集合
//让引用复用
Emp emp = null;
list = new ArrayList<Emp>();
while (rs.next()) {
//7.获取数据
int id = rs.getInt("id");
String ename = rs.getString("ename");
int job_id = rs.getInt("job_id");
int mgr = rs.getInt("mgr");
Date join_date = 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.setId(id);
emp.setEname(ename);
emp.setJob_id(job_id);
emp.setMgr(mgr);
emp.setJoindate(joindate);
emp.setBonus(bonus);
emp.setDept_id(dept_id);
//装载到集合
list.add(emp);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//判断
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stat != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return list;
}
}