Tomcat5 Configures MySQL JDBC Database Connection Pool
If you just interested in mysql, you can use this short message. If you want to configure the connection pool of other database types, you can also use simple modification parameters.
1, install Tomcat
Refer to the Tomcat for WINDOW installation wizard, it is basically directly installed. Note: Enter the management username and password when installing, this is the username and password used later, remember.
2, install MySQL
The default is installed.
3, use Tomcat's Web Management Application Configure Data Source
Start the Tomcat server, open the browser, enter http: // localhost: 8080 / admin / (where localhost may be a machine's IP or server name), enter the login page of the management interface, please enter the original installation requirement Enter the username and password, log in to the management interface,
Select Resources-Data Sources to enter the configuration data source interface, select Data Source Actions -> Select CREATE New Data Source to enter the configuration details interface, the content is as follows:
JNDI NAME: JDBC / MySQL
Data Source URL: JDBC: mysql: //192.168.0.16/suBRDB
JDBC Driver Class: Org.gjt.mm.mysql.driver
User Name: root
PASSWORD: *********
Max. Active Connections: 4
Max. Idle Connections: 2
Max. Wait for Connection: 500
Validation Query:
The information requested by JNDI Name is required, in which other other can be filled in according to your needs. For example, the content of the Data Source URL may be: jdbc: mysql: // ip or name / databasename, where DatabaseName is your database name, IP is the IP or name of the server where your database is located. Finally click Save-> Commit Change. So your basic data of your data source is half configured.
4, Web.xml and% Tomcat_Home% / conf / Catalina / LocalHost, corresponding to your reference configuration file modification
Navigate through the folder to% tomcat_home / conf, open web.xml, add the following in front of web-app>::
resource-ref>
Note that the content fill in RES-REF-NAME is consistent with the JNDI Name name mentioned above.
Navigate through the folder to% tomcat_home / conf / Catalina / localhost, find the .xml file corresponding to your web application, such as root.xml, and add the code under this file:
5, other considerations
Don't forget the JDBC driver mysql-connection-bin.ja must be placed to Tomcat's corresponding directory, your JDBC driver may be high, but as long as you can correspond to the mysql used Yes, because I found that the version of the JDBC driver that does not support 4.0. * Version of the MySQL database, it is recommended to place under the% Tomcat_Home / Common / LIB and the application of Web-INF / LIB. What is the difference between the two? In fact, I will understand that Common / Li is all applications where all applications can be used.
Restart your Tomcat service.
6, write test code
Create a Test.jsp file in the application's directory, the code is as follows:
"http://www.w3.org/tr/rec-html40/strict.dtd">
<% @ Page Import = "java.sql. *"%>
<% @ Page Import = "javax.sql. *"%>
<% @ Page Import = "javax.naming. *"%>
<% @ Page session = "false"%>
<%
Out.print ("My Test Start");
DataSource DS = NULL;
Try {
InitialContext CTX = New InitialContext ();
DS = (DataSource) CTX.lookup ("Java: Comp / Env / JDBC / MySQL");
Connection conn = ds.getConnection ();
Statement Stmt = conn.createstatement ();
// Tip: Users must be a table for the database,
// The database mentioned in the Data Source URL configuration herein is included in the database.
String strsql = "select * from users";
ResultSet RS = Stmt.executeQuery (strsql);
While (rs.next ()) {
Out.print (Rs.getstring (1));
}
Out.print ("My Test ends");
}
Catch (Exception EX) {
Out.print ("Exception Exceptions, information is:" ex.getMessage ());
EX.PrintStackTrace ();
}
%>
hEAD>
body>
html>
operation result:
My test starts 12345678 My test ends because my RS.Getstring (1) is stored in the database 123456787, summary
The configuration process and its simple understanding, these configurations are exempt from the developers to write the pain of the data source to connect the pool, which is also a strong support for the development process.