适用于获取Connection对象的util package com.briup.myDataSource; import java.io.FileReader; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; // 用于连接数据库的工具类 public class JDBCUTil { private static String url; private static String user; private static String password; static { try { Properties pro = new Properties(); InputStream is = JDBCUTil.class.getClassLoader().getResourceAsStream("com/briup/jdbctest/jdbc.properties"); pro.load(is); url = pro.getProperty("url"); user = pro.getProperty("user"); password = pro.getProperty("password"); // 加载驱动类 Class.forName(pro.getProperty("driver")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url, user, password); } // 释放增删改的资源 public static void close(Statement statement, Connection conn) { if (statement != null) { try { statement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } // 用于查询时的释放资源 public static void close(ResultSet set, Statement statement, Connection conn) { if (set != null) { try { set.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }