将获取连接池整合成工具类,然后利用JdbcTemplate对多个表进行查询,并将结果输出
工具类代码:
import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; /* 整合工具类,获取连接池和释放资源 */ public class Utils22 { static Properties pro =null; static {//加载配置文件 pro = new Properties(); InputStream in = Utils22.class.getClassLoader().getResourceAsStream("druid.properties"); try { pro.load(in); } catch (IOException e) { e.printStackTrace(); } } //获取连接池对象 public static DataSource getDataSource(){ DataSource ds = null; try { ds = DruidDataSourceFactory.createDataSource(pro); } catch (Exception e) { e.printStackTrace(); } return ds; } //释放资源 public static void closeResource(ResultSet rs, Statement st, Connection ct) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (st != null) { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } if (ct != null) { try { ct.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
自定义员工类简易代码:
public class Emp { private Integer id; private String ename; private String dname; private String jname; private Double salary; private Integer grade; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getDname() { return dname; } public void setDname(String dname) { this.dname = dname; } public String getJname() { return jname; } public void setJname(String jname) { this.jname = jname; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public Integer getGrade() { return grade; } public void setGrade(Integer grade) { this.grade = grade; } @Override public String toString() { return "Emp{" + "id=" + id + ", ename='" + ename + '\'' + ", dname='" + dname + '\'' + ", jname='" + jname + '\'' + ", salary=" + salary + ", grade=" + grade + '}'; } }
测试类代码:
/* JdbcTemplate使用练习 */ import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; import java.util.List; import java.util.Map; public class Test04 { public static void main(String[] args) throws Exception { //调用工具类方法获得连接池 DataSource ds = Utils22.getDataSource(); JdbcTemplate jt = new JdbcTemplate(ds);//创建JdbcTemplate实例对象 //编写sql语句 //查询经理的总数 String sql1 = "SELECT COUNT(*) FROM emp e JOIN job j ON e.`job_id`=j.`id` WHERE j.`jname`=?;"; int count = jt.queryForObject(sql1, int.class, "经理");//将查询结果以int类型返回 //查询工资最高的员工的姓名,工资金额,部门名字和职位名称 String sql2 = "SELECT e.ename,j.jname,d.dname,e.`salary` FROM emp e JOIN dept d JOIN job j ON\n" + "e.`job_id`=j.`id` AND e.`dept_id`=d.`id` HAVING e.`salary`=MAX(salary);"; Map<String, Object> map = jt.queryForMap(sql2);//将查询结果以map形式返回 //查询名字叫孙悟空的 员工id,姓名和职位名称,部门名称,工资,及工资等级, String sql3 ="select e.id,e.ename,j.jname,d.dname,e.salary,s.grade from emp e join job j join\n" + "dept d join salarygrade s on e.`job_id`=j.`id` and e.`dept_id`=d.`id` where \n" + "e.`salary` between s.`losalary` and s.`hisalary` and e.`ename`=?;"; Emp emp = jt.queryForObject(sql3, new BeanPropertyRowMapper<Emp>(Emp.class), "孙悟空");//将查询结果封装成对象 //查询工资大于20000的员工的 员工id,姓名和职位名称,部门名称,工资,及工资等级, String sql4 = "SELECT e.id,e.ename,j.jname,d.dname,e.salary,s.grade FROM emp e JOIN job j JOIN\n" + "dept d JOIN salarygrade s ON e.`job_id`=j.`id` AND e.`dept_id`=d.`id` WHERE \n" + "e.`salary` BETWEEN s.`losalary` AND s.`hisalary` AND e.`salary`>?;"; List<Emp> list = jt.query(sql4, new BeanPropertyRowMapper<>(Emp.class), 20000);//查询结果为Emp对象的list集合 List<Map<String, Object>> maps = jt.queryForList(sql4, 20000);//查询结果为储存map元素的集合 //输出查询结果 System.out.println(count); System.out.println("==============================================="); System.out.println(map); System.out.println("==============================================="); System.out.println(emp); System.out.println("==============================================="); for (Emp emp1 : list) { System.out.println(emp1); } System.out.println("==============================================="); for (Map<String, Object> objectMap : maps) { System.out.println(objectMap); } } }
控制台输出效果: