In the past few days, follow the existing tutorial on the Internet to combine MySQL and tomcat to configure the database connection pool. After a great deal of effort, I finally succeeded. There are two methods to configure the connection pool (Tomcat + MySQL): code configuration and visual configuration.
The visual configuration is convenient and quick. The following describes the configuration process based on my configuration experience.
Version: MySQL-4.19, Tomcat -- 5.5.9.
Before configuring the Tomcat connection pool, extract mysql-connector-java-xxx.zip and put the mysql-connector ctor-Java-3. X. x-xxx.jar in commonlib? Go to http://www.mysql.com/productsto download it.
1. if admin is not installed on Tomcat, install Admin first. After the installation with admin permission login (user management in/CONF/tomcat-user.xml, if there is no admin permission of the user, create a user, or set existing tomcat or both to roles = "admin ");
2. After logging in, you can see the following functions in the selection bar on the left:
-- Tomcat server
-- + Service (Catalina)
-- Resources
-- + Data sources
-- + Mail sessions
-- + Environment entries
-- + User Databases
-- User Definition
-- + Users
-- + Groups
-- + Roles
Click data sources --> data source actions and select create new data source from the drop-down list. Fill in the following options. yxz is the MySQL instance I created and can be modified as needed.
JNDI name: MySQL/yxz
Data source URL: JDBC: mysql: // localhost: 3306/yxz
JDBC Driver Class: COM. MySQL. JDBC. Driver
User name: Root
Password: Root
Max. Active connections: 20 --- maximum number of active connections
Max. idle connections: 10 --- maximum number of idle connections
Max. Wait for connection: 5000
Validation query:
After completion, press save, then Commit Changes, OK!
3. The visual configuration of the database connection pool is complete. However, if you want to use the connection pool under the corresponding application, you still need to configure the corresponding content. xml. Create the content. xml file in yourapp/META-INF/and copy the configuration as follows:
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/yxz"
username="root" password="root"
maxActive="20" maxIdle="10" maxWait="-1" />
Save, OK.
4. Create a table: User in yxz in MySQL for testing. The User table contains the field ID and name.
5. Test the program, create index. jsp under yourapp, and copy the following code: