要求:定义一个方法,查询emp表的数据将其封装为对象,然后装载集合,返回。
实现方法:
1.定义Emp类
2.定义方法 public List<Emp> findAll(){}
3.实现方法 select * from emp;
案例:
定义Emp类:
Emp{id=1, name='张三', gender='男', salary=1800.0, join_date=2019-08-23, dept_id=1}
Emp{id=2, name='李四', gender='男', salary=3800.0, join_date=2025-02-23, dept_id=1}
Emp{id=3, name='王五', gender='女', salary=1800.0, join_date=2019-08-23, dept_id=2}
Emp{id=4, name='麻六', gender='男', salary=2800.0, join_date=2010-07-23, dept_id=3}
Emp{id=5, name='田七', gender='女', salary=3800.0, join_date=2014-08-23, dept_id=1}

实现方法:
1.定义Emp类
2.定义方法 public List<Emp> findAll(){}
3.实现方法 select * from emp;
案例:
定义Emp类:
package cn.itcast.domain;
import java.util.Date;
/*
对应封装emp表数据的JavaBean
*/
public class Emp {
private int id;
private String name;
private String gender;
private double salary;
private Date join_date;
private int dept_id;
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", salary=" + salary +
", join_date=" + join_date +
", dept_id=" + dept_id +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Date getJoin_date() {
return join_date;
}
public void setJoin_date(Date join_date) {
this.join_date = join_date;
}
public int getDept_id() {
return dept_id;
}
public void setDept_id(int dept_id) {
this.dept_id = dept_id;
}
}
使用Emp类
package cn.itcast.jbdc;
import cn.itcast.domain.Emp;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/*
定义一个方法,查询emp表的数据将其封装为对象,然后装载集合,返回。
*/
public class JDBCDemo06 {
public static void main(String[] args) {
List<Emp> list = new JDBCDemo06().findAll();
for (Emp emp : list) {
System.out.println(emp);
}
}
/*
查询所有emp对象
*/
public List<Emp> findAll(){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List<Emp> list = null;
try {
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
conn = DriverManager.getConnection("jdbc:mysql:///bd3", "root", "root");
//3.定义sql
String sql = "select * from emp";
//4.获取执行sql对象
stmt = conn.createStatement();
//5.执行sql
rs = stmt.executeQuery(sql);
//6.遍历结果集,封装对象,装载集合
Emp emp = null;
list = new ArrayList<Emp>();
while (rs.next()){
//获取数据
int id = rs.getInt("id");
String name = rs.getString("name");
String gender = rs.getString("gender");
double salsry = rs.getDouble("salary");
Date join_date = rs.getDate("join_date");
int dept_id = rs.getInt("dept_id");
//创建emp对象,并赋值
emp = new Emp();
emp.setId(id);
emp.setName(name);
emp.setGender(gender);
emp.setSalary(salsry);
emp.setJoin_date(join_date);
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 (stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return list;
}
}
Emp{id=1, name='张三', gender='男', salary=1800.0, join_date=2019-08-23, dept_id=1}
Emp{id=2, name='李四', gender='男', salary=3800.0, join_date=2025-02-23, dept_id=1}
Emp{id=3, name='王五', gender='女', salary=1800.0, join_date=2019-08-23, dept_id=2}
Emp{id=4, name='麻六', gender='男', salary=2800.0, join_date=2010-07-23, dept_id=3}
Emp{id=5, name='田七', gender='女', salary=3800.0, join_date=2014-08-23, dept_id=1}

京公网安备 11010502036488号