数据库连接工具类封装

数据库连接工具类封装

src目录下的db.properties:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbcstudy?			  useUnicode=true&characterEncoding=utf8&useSSL=true
username=root
password=123456

数据库连接工具类:

public class JdbcUtils{
    private static String driver = null;
    private static String url = null;
    private static String username = null;
    private static String password = null;
    
    static{
        try{
            InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
            Properties properties = new Properties();
            properties.load(in);
            
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
            
            //1.驱动只用加载一次
            Class.forName(driver);
        } catch(Exception e){
            e.printStackTrace();
        }
    }
    
    //获取连接
    public static Connection getConnection() throws SQLException{
        return DriverManager.getConnection(url,username,password);
    }
    
    //释放连接资源
    public static void release(Connection conn,Statement st,ResultSet rs){
        if (rs != null){
            try{
                rs.close();
            } catch (SQLException e){
                e.printStackTrace();
            }
        }
        if (st != null){
            try{
                st.close();
            } catch (SQLException e){
                e.printStackTrace();
            }
        }
        if (conn != null){
            try{
                conn.close();
            } catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
}

测试:

public class TestInsert{
    public static void main(String[] args){
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        
        try{
            conn = JdbcUtils.getConnection();	//获取数据库连接
            st = conn.createStatement();	//获取SQL执行对象
            String sql = "INSERT INTO users(id,'NAME','PASSWORD','email','birthday') VALUES(3,'BAN','123456','25445451@qq.com','2021-01-01')";
            int i = st.executeUpdate(sql);
            if (i>0) {
                System.out.println("插入成功!");
            }
        } catch (SQLException e){
            e.printStackTrace();
        } finally {
            JdbcUtils.release(conn,st,rs);
        }
    }
}
public class TestQuery{
    public static void main(String[] args){
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        
        try{
            conn = JdbcUtils.getConnection();	//获取数据库连接
            st = conn.createStatement();	//获取SQL执行对象
            String sql = "SELECT * FROM users where id = 3";
            rs = st.executeQuery(sql);
            
            while (rs.next()) {
                System.out.println(rs.getString("NAME"));
            }
        } catch (SQLException e){
            e.printStackTrace();
        } finally {
            JdbcUtils.release(conn,st,rs);
        }
    }
}
posted @ 2021-08-01 12:11  有我的担忧  阅读(32)  评论(0编辑  收藏  举报