java使用dbcp2資料庫連接池
在開發中我們常會使用到資料庫連線池,例如dbcp資料庫連線池,本章將講解java連接dbcp資料庫連線池的簡單使用。
開發工具myeclipse2014
1、先建立一個web項目,我把專案名稱取名為testjdbc,需要web.xml的設定文件,進行servlet的配置,建立完成以後的專案架構如下:
2、建立套件,我建立的套件名稱是com.szkingdom.db
3、建立幫助類別CastUtil,程式碼如下:
package com.szkingdom.db; /** * Created by jack on 2015/12/26. * 转型操作工具类 */ public class CastUtil { /* * 转为String型 * */ public static String castString(Object obj) { return CastUtil.castString(obj, ""); } /* * 转为String型(提供默认值) * */ public static String castString(Object obj, String defaultValue) { return obj != null ? String.valueOf(obj) : defaultValue; } /* * 转为double型 * */ public static double castDouble(Object obj) { return castDouble(obj, (double)0); } /* * 转为double型(提供默认值) * */ public static double castDouble(Object obj, Double defaultValue) { double doubleValue = defaultValue; if (obj != null) { String strValue = castString(obj); if (StringUtil.isNotEmpty(strValue)) { try { doubleValue = Double.parseDouble(strValue); } catch (NumberFormatException e) { defaultValue = defaultValue; } } } return doubleValue; } /* * 转为long型 * */ public static long castLong(Object obj) { return castLong(obj, 0); } /* * 转为long型(提供默认值) * */ public static long castLong(Object obj, long defaultValue) { long longValue = defaultValue; if (obj != null) { String strValue = castString(obj); if (StringUtil.isNotEmpty(strValue)) { try { longValue = Long.parseLong(strValue); }catch (NumberFormatException e){ longValue=defaultValue; } } } return longValue; } /* * 转为int型 * */ public static int castInt(Object obj){ return castInt(obj,0); } /* * 转为int型(提供默值) * */ public static int castInt(Object obj,int defaultValue){ int intValue=defaultValue; if (obj!=null){ String strValue=castString(obj); if(StringUtil.isNotEmpty(strValue)){ try { intValue=Integer.parseInt(strValue); }catch (NumberFormatException e){ intValue=defaultValue; } } } return intValue; } /* * 转为boolean型 * */ public static boolean castBoolean(Object obj){ return castBoolean(obj,false); } /* * 转为boolean型(提供默认值) * */ public static boolean castBoolean(Object obj,boolean defaultValue){ boolean booleanValue=defaultValue; if(obj!=null){ booleanValue=Boolean.parseBoolean(castString(obj)); } return booleanValue; } }
4、建立屬性檔案讀取說明:
package com.szkingdom.db; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * Created by jack on 2015/12/26. * 属性文件工具类 */ public class PropsUtil { //private static final Logger LOGGER = LoggerFactory.getLogger(PropsUtil.class); /* * 加载属性文件 * * */ public static Properties loadProps(String fileName) { Properties properties = null; InputStream inputStream = null; try { inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); if (inputStream == null) { throw new FileNotFoundException(fileName + " file is not found!"); } properties = new Properties(); properties.load(inputStream); } catch (IOException e) { //LOGGER.error("load properties file failure", e); System.out.println("load properties file failure:"+e); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { //LOGGER.error("close input stream failure", e); System.out.println("close input stream failure:"+e); } } } return properties; } /* * 获取字符型属性(默认为空字符串) * * */ public static String getString(Properties props, String key) { return getString(props, key, ""); } /* * 获取字符型属性(可指定默认值) * */ public static String getString(Properties props, String key, String defaultValue) { String value = defaultValue; if (props.containsKey(key)) { value = props.getProperty(key); } return value; } /* * 获取数值类型属性(默认为0) * */ public static int getInt(Properties props, String key) { return getInt(props, key, 0); } /* * 获取数值类型属性(可指定默认值) * */ public static int getInt(Properties props, String key, int defaultValue) { int value = defaultValue; if (props.containsKey(key)) { value = CastUtil.castInt(props.getProperty(key)); } return value; } /* * 获取布尔型属性(默认值为false) * */ public static boolean getBoolean(Properties props, String key) { return getBoolean(props, key, false); } /* * 获取布尔型属性(可指定默认值) * */ public static boolean getBoolean(Properties props, String key, Boolean defaultValue) { boolean value = defaultValue; if (props.containsKey(key)) { value = CastUtil.castBoolean(props.getProperty(key)); } return value; } }
5、建立一個字串幫助類StringUtil,程式碼如下:
package com.szkingdom.db; /** * Created by jack on 2015/12/26. * 字符串工具类 */ public class StringUtil { /* * 判断字符串是否为空 * */ public static boolean isEmpty(String str){ if(str != null){ str=str.trim(); } //return StringUtils.isEmpty(str); return "".equals(str); } /* * 判断字符串是否非空 * */ public static boolean isNotEmpty(String str){ return !isEmpty(str); } }
6、在src目錄下建立一個資料庫連線的屬性檔
6、在src目錄下建立一個資料庫連接的屬性檔把必備的jar包放到lib目錄下:
8、使用dbcp創建資料庫幫助類
<span style="color:#333333;">jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://</span><span style="color:#ff6666;background-color: rgb(255, 0, 0);">127.0.0.1:3306/****</span><span style="color:#333333;"> jdbc.username=**** jdbc.password=****</span>
9、基本的資料庫連接池就可以完成了,之後就可以透過DatabaseHelper的updateHelper的update方法來建立模擬取得資料庫連線進行資料庫的操作,可依自己的需求進行資料的操作。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持PHP中文網。
更多java使用dbcp2資料庫連接池相關文章請關注PHP中文網!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

在使用IntelliJIDEAUltimate版本啟動Spring...

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...

Redis緩存方案如何實現產品排行榜列表的需求?在開發過程中,我們常常需要處理排行榜的需求,例如展示一個�...
