发表于: 2020-10-03 23:54:19
1 1463
今天完成的事情:
遇到的问题:
做这个的时候,报错出红一直解决不了,最后发现是定义对象的问题
emp类
package JDBCTest;
import java.sql.Date;
/**
* 封装Emp表数据的JavaBean
*/
public class emp {
private int id;
private String ename;
private int job_id;
private int mgr;
private java.util.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 (Date) 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 +
'}';
}
}
package jdbc;
import JDBCTest.emp;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* 定义一个方法,查询emp表的数据将其封装为对象,然后装载集合,返回。
*/
public class TestJDBC7 {
public static void main(String[] args){
List<emp> list = new TestJDBC7().findAll();
System.out.println(list);
System.out.println(list.size());
}
/**
* 查询所有emp对象
* @return
*/
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.获取连接
try {
conn = DriverManager.getConnection("jdbc:mysql:///yewubiao","root","wsj199599");
//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 ename = rs.getString("ename");
int job_id = rs.getInt("job_id");
int mgr =rs.getInt("mgr");
Date joindate = rs.getDate("joindate");
double bonus = rs.getDouble("bonus");
int dept_id = rs.getInt("dept_id");
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 (SQLException throwables) {
throwables.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
if (rs !=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (stmt !=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn !=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
return list;
}
}
输出结果
对着敲代码,出现报错
问了师兄发现,是在方法里面定义方法(缺少大括号),出现第二次了
报错,找不到文件
对照代码发现,yewubiao应该改成jdbc
成功运行
明天计划的事情:
1.了解 statement 和 preparedstatement 的区别
2. 了解占位符传参
3.学习JdbcTemplate
收获:感觉效率很低
评论