能过JdbcTemplate,druid来查询表数据并封闭成Bean 与传统手工写法对比 100多行的代码 5行搞定 (Jar 牛B)

引用Jar后

package jdbcStudy;

import cn.itcast.domain.Emp;
import cn.itcast.util.JDBCDruid;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @author KuKaFei.Hai
 * @date 2020/5/11 : 13:02
 */
public class JDBCforEmpByTemplate {
    public static void main(String[] args) {
        //创建Template对象,并通过阿里巴巴Druid连接池,来获取连接
        JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCDruid.getDataSource());
        //创建SQL
        String selectSql ="select * from emp ";
        //执行SQL,并封装JAVA Bean 对象
        List rsSelect = jdbcTemplate.query(selectSql, new BeanPropertyRowMapper(Emp.class));
        //遍历集合
        for (Emp emp : rsSelect) {
            System.out.println(emp);
        }
    }
}

引用jar前

 

package jdbcStudy;

import cn.itcast.domain.Emp;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author KuKaFei.Hai
 * @date 2020/5/6 : 14:28
 */
public class JDBCforEmp {
    public static void main(String[] args) {
        String sql = "select * from emp";
        List emps = selectTable(sql);
        System.out.println("共查询到:"+emps.size()+"条记录");
        for (Emp emp : emps) {
            System.out.println(emp.toString());
        }

    }

    /**
     * 查询一个表,返回所有结果
     *
     * @param sql 传入的查询语句
     * @return 返回表中的所有记录 (每条记录封装成Emp对象,然后,把所有的记录,都保存在List中)
     */
    private static List selectTable(String sql) {
        /**
         * 抽取公有变量
         */
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        List listEmp = null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接对象
            conn = DriverManager.getConnection("jdbc:mysql://172.16.1.9/enterpriseinfomation",
                    "root", "***********");
            //3.获取sql执行对象
            stmt = conn.createStatement();
            //4.执行sql
            rs = stmt.executeQuery(sql);
            //5.遍历集合,封装对象
            Emp emp = null;
            listEmp = new ArrayList();
            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 salary = rs.getDouble("salary");
                double bonus = rs.getDouble("bonus");
                int dept_id = rs.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);
                //装载集合
                listEmp.add(emp);
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return listEmp;
    }
}

 

 

你可能感兴趣的:(苦咖菲.Java)