SoFunction
Updated on 2025-03-08

Analysis of two implementation methods for configuration jnid data source tomcatc3p0

Using c3p0

Import c3p0jar package

<!-- /artifact//c3p0 -->
  <dependency>
   <groupId></groupId>
   <artifactId>c3p0</artifactId>
   <version>0.9.5.2</version>
  </dependency>

Add data source configuration in tomcat file

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

Get the connection

protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    try {
      //Create a context      Context context=new InitialContext();
      //Get the data source      ComboPooledDataSource comboPooledDataSource= (ComboPooledDataSource) 
          ("java:comp/env/jdbc/myDB");
      //Get the database connection      Connection connection=();
      
      if(!()){
        ("Connected successfully");
      }
    } catch (NamingException e) {
      ();
    } catch (SQLException e) {
      ();
    }
  }

Using druid

Import jar package

 <!-- /artifact//druid -->
  <dependency>
   <groupId></groupId>
   <artifactId>druid</artifactId>
   <version>1.1.16</version>
  </dependency>

Add data source configuration in tomcat file

<Resource
  name="jdbc/MysqlDataSource"
  factory=""
  auth="Container"
  type=""
  driverClassName=""
  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"/>

Get the connection

protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    try {
      //Get the context object      Context context=new InitialContext();
      //Get the data source      DataSource ds= (DataSource) ("java:comp/env/jdbc/MysqlDataSource");
      //Get Connection object      Connection connection=();
​
      if(!()){
        ("Connected successfully");
​
      }
    } catch (NamingException e) {
      ();
    } catch (SQLException e) {
      ();
    }
  }

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.