jdbc結(jié)合dpcp連接池的封裝實(shí)例
更新時間:2017年10月27日 08:37:34 作者:葉灬黎
下面小編就為大家?guī)硪黄猨dbc結(jié)合dpcp連接池的封裝實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
demo需求:
實(shí)現(xiàn)jdbc結(jié)合dpcp連接池的封裝(以oracle數(shù)據(jù)庫為例)并實(shí)現(xiàn)簡單地查找demo主要技術(shù):
(1)Properties類加載.properties的方式 (2)dpcp連接池建立數(shù)據(jù)庫連接的方式 (3)查詢數(shù)據(jù)的方式 (4)靜態(tài)代碼塊的使用,分離驅(qū)動的加載和連接信息的載入,整個服務(wù)器生命周期只執(zhí)行一次
demo所用jar包:
classes12.jar commons-dbcp-1.4.jar commons-pool-1.5.4.jar
demo主要代碼展示:
Utils.java private static Connection conn = null; private static BasicDataSource dataSource = new BasicDataSource(); private static Properties prop = getProperties("src/db.properties"); // 將連接池的創(chuàng)建放在靜態(tài)代碼塊,保證整個服務(wù)器生命周期只執(zhí)行一次,減少服務(wù)器負(fù)擔(dān) static { try { dataSource.setDriverClassName(prop.getProperty("driver")); dataSource.setUrl(prop.getProperty("url")); dataSource.setUsername(prop.getProperty("user")); dataSource.setPassword(prop.getProperty("password")); dataSource.setMaxActive(20); dataSource.setInitialSize(10); } catch (Exception e) { System.out.println("連接池創(chuàng)建失敗"); } } /** * show 方法簡介 獲取數(shù)據(jù)庫連接池的連接,因?yàn)橐呀?jīng)封裝,以后只需要配置db.properties,無需動這邊代碼 * * @author 葉灬黎 * @return */ public static Connection getConnection() { try { conn = dataSource.getConnection(); } catch (Exception e) { System.out.println("數(shù)據(jù)庫連接失敗"); } return conn; } /** * show 方法簡介 讀取.properties文件,這里主要服務(wù)于想將jdbc連接數(shù)據(jù)庫的各項(xiàng)參數(shù)抽取出來 * * @author 葉灬黎 * @param file * 要讀取的.properties文件的路徑 * @return Properties類對象 */ private static Properties getProperties(String file) { Properties properties = new Properties(); try { FileInputStream fis = new FileInputStream(new File(file)); properties.load(fis); fis.close(); } catch (IOException e) { System.out.println("加載配置文件出錯"); } return properties; }
OneSelect.java public static void main(String[] args) { List<String> names = new ArrayList<>(); try { Connection conn = Utils.getConnection(); //創(chuàng)建執(zhí)行引擎 Statement state = conn.createStatement(); //執(zhí)行sql String sql = "select * from emp"; ResultSet rs = state.executeQuery(sql); while(rs.next()){ names.add(rs.getString("ename")); } rs.close(); state.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } for(String s : names){ System.out.println(s); } }
db.properties(src目錄下) driver = oracle.jdbc.driver.OracleDriver url = jdbc:oracle:thin:@127.0.0.1:1521:orcl user = scott password = 123456
demo資源位置:
svn://106.15.229.200/Javaweb/tinyDemo_jdbc 用戶temp/密碼temp)
以上這篇jdbc結(jié)合dpcp連接池的封裝實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot Actuator的指標(biāo)監(jiān)控可視化功能詳解
SpringBoot Actuator是springboot為簡化我們對微服務(wù)項(xiàng)目的監(jiān)控功能抽取出來的模塊,使得我們每個微服務(wù)快速引用即可獲得生產(chǎn)界別的應(yīng)用監(jiān)控、審計等功能。這篇文章主要介紹了springboot Actuator的指標(biāo)監(jiān)控可視化,需要的朋友可以參考下2021-08-08使用easyexcel導(dǎo)出的excel文件,使用poi讀取時異常處理方案
這篇文章主要介紹了使用easyexcel導(dǎo)出的excel文件,使用poi讀取時異常處理方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12Java?C++題解leetcode764最大加號標(biāo)志示例
這篇文章主要為大家介紹了Java?C++題解leetcode764最大加號標(biāo)志示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01SpringBoot關(guān)于自定義注解實(shí)現(xiàn)接口冪等性方式
這篇文章主要介紹了SpringBoot關(guān)于自定義注解實(shí)現(xiàn)接口冪等性方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11SpringBoot Swagger2 接口規(guī)范示例詳解
Swagger(在谷歌、IBM、微軟等公司的支持下)做了一個公共的文檔風(fēng)格來填補(bǔ)上述問題,在本文中,我們將會學(xué)習(xí)怎么使用Swagger的 Swagger2注解去生成REST API文檔,感興趣的朋友一起看看吧2023-12-12