读取数据库
(定义一个方法,查询数据库中emp表的数据将其封装为对象,然后装载集合,返回)
1、首先定义一个Emp(数据库中的emp表)(包含数据库中的查询字段)
package cn.itcast.domain;
import java.sql.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;
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 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;
}
@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 +
'}';
}
}
#-------------------------------------------------------------------------------------------------------------------
2、定义一个类(里面包含一个循环查询的findAll()方法,并返回一个集合)
package cn.itcast.jdbc;
import cn.itcast.domain.Emp;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* 定义一个方法,查询数据库中emp表的数据将其封装为对象,然后装载集合,返回
*/
public class JdbcDemo05 {
public static void main(String[] args) {
JdbcDemo05 j = new JdbcDemo05();
List<Emp> li = j.findAll();
//打印list的结果
System.out.println(li);
System.out.println("共有" + li.size() + "条数据");
System.out.println("----------------------------------");
//加强for遍历集合
for (Emp e : li) {
System.out.println(e);
System.out.println("----------------------------------------------------------" +
"--------------------------------------------------------");
}
}
/**
* 查询数据库的函数
* @return 返回一个集合
*/
public List<Emp> findAll() {
//定义变量
Connection con = null;
Statement st = null;
ResultSet re = null;
Emp emp = null;
List<Emp> list = null;
try {
//1、加载jar包,并且注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2、定义一个查询SQL语句
String sql = "select * from emp";
//3、获取连接数据库对象
con = DriverManager.getConnection("jdbc:mysql:///gh_test2", "root", "root");
//4、获取执行SQL语句的对象
st = con.createStatement();
//5、执行SQL语句
re = st.executeQuery(sql);
//6、处理结果
list = new ArrayList<Emp>();
while (re.next()) {
int id = re.getInt("id");
String ename = re.getString("ename");
int job_id = re.getInt("job_id");
int mgr = re.getInt("mgr");
Date joindate = re.getDate("joindate");
double salary = re.getDouble("salary");
double bonus = re.getDouble("bonus");
int dept_id = re.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.setSalary(salary);
emp.setBonus(bonus);
emp.setDept_id(dept_id);
//写入数据进入集合
list.add(emp);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//7、释放资源
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
re.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
}