zoukankan      html  css  js  c++  java
  • 自定义数据库连接池

    直接上代码

    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; } }
  • 相关阅读:
    AOP AspectJ 字节码 语法 MD
    判断小米华为等系统 MD
    vuejs2.0实现分页组件,使用$emit进行事件监听数据传递
    vuejs2.0实现一个简单的分页
    vuejs2.0使用Sortable.js实现的拖拽功能
    JavaScript之Number、String、Array常用属性与方法手册
    CSS3效果:5种预载动画效果
    vuejs 1.x
    window.requestAnimationFrame与Tween.js配合使用实现动画缓动效果
    如何用JavaScript判断dom是否有存在某class的值?
  • 原文地址:https://www.cnblogs.com/gxlaqj/p/11351088.html
Copyright © 2011-2022 走看看