1. 程式人生 > >JDBC程式設計學習筆記之資料庫連線池的實現

JDBC程式設計學習筆記之資料庫連線池的實現

在JDBC程式設計的時候,獲取到一個數據庫連線資源是很寶貴的,倘若資料庫訪問量超大,而資料庫連線資源又沒能得到及時的釋放,就會導致系統的崩潰甚至宕機。造成的損失將會是巨大的。再看有了資料庫連線池的JDBC,就會較好的解決資源的建立與連線問題,其主要還是針對於連線資源使用層面的改進。下面我就談一談我對資料庫連線池的理解。

資料庫連線池理論基礎

對於建立一個數據庫連線池,需要做好準備工作。原理就是先實現DataSource介面,覆蓋裡面的getConnection()方法,這個方法是我們最為關注的。既然是池,就不會是一個連線物件,因此需要使用集合來儲存這些個連線物件。
但是,最為關鍵的是,開發人員在使用完連線物件後通常會呼叫conn.close(),方法來釋放資料庫連線資源,這將會把資料庫連線返還給資料庫,而不是資料庫連線池,因此,資料庫連線池的存在就沒了意義了。所以我們要增強close方法,來保證資料庫連線資源返還給資料庫連線池。

建立資料庫連線池

public class JDCBCPool implements DataSource {

    /*
     * 既然是一個數據庫連線池,就必須有許多的連線,所以需要使用一個集合類儲存這些連線 (non-Javadoc)
     * 
     * @see javax.sql.CommonDataSource#getLogWriter()
     */
    private static  LinkedList<Connection> list = new LinkedList<Connection>();

