Java中多表查询之JdbcTemplate

将获取连接池整合成工具类,然后利用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);
        }

    }
}

控制台输出效果:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值