Using c3p0 Import the c3p0jar package <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> Add data source configuration to the context.xml file of tomcat <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&serverTimezone=GMT%2B8" /> Get connected protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //Create contextContext context=new InitialContext(); //Get the data source ComboPooledDataSource comboPooledDataSource= (ComboPooledDataSource) context.lookup ("java:comp/env/jdbc/myDB"); //Get database connection Connection connection=comboPooledDataSource.getConnection(); if(!connection.isClosed()){ System.out.println("Connected successfully"); } } catch (NamingException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } Using druid Import jar package <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.16</version> </dependency> Add data source configuration to the context.xml file of tomcat <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&serverTimezone=GMT%2B8" username="root" password="123456" maxActive="50" maxWait="10000" removeabandoned="true" removeabandonedtimeout="60" logabandoned="false" filters="stat"/> Get connected 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) context.lookup("java:comp/env/jdbc/MysqlDataSource"); //Get the Connection object Connection connection=ds.getConnection(); if(!connection.isClosed()){ System.out.println("Connection successful"); } } catch (NamingException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Sample code using the element calendar component in Vue
>>: Detailed explanation of the correct use of the count function in MySQL
The specific method of installing CentOS 7.0 on V...
The tee command is mainly used to output to stand...
It has been three or four months since I joined Wo...
Table of contents The first The second Native Js ...
Preface Many friends who have just come into cont...
This article shares the MySQL installation tutori...
We usually use float layout to solve the compatib...
Docker installation 1. Requirements: Linux kernel...
tomcat server configuration When everyone is lear...
Problem Description The MySQL startup error messa...
This article mainly introduces how to build a MyS...
Today I got familiar with the mouse zooming effect...
Preface In the process of continuous code deliver...
Core code -- Below I will demonstrate the impleme...
Table of contents Preface Basic Concepts of Argum...