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

    package com.huawei.datasource;

    import java.sql.Array;
    import java.sql.Blob;
    import java.sql.CallableStatement;
    import java.sql.Clob;
    import java.sql.Connection;
    import java.sql.DatabaseMetaData;
    import java.sql.NClob;
    import java.sql.PreparedStatement;
    import java.sql.SQLClientInfoException;
    import java.sql.SQLException;
    import java.sql.SQLWarning;
    import java.sql.SQLXML;
    import java.sql.Savepoint;
    import java.sql.Statement;
    import java.sql.Struct;
    import java.util.Map;
    import java.util.Properties;

    public class SXTConnection implements Connection{

    private Connection connection;

    private boolean used = false;




    public SXTConnection(Connection connection) {
    this.connection = connection;
    }

    public boolean isUsed() {
    return used;
    }

    public void setUsed(boolean used) {
    this.used = used;
    }

    public Connection getConnection() {
    return connection;
    }

    public void setConnection(Connection connection) {
    this.connection = connection;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
    // TODO Auto-generated method stub
    return this.connection.unwrap(iface);
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
    // TODO Auto-generated method stub
    return false;
    }

    @Override
    public Statement createStatement() throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public PreparedStatement prepareStatement(String sql) throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public CallableStatement prepareCall(String sql) throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public String nativeSQL(String sql) throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public void setAutoCommit(boolean autoCommit) throws SQLException {
    // TODO Auto-generated method stub

    }

    @Override
    public boolean getAutoCommit() throws SQLException {
    // TODO Auto-generated method stub
    return false;
    }

    @Override
    public void commit() throws SQLException {
    // TODO Auto-generated method stub

    }

    @Override
    public void rollback() throws SQLException {
    // TODO Auto-generated method stub

    }

    @Override
    public void close() throws SQLException {
    // TODO Auto-generated method stub
    this.used = false;
    }

    @Override
    public boolean isClosed() throws SQLException {
    // TODO Auto-generated method stub
    return false;
    }

    /**
    * 释放资源
    */
    public void release(){
    try {
    this.connection.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

    @Override
    public DatabaseMetaData getMetaData() throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public void setReadOnly(boolean readOnly) throws SQLException {
    // TODO Auto-generated method stub

    }

    @Override
    public boolean isReadOnly() throws SQLException {
    // TODO Auto-generated method stub
    return false;
    }

    @Override
    public void setCatalog(String catalog) throws SQLException {
    // TODO Auto-generated method stub

    }

    @Override
    public String getCatalog() throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public void setTransactionIsolation(int level) throws SQLException {
    // TODO Auto-generated method stub

    }

    @Override
    public int getTransactionIsolation() throws SQLException {
    // TODO Auto-generated method stub
    return 0;
    }

    @Override
    public SQLWarning getWarnings() throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public void clearWarnings() throws SQLException {
    // TODO Auto-generated method stub

    }

    @Override
    public Statement createStatement(int resultSetType, int resultSetConcurrency)
    throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int resultSetType,
    int resultSetConcurrency) throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public CallableStatement prepareCall(String sql, int resultSetType,
    int resultSetConcurrency) throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public Map<String, Class<?>> getTypeMap() throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
    // TODO Auto-generated method stub

    }

    @Override
    public void setHoldability(int holdability) throws SQLException {
    // TODO Auto-generated method stub

    }

    @Override
    public int getHoldability() throws SQLException {
    // TODO Auto-generated method stub
    return 0;
    }

    @Override
    public Savepoint setSavepoint() throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public Savepoint setSavepoint(String name) throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public void rollback(Savepoint savepoint) throws SQLException {
    // TODO Auto-generated method stub

    }

    @Override
    public void releaseSavepoint(Savepoint savepoint) throws SQLException {
    // TODO Auto-generated method stub

    }

    @Override
    public Statement createStatement(int resultSetType,
    int resultSetConcurrency, int resultSetHoldability)
    throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int resultSetType,
    int resultSetConcurrency, int resultSetHoldability)
    throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public CallableStatement prepareCall(String sql, int resultSetType,
    int resultSetConcurrency, int resultSetHoldability)
    throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
    throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
    throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public PreparedStatement prepareStatement(String sql, String[] columnNames)
    throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public Clob createClob() throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public Blob createBlob() throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public NClob createNClob() throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public SQLXML createSQLXML() throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public boolean isValid(int timeout) throws SQLException {
    // TODO Auto-generated method stub
    return false;
    }

    @Override
    public void setClientInfo(String name, String value)
    throws SQLClientInfoException {
    // TODO Auto-generated method stub

    }

    @Override
    public void setClientInfo(Properties properties)
    throws SQLClientInfoException {
    // TODO Auto-generated method stub

    }

    @Override
    public String getClientInfo(String name) throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public Properties getClientInfo() throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public Array createArrayOf(String typeName, Object[] elements)
    throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public Struct createStruct(String typeName, Object[] attributes)
    throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    }

    package com.huawei.datasource;

    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;

    import javax.sql.DataSource;

    import com.huawei.utils.DBUtil;

    /**
    * @author Administrator
    * 自定义数据库连接池 必须实现 javax.sql.DataSource接口
    *
    */
    public class SXTDataSource implements DataSource{

    List<SXTConnection> pool = new ArrayList<SXTConnection>();

    @Override
    public PrintWriter getLogWriter() throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {
    // TODO Auto-generated method stub

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {
    // TODO Auto-generated method stub

    }

    @Override
    public int getLoginTimeout() throws SQLException {
    // TODO Auto-generated method stub
    return 0;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
    // TODO Auto-generated method stub
    return false;
    }

    @Override
    public Connection getConnection() throws SQLException {
    // TODO Auto-generated method stub
    //去遍历池子里面的连接
    for(SXTConnection conn:pool){
    if(!conn.isUsed()){ //判断连接是否可用
    conn.setUsed(true); //标记为正在使用
    return conn;
    }
    }
    //创建一个自定义的连接
    SXTConnection connection = new SXTConnection(DBUtil.getConnection());
    connection.setUsed(true);
    pool.add(connection);
    return connection;
    }

    @Override
    public Connection getConnection(String username, String password)
    throws SQLException {
    return null;
    }

    }

  • 相关阅读:
    Hadoop2.2.0 注意事项
    为一个表增加一列,这个列能够自增加1
    商品推荐系统问题
    Android Service服务-(转)
    android实现通知栏消息
    【Android】状态栏通知Notification、NotificationManager详解(转)
    android调用邮件应用发送email
    有关WebView开发问题(转)
    Android开发把项目打包成apk-(转)
    对话框(单选)
  • 原文地址:https://www.cnblogs.com/hwgok/p/5886030.html
Copyright © 2011-2022 走看看