Correct configuration of java jsp tomcat6 MySQL connection pool

Source: Internet
Author: User
Tags mysql connection pool

The following articles mainly describe the correct configuration of the java jsp tomcat6 MySQL connection pool. Many people use Tomcat 5 to configure the java jsp tomcat6 MySQL connection pool, however, I personally think it is too time-consuming to use Tomcat 5. Now I will share the following:

1. required file: Installation (Installation File)

2. Configure the context. xml file under conf under tomcat, and add the java jsp tomcat6 MySQL connection pool between <context> </context> as follows:

 
 
  1. <Resource name="jdbc/MySQL"   
  2. auth="Container"   
  3. type="javax.sql.DataSource"   
  4. driverClassName="com.MySQL.jdbc.Driver"   
  5. url="jdbc:MySQL://localhost/test"   
  6. username="root"   
  7. password="root"   
  8. maxActive="100"   
  9. maxIdle="30"   
  10. maxWait="10000" />   
  11.  

I don't need to talk about the above parameters. I know what it means.

3. Configure <web-app> </web-app> in the web. xml file of your application to add:

 
 
  1. Xml Code <resource-ref>
  2. <Description> DB Connection </description>
  3. <Res-ref-name> jdbc/MySQLx </res-ref-name>
  4. <Res-type> javax. SQL. DataSource </res-type>
  5. <Res-auth> Container </res-auth>
  6. </Resource-ref>

4. the success is that you do not need to use the original server. after the configuration is in xml, you can write the test program below. There will be a lot on the internet, mainly on the top. Of course, you need to put the connection drivers under the lib under Tomcat 6. the test code is as follows:

 
 
  1. Java code <! Doctype html public "-// w3c // dtd html 4.0 transitional // en"
  2. Http://www.w3.org/TR/REC-html40/strict.dtd>
  3. <% @ Page import = "java. SQL. *" %>
  4. <% @ Page import = "javax. SQL. *" %>
  5. <% @ Page import = "javax. naming. *" %>
  6. <% @ Page session = "false" %>
  7. <Html>
  8. <Head>
  9. <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
  10. <Title> </title>
  11. <%
  12. Out. print ("My test started ");
  13. DataSource ds = null;
  14. Try {
  15. InitialContext ctx = new InitialContext ();
  16. Ds = (DataSource) ctx. lookup ("java: comp/env/jdbc/MySQL ");
  17. Connection conn = ds. getConnection ();
  18. Statement stmt = conn. createStatement ();

Tip: users must be an existing database table,

The database contained in the Data Source URL configuration mentioned above.

 
 
  1. String strSql = "select * from users ";
  2. ResultSet rs = stmt.exe cuteQuery (strSql );
  3. While (rs. next ()){
  4. Out. print (rs. getString (1 ));
  5. }
  6. Out. print ("My test ended ");
  7. }
  8. Catch (Exception ex ){
  9. Out. print ("exception, message:" + ex. getMessage ());
  10. Ex. printStackTrace ();
  11. }
  12. %>
  13. </Head>
  14. <Body>
  15. </Body>
  16. </Html>

I have tested the above. If you have any questions, please leave a message. The above content is an introduction to the configuration of the java jsp tomcat6 MySQL connection pool. I hope you will gain some benefits.


 

Related Article

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.