package com.liujinghe.util; import java.io.FileInputStream; import java.io.IOException; import java.sql.*; import java.util.Properties; public class JdbcUtil4 { /** * 加载驱动 * 设置url,user,password * 获得数据库链接对象 * 关闭资源 */ private static String url; private static String user; private static String password; private static String driver; static { //创建properties对象 Properties properties = new Properties(); //将db.properties读入到properties对象中 try { properties.load(new FileInputStream("./src/db.properties")); //获取属性值 url=properties.getProperty("url"); user=properties.getProperty("user"); password=properties.getProperty("password"); driver=properties.getProperty("driver"); //加载驱动 Class.forName(driver); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } //获取数据库链接对象 public static Connection getConnection(){ Connection connection = null; try { connection = DriverManager.getConnection(url, user, password); } catch (SQLException e) { e.printStackTrace(); } return connection; } //关闭流资源 public static void close(Connection connection,Statement statement){ close(connection,statement,null); } public static void close(Connection connection, Statement statement,ResultSet resultSet){ if(resultSet!=null){ try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if(statement!=null){ try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if(connection!=null){ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
JdbcUtil封装(获取connection对象)
最新推荐文章于 2023-06-13 15:30:38 发布