Java dbcp連接池基本使用方法詳解
1、依賴api的使用
導(dǎo)入jar包
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.7.0</version> </dependency>
導(dǎo)入dbcp.properties配置文件
獲取連接
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
Properties properties=new Properties();
//獲取dbcp配置文件對應(yīng)輸入流
InputStream inputStream=DbcpServlet.class.getClassLoader().getResourceAsStream("dbcp.properties");
//加載dbcp配置文件
properties.load(inputStream);
BasicDataSource basicDataSource=null;
try {
//數(shù)據(jù)源對象
basicDataSource=BasicDataSourceFactory.createDataSource(properties);
//獲取數(shù)據(jù)庫連接
Connection connection=basicDataSource.getConnection();
System.out.println(connection);
} catch (Exception e) {
e.printStackTrace();
}
}
2、依賴tomcat容器的使用
利用jndi機(jī)制實(shí)現(xiàn),jndi(命名及目錄查找接口),將數(shù)據(jù)源連接池的配置信息在容器(Tomcat)實(shí)現(xiàn)配置
具體如何實(shí)現(xiàn)配置
在tomcat的context.xml文件加入數(shù)據(jù)源配置
<Resource
<!--數(shù)據(jù)源名字-->
name="jdbc/news"
<!--驗(yàn)證數(shù)據(jù)源的容器類型-->
auth="Container"
type="javax.sql.DataSource"
<!--最大連接數(shù)據(jù)庫連接對象的數(shù)量100-->
maxActive="100"
<!--最大空閑數(shù)是30-->
maxIdle="30"
<!--最大等待時(shí)間-->
maxWait="10000"
<!--數(shù)據(jù)庫用戶名-->
username="root"
<!--數(shù)據(jù)庫密碼-->
password="123456"
<!--數(shù)據(jù)庫驅(qū)動(dòng)-->
driverClassName="com.mysql.cj.jdbc.Driver"
<!--數(shù)據(jù)庫url-->
url="jdbc:mysql://localhost:3306/yl?characterEncoding=utf8&serverTimezone=GMT%2B8"/>
獲取連接
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
//獲取上下文對象
Context ctx = new InitialContext();
//通過jndi命名服務(wù),找到數(shù)據(jù)源配置
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/news");
//獲取數(shù)據(jù)庫連接
Connection connection = ds.getConnection();
if (!connection.isClosed()) {
System.out.println("連接成功");
}
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java數(shù)據(jù)庫連接池技術(shù)的入門教程
- Java 模擬數(shù)據(jù)庫連接池的實(shí)現(xiàn)代碼
- Java中Druid連接池連接超時(shí)獲取不到連接的解決
- Java數(shù)據(jù)庫連接池連接Oracle過程詳解
- Java數(shù)據(jù)庫連接池c3p0過程解析
- Java使用MySQL實(shí)現(xiàn)連接池代碼實(shí)例
- 如何解決線程太多導(dǎo)致java socket連接池出現(xiàn)的問題
- 淺談常用Java數(shù)據(jù)庫連接池(小結(jié))
- Java FTPClient連接池的實(shí)現(xiàn)
- Java 如何快速實(shí)現(xiàn)一個(gè)連接池
相關(guān)文章
Spring?Boot中獲取request的三種方式及請求過程
這篇文章主要介紹了Spring?Boot當(dāng)中獲取request的三種方式,包括請求過程流程分析及response常用API,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03
JAXB簡介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了JAXB簡介的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Springboot 實(shí)現(xiàn)跨域訪問無需使用jsonp的實(shí)現(xiàn)代碼
這篇文章主要介紹了Springboot 實(shí)現(xiàn)跨域訪問 無需使用jsonp的實(shí)現(xiàn)代碼,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09
Java?使用geotools讀取tiff數(shù)據(jù)的示例代碼
這篇文章主要介紹了Java?通過geotools讀取tiff,一般對于tiff數(shù)據(jù)的讀取,都會(huì)借助于gdal,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
Spring實(shí)戰(zhàn)之獲取方法返回值操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之獲取方法返回值操作,涉及spring配置文件與方法返回值操作相關(guān)使用技巧,需要的朋友可以參考下2019-12-12

