TOMCAT5 Configure MySQL JDBC database connection pool

Source: Internet
Author: User
Tags auth mysql resource stmt mysql database tomcat tomcat server
mysql| Data | database | Database connection If you are interested in MySQL can copy this essay, if you want to configure other database types of connection pool, you can also make simple modification parameters to use.



1. Install Tomcat

Refer to the Tomcat for Window Installation Wizard, the basic direct installation, note: Installation will be prompted to enter the admin username and password, this is the user name and password will be used in the future, remember.



2, install MySQL

The default installation can be.



3. Configure a data source using Tomcat's Web management application

Start the Tomcat server, open the browser, enter the http://localhost:8080/admin/(where localhost may be the IP or server name of a machine) and enter the landing page of the admin interface, At this time, please enter the original installation requirements of the user name and password, login to the management interface,


Select Resources-data sources to enter the configuration data source interface, select the "User" to choose "Create New Data", enter the configuration details interface, 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:



Requires information such as the Jndi name entered, except for JDBC Driverclass, which 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 your database of the server's IP or name. Finally click Save->commit Change. So your data source has half the basic data configuration.



4, Web.xml and%tomcat_home%\conf\catalina\localhost the configuration file corresponding to your reference modification

Navigate to%tomcat_home%\conf by folder, open Web.xml, and add the following in front of </web-app>:

<resource-ref>

<description>db connection</description>

<res-ref-name>jdbc/mysql</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>



Note that the content to be filled out by res-ref-name is consistent with the Jndi name names mentioned above.

Navigate through the folder to%tomcat_home%\conf\catalina\localhost, find your Web application corresponding. xml file, such as Root.xml, and add code to the bottom of this file:

<resourcelink name= "Jdbc/mysql" global= "Jdbc/mysql" type= "Javax.sql.DataSourcer"/>

Here, the configuration work is basically done.



5, Other matters needing attention

Don't forget the JDBC driver Mysql-connector-java-3.0.9-stable-bin.jar must be placed in the corresponding directory of tomcat, your JDBC driver may be higher than the author, but as long as you can use the MySQL corresponding to the Because I found that the version too low JDBC driver could not support the 4.0.* version of the MySQL database, it is recommended to place it under the%tomcat_home%\common\lib and application Web-inf\lib. What is the difference between the two? As a matter of fact, Common\li is a library file location that all applications can use.

Restart your Tomcat service.





6. Write test code

Create a test.jsp file in the applied directory with the following code:

<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"

"Http://www.w3.org/TR/REC-html40/strict.dtd" >

<%@ page import= "java.sql.*"%>

<%@ page import= "javax.sql.*"%>

<%@ page import= "javax.naming.*"%>

<%@ page session= "false"%>



<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<title></title>

<%

Out.print ("My Test Begins");

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 tables that are already in the database.

The database contained in the data Source URL configuration mentioned above is described here.

String strSQL = "SELECT * from users";

ResultSet rs = stmt.executequery (strSQL);

while (Rs.next ()) {

Out.print (rs.getstring (1));

}

Out.print ("My test is Over");

}

catch (Exception ex) {

Out.print ("An exception occurs, the message is:" +ex.getmessage ());

Ex.printstacktrace ();

}

%>


<body>

</body>




Run Result:

My test started 12345678 my test ended, because my rs.getstring (1) in the database is to store 12345678



7, summary

The configuration process is simple and straightforward, and these configurations exempt developers from the pain of writing a data source connection pool themselves, which is a strong support for the development process.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.