人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

Tomcatc3p0配置jnid數(shù)據(jù)源2種實(shí)現(xiàn)方法解析

發(fā)布日期:2022-01-14 08:48 | 文章來源:站長(zhǎng)之家

使用c3p0

導(dǎo)入c3p0jar包

<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
  <dependency>
   <groupId>com.mchange</groupId>
   <artifactId>c3p0</artifactId>
   <version>0.9.5.2</version>
  </dependency>

在tomcat的context.xml文件加入數(shù)據(jù)源配置

<Resource 
    auth="Container" 
    description="DB Connection" 
    driverClass="com.mysql.jdbc.Driver" 
    maxPoolSize="100" minPoolSize="2" 
    acquireIncrement="2" 
    name="jdbc/myDB" 
    user="root" 
    password="123456" 
    factory="org.apache.naming.factory.BeanFactory" 
    type="com.mchange.v2.c3p0.ComboPooledDataSource" 
    jdbcUrl="jdbc:mysql://localhost:3306/attendance_system?characterEncoding=utf8&amp;serverTimezone=GMT%2B8" />

獲取連接

protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    try {
      //創(chuàng)建上下文
      Context context=new InitialContext();
      //獲取數(shù)據(jù)源
      ComboPooledDataSource comboPooledDataSource= (ComboPooledDataSource) context.lookup
          ("java:comp/env/jdbc/myDB");
      //獲取數(shù)據(jù)庫(kù)連接
      Connection connection=comboPooledDataSource.getConnection();
      
      if(!connection.isClosed()){
        System.out.println("已經(jīng)連接成功");
      }
    } catch (NamingException e) {
      e.printStackTrace();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

使用druid

導(dǎo)入jar包

 <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.1.16</version>
  </dependency>

在tomcat的context.xml文件加入數(shù)據(jù)源配置

<Resource
  name="jdbc/MysqlDataSource"
  factory="com.alibaba.druid.pool.DruidDataSourceFactory"
  auth="Container"
  type="javax.sql.DataSource"
  driverClassName="com.mysql.cj.jdbc.Driver"
  url="jdbc:mysql://localhost:3306/yl?characterEncoding=utf8&amp;serverTimezone=GMT%2B8"
  username="root"
  password="123456"
  maxActive="50"
  maxWait="10000"
  removeabandoned="true"
  removeabandonedtimeout="60"
  logabandoned="false"
  filters="stat"/>

獲取連接

protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    try {
      //獲取上下文對(duì)象
      Context context=new InitialContext();
      //獲取數(shù)據(jù)源
      DataSource ds= (DataSource) context.lookup("java:comp/env/jdbc/MysqlDataSource");
      //獲取Connection對(duì)象
      Connection connection=ds.getConnection();
​
      if(!connection.isClosed()){
        System.out.println("連接成功");
​
      }
    } catch (NamingException e) {
      e.printStackTrace();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開通

自選配置、實(shí)時(shí)開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對(duì)1客戶咨詢顧問

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部