jdbc连接池配置方法

代码:import java.io.FileInputStream;import java.io.FileNotFoundException;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.LinkedList;
import java.util.Properties;

import org.slf4j.Logger;

public class ConnPool {
    // 使用LinkedList集合存放数据库连接
    private static LinkedList<Connection> connPool = new LinkedList<Connection>();

    // 在静态代码块中加载配置文件
    static {
     //如果以jar包运行,此处会报找不到这个文件的异常,解决方案如下。 String path
= ConnPool.class.getClassLoader().getResource("db.properties").getPath();//解决方案需要注释这行代码
     //InputStream in = PropertiesUtil.class.getClassLoader.getResourceAsStream("db.properties");//开启这行代码解决以上问题
FileInputStream in;//开启以上解决方案需要注释调这行代码
try { in = new FileInputStream(path);//开启以上解决方案需要注释这行代码 Properties prop = new Properties(); prop.load(in); String driver = prop.getProperty("driver"); String url = prop.getProperty("url"); String user = prop.getProperty("user"); String password = prop.getProperty("password"); // 数据库连接池的初始化连接数的大小 int InitSize = Integer.parseInt(prop.getProperty("InitSize")); // 加载驱动 Class.forName(driver); for (int i = 0; i < InitSize; i++) { Connection conn = DriverManager.getConnection(url, user, password); // 将创建的连接添加的list中 System.out.println("初始化数据库连接池,创建第 " + (i + 1) + " 个连接,添加到池中"); connPool.add(conn); } } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } /*获取数据库连接*/ public Connection getConnection() throws SQLException { if(connPool.size() > 0){ //从集合中获取一个连接 final Connection conn = connPool.removeFirst(); //返回Connection的代理对象 return (Connection) Proxy.newProxyInstance(ConnPool.class.getClassLoader(), conn.getClass().getInterfaces(), new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if(!"close".equals(method.getName())){ return method.invoke(conn, args); }else{ connPool.add(conn); System.out.println("关闭当前连接,把连接还给连接池........."); System.out.println("池中连接数为 " + connPool.size()); return null; } } }); }else{ throw new RuntimeException("数据库繁忙,稍后再试............"); } } public PrintWriter getLogWriter() throws SQLException { return null; } public void setLogWriter(PrintWriter out) throws SQLException { } public void setLoginTimeout(int seconds) throws SQLException { } public int getLoginTimeout() throws SQLException { return 0; } public Logger getParentLogger() throws SQLFeatureNotSupportedException { return null; } public Object unwrap(Class iface) throws SQLException { return null; } public boolean isWrapperFor(Class iface) throws SQLException { return false; } public Connection getConnection(String username, String password) throws SQLException { return null; } }
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcUtil {

    //数据库连接池
    private static ConnPool  connPool = new ConnPool();

    /**
     * 从池中获取一个连接
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException{
        return connPool.getConnection();
    }

    /**
     * 关闭连接
     * @param conn
     * @param st
     * @param rs
     * @throws SQLException 
     */
    public static void CloseConnection(Connection conn, Statement st, ResultSet rs) throws SQLException{

        // 关闭存储查询结果的ResultSet对象
        if(rs != null){
                rs.close();
        }

        //关闭Statement对象
        if(st != null){
                st.close();
        }

        //关闭连接
        if(conn != null){
                conn.close();
        }
    }

}

db.properties

driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/数据库名?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=TRUE
user = root
password = root
InitSize = 15

调用如下:

JdbcUtil jdbcUtil = new JdbcUtil();
  String sql = "要写的SQL语句";
  Connection connection = null;
  Statement statement = null;
  ResultSet resultSet = null;
try{
  connection = jdbcUtil.getConnection();
  statement = connection.createStatement();
  resultSet = statement.executeQuery(sql);
    while (resultSet.next()) {
        String id= resultSet.getString("id");
        if (id!= null) {
      //可以在这里写业务逻辑
      }
    }
  } catch (Exception e) { logger.error("数据获取失败", e); msg = createResultJson(1, "数据获取失败!"); } finally { try {   jdbcUtil.CloseConnection(connection, statement, resultSet);//最后记得关闭流,不然会报创建连接过多的异常 } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
原文地址:https://www.cnblogs.com/wangquanyi/p/11446004.html