• 自定义数据库连接池


    直接上代码

    package cn.sm1234.controller; import java.io.PrintWriter; import java.sql.Connection; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; import java.util.LinkedList; import java.util.List; import java.util.logging.Logger; import javax.sql.DataSource; import com.sun.istack.
    internal.Pool; public class MyPool implements DataSource { /**创建一个容器存放数据库连接池*/ static List<Connection> pool = new LinkedList<Connection>(); /**静态类初始化数据库连接*/ static{ for (int i = 0; i < 10; i++) { //可以从工具类里面获取数据库的连接池, 如下: //Connection con = JDBCUtils.getConnection(); pool.add(con); } } /**提供给外界connection方法,外界用来获取数据库连接*/ public Connection getConnection() throws SQLException { //从连接池中移除一个对象并返回这个对象 Connection conn = pool.remove(0); return conn; } /**提供一个返回连接的方法*/ public void returnConnection(Connection conn) { try { if (conn!=null && !conn.isClosed()) { pool.add(conn); } } catch (Exception e) { // TODO: handle exception } } public PrintWriter getLogWriter() throws SQLException { // TODO Auto-generated method stub return null; } public void setLogWriter(PrintWriter out) throws SQLException { // TODO Auto-generated method stub } public void setLoginTimeout(int seconds) throws SQLException { // TODO Auto-generated method stub } public int getLoginTimeout() throws SQLException { // TODO Auto-generated method stub return 0; } public Logger getParentLogger() throws SQLFeatureNotSupportedException { // TODO Auto-generated method stub return null; } public <T> T unwrap(Class<T> iface) throws SQLException { // TODO Auto-generated method stub return null; } public boolean isWrapperFor(Class<?> iface) throws SQLException { // TODO Auto-generated method stub return false; } public Connection getConnection(String username, String password) throws SQLException { // TODO Auto-generated method stub return null; } }
  • 相关阅读:
    并发队列 – 无界阻塞队列 LinkedBlockingQueue 原理探究
    并发队列 – 有界阻塞队列 ArrayBlockingQueue 原理探究
    Java回调机制解读
    一张图看懂encodeURI、encodeURIComponent、decodeURI、decodeURIComponent的区别
    uva 111 History Grading
    hdu 2546 饭卡
    hdu 2602 Bone Collector
    uva 10720 Graph Construction
    uva 10716 Evil Straw Warts Live
    uva 10070 Camel trading
  • 原文地址:https://www.cnblogs.com/gxlaqj/p/11351088.html
Copyright © 2020-2023  润新知