06 dbcp 連接池的使用

使用前提:導(dǎo)包

方式1:

public class DbcpTest {
    private static String url = "jdbc:mysql://localhost:3306/employee_db";
    private static String user = "root";
    private static String password = "221121";
    private static String driverClass = "com.mysql.jdbc.Driver"; 
    
    static PreparedStatement st = null; 
    static ResultSet rs = null;
    public static void main(String[] args) throws SQLException {
        //創(chuàng)建連接池對象
        BasicDataSource bds = new BasicDataSource();
        
        //設(shè)置連接參數(shù)
        bds.setUrl(url);
        bds.setUsername(user);
        bds.setPassword(password);
        bds.setDriverClassName(driverClass);
        
        //設(shè)置連接池參數(shù)
        bds.setInitialSize(5);//初始化連接個(gè)數(shù)
        bds.setMaxActive(10);//最大連接個(gè)數(shù)
        bds.setMaxWait(3000);//當(dāng)超過最大連接數(shù)的等候時(shí)間
        
        try{
            Connection conn = bds.getConnection();
            st = conn.prepareStatement("select * from employee");
            rs = st.executeQuery();
            while(rs.next()){
                int id = rs.getInt(1);
                String name = rs.getNString("name");
                System.out.println(id+":"+name);
            }
                
        }catch (Exception e){
            e.printStackTrace();
        } finally{
            bds.close();//把連接對象放回連接池R
        }
    }

}

方式2:
配置文件

public class DbcpTest2 {
    
    static PreparedStatement st = null; 
    static ResultSet rs = null;
    public static void main(String[] args) throws SQLException {
        try {
            //1)使用工廠類來創(chuàng)建dbcp連接池對象(讀取配置文件方式)
            Properties prop = new Properties();
            //使用類路徑讀取配置文件
            InputStream in = DbcpTest2.class.getResourceAsStream("/jdbc.properties");
            //加載配置文件
            prop.load(in);

            BasicDataSource bds = (BasicDataSource)BasicDataSourceFactory.createDataSource(prop);
            /**
             * 讀取jdbc.properties文件內(nèi)容
             *   dbcp可以自動識別每個(gè)配置信息,但是約定前提: 配置文件的key名稱和設(shè)置方法的名稱保持一致!?。?             */
            
            Connection conn = bds.getConnection();
            st = conn.prepareStatement("select * from employee");
            rs = st.executeQuery();
            while(rs.next()){
                int id = rs.getInt(1);
                String name = rs.getNString("name");
                System.out.println(id+":"+name);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,590評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,112評論 25 709
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,275評論 6 342
  • Ubuntu的發(fā)音 Ubuntu,源于非洲祖魯人和科薩人的語言,發(fā)作 oo-boon-too 的音。了解發(fā)音是有意...
    螢火蟲de夢閱讀 100,716評論 9 468
  • 你的名字有簡單的筆畫,卻是縈繞在我思緒里最復(fù)雜的心事。 你的名字就像春天里和煦的風(fēng)。拂過大地就染紅了鮮花點(diǎn)綠...
    水裕田閱讀 767評論 0 1

友情鏈接更多精彩內(nèi)容