本帖最后由 StephenXiong000 于 2019-11-26 15:30 编辑
今天是自习,班主任说要我们写帖,我只能先写帖,再敲代码咯
从11.5开班到现在,已经学习了Java
(继承/多态/接口/异常/集合/IO流/多线程/反射)
和前端
(html/css/JS/MySQL/JDBC).
昨天刚讲完JDBC,用Java程序连接数据库,程序一步一步优化,前面的优化过程都不记得了,只记得最简的那几条语句,哈哈
public class JdbcTemplateDemo2 {
//1. 获取JDBCTemplate对象
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
/**
* 查询所有记录,将其封装为Emp对象的List集合
*/@Test
public void test6() {
String sql = "select * from emp";
List<Emp> list = template.query(sql, new RowMapper<Emp>() {
@Override
public Emp mapRow(ResultSet rs, int i) throws SQLException {
Emp emp = new Emp();
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.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);
return emp;
}
});
for (Emp emp : list) {
System.out.println(emp);
}
}
}
发完贴后我得赶紧重新温习整个优化过程了,大家继续加油!
|
|