    // 建立一個配置檔案,用於讀取相應配置檔案中儲存的資料資訊
private static Properties config = new Properties(); /* * 在這裡向資料庫申請一批資料庫連線 為了只加載一次驅動程式,因此在靜態程式碼塊中進行宣告,這樣驅動就只會載入一次 */ static { try { config.load(JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties")); // Class.forName("com.mysql.jdbc.Driver");
Class.forName(config.getProperty("DRIVER")); try { //申請十個資料庫連線物件,也就是資料庫連線池的容量是10 for(int i=0 ;i<10;i++){ Connection conn = DriverManager.getConnection(config.getProperty("URL"), config.getProperty("USER"),config.getProperty("PASSWORD")); list.add(conn); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (ClassNotFoundException | IOException e) { // TODO Auto-generated catch block throw new ExceptionInInitializerError(); } } @Override public Connection getConnection() throws SQLException { if(list.size()<=0){ throw new RuntimeException("資料庫忙,請待會再試試吧!"); } //需要注意的是,不能使用get方式(這個方法知識返回一個引用而已), //應該在獲取的同時,刪除掉這個連結,之後再還回來.現在注意是返還給資料庫連線池!!! Connection conn = list.removeFirst(); MyConnection myconn = new MyConnection(conn); //從這裡開始返回的就是一個數據庫連線池物件的conn return myconn; } /////////////////////////////////////////////////////////////////////////datasource介面的實現方法開始 @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 Logger getParentLogger() throws SQLFeatureNotSupportedException { // TODO Auto-generated method stub return null; } @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(String username, String password) throws SQLException { // TODO Auto-generated method stub return null; } }

不難看出,資料庫連線池相對於普通的資料庫連線的建立,並沒有什麼特別難寫的部分。

增強close方法,保證資料庫連線資源用完後返還給連線池

要想增強close方法的功能,一般來說我們有如下幾種方式。

  • 建立子類,覆蓋close方法,但是建立子類的時候要實現的部分可能會特別多,因此並不常用
  • 使用包裝設計模式
  • 使用動態代理方式

今天我就來實現一下怎麼使用包裝設計模式來實現close方法的增強。

包裝設計模式實現close方法的增強

要實現包裝設計模式,思路很簡單。

  • 建立一個類,實現與被增強物件相同的介面
  • 將被增強物件作為一個成員變數加到這個類中
  • 定義一個構造方法,並把被增強物件作為引數傳進來
  • 覆蓋要增強的方法,這裡是close方法
  • 對於不想進行增強的方法,藉助於被增強物件實現即可。

下面是基於包裝設計模式實現的增強的MyConnection類:

class MyConnection implements Connection {

        private Connection conn ;

        public MyConnection(Connection conn ){
            this.conn = conn;
        }

        /**
         * 自定義的包裝設計模式類,增強close方法,
         * 將資料庫連結資源返還給資料庫連線池,而不是資料庫
         */
        public void close(){
            list.add(conn);
        }

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

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

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

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

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

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

        @Override
        public void setAutoCommit(boolean autoCommit) throws SQLException {
            // TODO Auto-generated method stub
            this.conn.setAutoCommit(autoCommit);
        }

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

        @Override
        public void commit() throws SQLException {
            // TODO Auto-generated method stub
            this.conn.commit();
        }

        @Override
        public void rollback() throws SQLException {
            // TODO Auto-generated method stub
            this.conn.rollback();
        }

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

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

        @Override
        public void setReadOnly(boolean readOnly) throws SQLException {
            // TODO Auto-generated method stub
            this.conn.setReadOnly(readOnly);
        }

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

        @Override
        public void setCatalog(String catalog) throws SQLException {
            // TODO Auto-generated method stub
            this.conn.setCatalog(catalog);
        }

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

        @Override
        public void setTransactionIsolation(int level) throws SQLException {
            // TODO Auto-generated method stub
            this.conn.setTransactionIsolation(level);
        }

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

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

        @Override
        public void clearWarnings() throws SQLException {
            // TODO Auto-generated method stub
            this.conn.clearWarnings();
        }

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

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

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

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

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

        @Override
        public void setHoldability(int holdability) throws SQLException {
            // TODO Auto-generated method stub
            this.conn.setHoldability(holdability);
        }

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

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

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

        @Override
        public void rollback(Savepoint savepoint) throws SQLException {
            // TODO Auto-generated method stub
            this.conn.rollback(savepoint);
        }

        @Override
        public void releaseSavepoint(Savepoint savepoint) throws SQLException {
            // TODO Auto-generated method stub
            this.conn.releaseSavepoint(savepoint);
        }

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

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

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

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

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

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

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

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

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

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

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

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

        @Override
        public void setClientInfo(Properties properties) throws SQLClientInfoException {
            // TODO Auto-generated method stub
            this.conn.setClientInfo(properties);
        }

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

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

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

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

        @Override
        public void setSchema(String schema) throws SQLException {
            // TODO Auto-generated method stub
            this.conn.setSchema(schema);
        }

        @Override
        public String getSchema() throws SQLException {
            // TODO Auto-generated method stub
            return this.conn.getSchema();
        }

        @Override
        public void abort(Executor executor) throws SQLException {
            // TODO Auto-generated method stub
            this.conn.abort(executor);
        }

        @Override
        public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
            // TODO Auto-generated method stub
            this.conn.setNetworkTimeout(executor, milliseconds);
        }

        @Override
        public int getNetworkTimeout() throws SQLException {
            // TODO Auto-generated method stub
            return this.conn.getNetworkTimeout();
        }



    }

一個簡單的資料庫連線池的實現案例

說是一個案例,其實也可以作為一個工具類作為今後程式開發的使用,這將會大大的提升資料庫連線資源的使用。需要注意的是,裡面的驅動程式是基於我自己的資料庫來書寫的,進行使用時記得修改一下配置檔案db.properties.檔案中的內容即可。
db.properties中內容如下:

#when use this db.properties ,need to change the database name
DRIVER = com.mysql.jdbc.Driver
URL = jdbc:mysql://localhost:3306/test
USER = root
PASSWORD = mysql

下面就是JDBCPool工具類的實現:

package utils;

import java.io.IOException;
import java.io.PrintWriter;
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.DriverManager;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.logging.Logger;

import javax.sql.DataSource;

/**
 * 資料庫連線池工具類
 * 
 * @author Summer
 *
 */
public class JDCBCPool implements DataSource {

    /*
     * 既然是一個數據庫連線池,就必須有許多的連線,所以需要使用一個集合類儲存這些連線 (non-Javadoc)
     * 
     * @see javax.sql.CommonDataSource#getLogWriter()
     */
    private static  LinkedList<Connection> list = new LinkedList<Connection>();

    // 建立一個配置檔案,用於讀取相應配置檔案中儲存的資料資訊
    private static Properties config = new Properties();

    /*
     * 在這裡向資料庫申請一批資料庫連線 為了只加載一次驅動程式,因此在靜態程式碼塊中進行宣告,這樣驅動就只會載入一次
     */
    static {

        try {
            config.load(JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties"));
            // Class.forName("com.mysql.jdbc.Driver");
            Class.forName(config.getProperty("DRIVER"));
            try {
                //申請十個資料庫連線物件,也就是資料庫連線池的容量是10
                for(int i=0 ;i<10;i++){
                    Connection conn =  DriverManager.getConnection(config.getProperty("URL"),
                            config.getProperty("USER"),config.getProperty("PASSWORD"));
                    list.add(conn);
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (ClassNotFoundException | IOException e) {
            // TODO Auto-generated catch block
            throw new ExceptionInInitializerError();
        }
    }


    @Override
    public Connection getConnection() throws SQLException {

        if(list.size()<=0){
            throw new RuntimeException("資料庫忙,請待會再試試吧!");
        }
        //需要注意的是,不能使用get方式(這個方法知識返回一個引用而已),
        //應該在獲取的同時,刪除掉這個連結,之後再還回來.現在注意是返還給資料庫連線池!!!
        Connection conn = list.removeFirst();
        MyConnection myconn = new MyConnection(conn);

        //從這裡開始返回的就是一個數據庫連線池物件的conn
        return myconn;
    }



/////////////////////////////////////////////////////////////////////////datasource介面的實現方法開始    

    @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 Logger getParentLogger() throws SQLFeatureNotSupportedException {
        // TODO Auto-generated method stub
        return null;
    }

    @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(String username, String password) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }
/////////////////////////////////////////////////////////////////////////datasource介面的實現方法結束

    /**
     * 包裝設計模式實現流程:
     * 1.建立一個類,實現與被增強物件相同的介面
     * 2.將被增強物件當做自定義類的一個成員變數
     * 3.定義一個構造方法,將被增強物件傳遞進去
     * 4.增強想要增強的方法,進行覆蓋即可
     * 5.對於不像被增強的方法,呼叫被增強物件的方法進行處理即可
     * @author Summer
     *
     */

///////////////////////////////////////////////////////////////////////使用包裝設計模式,增強close方法的自定義類開始
    class MyConnection implements Connection {

        private Connection conn ;

        public MyConnection(Connection conn ){
            this.conn = conn;
        }

        /**
         * 自定義的包裝設計模式類,增強close方法,
         * 將資料庫連結資源返還給資料庫連線池,而不是資料庫
         */
        public void close(){
            list.add(conn);
        }

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

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

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

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

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

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

        @Override
        public void setAutoCommit(boolean autoCommit) throws SQLException {
            // TODO Auto-generated method stub
            this.conn.setAutoCommit(autoCommit);
        }

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

        @Override
        public void commit() throws SQLException {
            // TODO Auto-generated method stub
            this.conn.commit();
        }

        @Override
        public void rollback() throws SQLException {
            // TODO Auto-generated method stub
            this.conn.rollback();
        }

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

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

        @Override
        public void setReadOnly(boolean readOnly) throws SQLException {
            // TODO Auto-generated method stub
            this.conn.setReadOnly(readOnly);
        }

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

        @Override
        public void setCatalog(String catalog) throws SQLException {
            // TODO Auto-generated method stub
            this.conn.setCatalog(catalog);
        }

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

        @Override
        public void setTransactionIsolation(int level) throws SQLException {
            // TODO Auto-generated method stub
            this.conn.setTransactionIsolation(level);
        }

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

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

        @Override
        public void clearWarnings() throws SQLException {
            // TODO Auto-generated method stub
            this.conn.clearWarnings();
        }

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

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

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

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

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

        @Override
        public void setHoldability(int holdability) throws SQLException {
            // TODO Auto-generated method stub
            this.conn.setHoldability(holdability);
        }

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

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

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

        @Override
        public void rollback(Savepoint savepoint) throws SQLException {
            // TODO Auto-generated method stub
            this.conn.rollback(savepoint);
        }

        @Override
        public void releaseSavepoint(Savepoint savepoint) throws SQLException {
            // TODO Auto-generated method stub
            this.conn.releaseSavepoint(savepoint);
        }

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

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

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

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

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

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

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

        @Override
        public Blob createBlob() 
            
           

相關推薦

JDBC程式設計學習筆記資料庫連線實現

在JDBC程式設計的時候,獲取到一個數據庫連線資源是很寶貴的,倘若資料庫訪問量超大,而資料庫連線資源又沒能得到及時的釋放,就會導致系統的崩潰甚至宕機。造成的損失將會是巨大的。再看有了資料庫連線池的JDBC,就會較好的解決資源的建立與連線問題,其主要還是針對於連線

JavaWeb 15.JDBC提高(事物和資料庫連線)

## 事務和資料庫連線池 ## **事務**          1.什麼是事務:一組邏輯上的操作,要麼全都成功,要麼全都失敗。     2.模擬事務的操作     &nb

JDBC 資料庫連線

資料庫連線池 在使用開發基於資料庫的web程式時,傳統的模式基本是按以下步驟:   –在主程式(如servlet、beans)中建立資料庫連線。 –進行sql操作 –斷開資料庫連線。   這種模式開發,存在的問題: –普通的JDBC資料庫連線使用 Driv

java學習筆記jsp連線資料庫的小型留言板專案

思路:編寫一個開始的靜態網頁,在這個網頁裡面提交form表單中的內容。然後開始java程式碼的編寫。這部分的java程式碼編寫和CS模式中java程式碼的編寫是一樣的。將其分為三個包:domain、persistence還有service。 以上是這個專案所有的東西。 d

併發程式設計學習筆記顯示鎖(十一)

ReentrantLock(重進入鎖)並不是作為內部鎖(synchronized)機制的替代,而是當內部鎖被證明受到侷限時,提供可選擇的高階特性. 1. Lock 和 ReentrantLock Lock介面: public interface Lock { void lock();

Spring Boot學習筆記使用Spring Boots實現資料庫操作(IntelliJ IDEA+navicat for Sql Server)

這裡使用Spring Boot實現了一個簡單的專案,能夠實現簡單的資料庫操作,工具使用的是IntelliJ IDEA2017.3,資料庫工具使用的是navicat for Sql Server,語言使用的Java。 1.新建一個空的Maven專案 2.匯入需要的jar包 pom.xml:

併發程式設計學習筆記構建自定義的同步工具(十一)

概述: 在併發程式設計學習筆記之併發工具類(四)中,為大家介紹了幾種同步工具(同步工具就是依靠自己的狀態,調節執行緒是阻塞還是執行用的.),閉鎖、FutureTask、訊號量、關卡. 使用以上的同步工具大部分時候可以滿足我們的需求,但是如果沒能滿足我們需要的功能,可以使用語言和類庫提供的底層

SpringBoot初始教程資料庫連線(druid)

SpringBoot初始教程之資料庫連線池(druid) 1.介紹 Druid是一個JDBC元件庫,包括資料庫連線池、SQL Parser等元件。DruidDataSource是最好的資料庫連線池。SpringBoot支援任何一種資料庫連結池的配置,在這裡用druid作為例子進行講解 2

併發程式設計學習筆記原子變數與非阻塞同步機制(十二)

概述 java.util.concurrent包中的許多類,比如Semaphore和ConcurrentLinkedQueue,都提供了比使用Synchronized更好的效能和可伸縮性.這是因為它們的內部實現使用了原子變數和非阻塞的同步機制. 近年來很多關於併發演算法的研究都聚焦在非阻塞演算法(nonb

JavaScript高階程式設計學習筆記事件

1、事件流 事件流描述的是從頁面中接收事件的順序。 事件冒泡 IE的事件流叫做事件冒泡(event bubbling),即事件開始時由最具體的元素(文件中巢狀層次最深的那個節點)接收,然後逐級向上傳播到較為不具體的節點(文件)。(DOM樹向上傳播)(通俗解釋(個人理解: 當一個元素上的事件被觸發的時候,比如

Mysql學習--07.c3p0資料庫連線

學習目標 C3p0資料庫連線池 一、c3p0資料庫連線池          1、概述: c3p0是一個開源的JDBC連線池、它實現了資料來源和JNDI(Java Naming and Directory Interface,Java命名和目錄介面)繫結、支援jdbc3

Java學習路程資料庫連線, common-dbutils和資料來源

一.資料庫連線 1.封裝資料庫連線 public class Day28{ public static void main(String[] args) throws SQLException { //查詢sort全表並且將查詢出來的每條資料封裝成一個物件

python資料庫連線.md

帶著問題做學問 之前只是聽說過資料庫連線池,但是還沒有真正的使用過。專案之初,只是簡單的利用pymysql做資料庫的一些操作,隨著專案的進行發現程式程序還在,但資料庫的連線已被關閉(超過一定時間後,資料庫連線會自動關閉),帶著這個問題,通過諮詢和查詢資料,於是有

併發程式設計學習筆記可伸縮性(九)

很多改進效能的技術增加了複雜度,因此增加了安全和活躍度失敗的可能性. 更糟糕的是,有些技術的目的是改善效能,事實上產生了相反的作用,帶來了其他的效能問題. 資料的正確性永遠是第一位的,保證程式是正確的,然後再讓它更快.只有當你的效能需求和評估標準需要程式執行得更快時,才去進行改進. 在設計併發應用程式的時候,

Qt資料庫資料庫連線

在前面的章節裡,我們使用了下面的函式建立和取得資料庫連線: void createConnectionByName(const QString &connectionName) { QSqlDatabase db = QSqlDatabase::addDatabase("QMYS

JDBC第四篇--【資料庫連線、DbUtils框架、分頁】

1.資料庫連線池 什麼是資料庫連線池 簡單來說:資料庫連線池就是提供連線的。 為什麼我們要使用資料庫連線池 資料庫的連線的建立和關閉是非常消耗資源的 頻繁地開啟、關閉連線造成系統性能低下 編寫連線池 編寫連線池需實現java.sql

JDBC資料庫連線實現原理(手動實現)

一、普通的資料庫連線     如下圖所示,個使用者獲取資料庫資料都要單獨建立一個jdbc連線,當用戶獲取資料完成後再將連線釋放,可見對cpu的資源消耗很大。  二

mysql筆記五——資料庫連線(原理、構建)和java動態代理的使用

資料庫連線池 1、什麼是資料庫連線池?       資料庫連線池負責分配、管理和釋放資料庫連線,它允許應用程式重複使用一個現有的資料庫連線,而不是再重新建立一個;釋放空閒時間超過最大空閒時間的資料庫連線來避免因為沒有釋放資料庫連線而引起的資料庫連線遺漏。這項

Android程式設計學習筆記 File資料儲存

File檔案可用來存放大量資料,如文字、圖片、音訊、視訊等。 在Android的資料儲存操作和Java中的IO流差不多的用法。 進行File資料儲存的步驟如下: ①開啟一個File物件 ②開啟一個FileOutputStream檔案輸出流,寫入資料 ③開啟一個FileIn

程式設計學習筆記Java相關vector向量的介紹

在Java中,有一個包叫java.util,它是一個儲存著各種常用工具類的類庫,其中就包括向量(vector)。向量是一種類似陣列的順序儲存的資料結構,但是它的功能比陣列強大的多。比如,Vector類的物件是允許不同型別大小的元素共存的變長陣列,Vector類的物件不但可以