| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4243 人关注过本帖
标题:JDBC工具类
只看楼主 加入收藏
飞逝的流年
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2020-8-27
结帖率:0
收藏
 问题点数:0 回复次数:0 
JDBC工具类
需求:
抽取注册驱动,抽取一个方法获取连接对象,抽取一个方法释放资源
大佬们,帮我看一下代码哪里有问题
工具类代码:
package itcast.util;
import
import
import
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {
    private static String url;
    private static String user;
    private static String password;
    private static String driver;

    static {

        try {
            //1.创建Properties集合类
            Properties pro = new Properties();
            //获取src路径下的文件的方式
            ClassLoader classLoader = JDBCUtils.class.getClassLoader();
            URL res = classLoader.getResource("jdbc.properties");
            String path = res.getPath();
            //2,加载文件
            pro.load(new FileReader(path));
            //pro.load(new FileReader("E:\\itcast\\JDBC\\src\\jdbc.properties"));
            //3.获取数据,赋值
            url = pro.getProperty("url");
            user = pro.getProperty("user");
            password = pro.getProperty("password");
            driver = pro.getProperty("driver");

            Class.forName(driver);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,user,password);
    }

    public static void close(Statement stmt ,Connection conn) {

        if ( stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }

    public static void close  ( ResultSet rs,Statement stmt ,Connection conn) {

        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();
                }

            }
        }

    }
}

jdbc.properties文件代码:
"jdbc:mysql://localhost:3306/db3?useSSL=false&serverTimezone=Asia/Shanghai
user = root
password =root123
driver=com.mysql.cj.jdbc.Driver




需要使用工具类的测试代码:
import itcast.domain.Emp;
import itcast.util.JDBCUtils;

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

public class JDBCDemo6 {
    public static void main(String[] args) {
        List<Emp> list = new JDBCDemo6().findAll();
        System.out.println(list);
        System.out.println(list.size());
    }

    public List<Emp> findAll() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        List<Emp> list = null;
        try {
            //1.注册驱动
            // Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取连接
            //String url = "jdbc:mysql://localhost:3306/db3?useSSL=false&&serverTimezone=Asia/Shanghai";
            //conn = DriverManager.getConnection(url, "root", "root123");
            //简化后测试
            conn = JDBCUtils.getConnection();
            //3.定义sql语句
            String sql = "select * from emp";
            //4.获取s执行sql的对象
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            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 salary = rs.getDouble("salary");
                double bonus = rs.getDouble("bonus");
                int dept_id = rs.getInt("dept_id");

                emp = new Emp();
                emp.setId(id);
                emp.setEnamn(ename);
                emp.setJob_id(job_id);
                emp.setMgr(mgr);
                emp.setJoindate(joindate);
                emp.setSalary(salary);
                emp.setBonus(bonus);
                emp.getDept_id(dept_id);

                list.add(emp);

                //  System.out.println(id + name +"--"+ job_id + "--"+ mgr + "--"+ joindate + "--"+ salary +"--"+ bonus +"--"+ dept_id);
            }

        } catch (SQLException throwables) {
            throwables.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();
                }

            }
        }*/
            //简化后测试
            JDBCUtils.close(rs, stmt, conn);

        }
        return list;
    }
}
搜索更多相关主题的帖子: conn close JDBC catch null 
2021-09-19 22:25
快速回复:JDBC工具类
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017110 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved