Use the TOMCAT connection pool to connect to MySQL

Source: Internet
Author: User

1) Start the Tomcat server, open the browser, and enter http: // localhost: 8080/admin (localhost is the name server or host) to go To the login page of the management interface, in this case, enter the user name and password required during the original installation and log on to the management interface.

2) Select Resources-Data sources to go to the Data Source configuration page. Select Data Source Actions-> Create New Data Source to go to the configuration details page.

The main content is as follows:

 
 
  1. JNDI Name:   ->jdbc/mysql 
  2. Data Source URL  ->jdbc:mysql://localhost:3306/test 
  3. JDBC Driver Class-> org.gjt.mm.mysql.Driver 

3) modify the \ conf \ Catalina \ localhost directory to create an xml file named. xml for your published web application. (For example, testpool. xml) and add the following content:

 
 
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <Context>
  3. <Resource
  4. Name = "jdbc/mysql"
  5. Type = "javax. SQL. DataSource"
  6. Password = "123456"
  7. DriverClassName = "org. gjt. mm. mysql. Driver"
  8. MaxIdle = "2"
  9. MaxWait = "50"
  10. Username = "root"
  11. Url = "jdbc: mysql: // localhost: 3306/test"
  12. MaxActive = "4"/>
  13.  
  14. </Context>
  15. The content is the same as that in conf/server. xml <GlobalNamingResources>
  16. <Resource
  17. Name = "jdbc/mysql"
  18. Type = "javax. SQL. DataSource"
  19. Password = "123456"
  20. DriverClassName = "org. gjt. mm. mysql. Driver"
  21. MaxIdle = "2"
  22. MaxWait = "50"
  23. Username = "root"
  24. Url = "jdbc: mysql: // localhost: 3306/test"
  25. MaxActive = "4"/>
  26. </GlobalNamingResources>

If this step is missing, the following error occurs: Cannot create JDBC driver of class ''for connect URL 'null'

4) modify web. xml

Open % atat_home % \ conf \ web. xml or yourwebapp/web-inf/web. xml and add the following content:

 
 
  1. <resource-ref> 
  2. <description>DB Connection</description> 
  3. <res-ref-name>jdbc/mysql</res-ref-name> 
  4. <res-type>javax.sql.DataSource</res-type> 
  5. <res-auth>Container</res-auth> 
  6. </resource-ref> 


Note that the res-ref-name must be the same as the JNDI Name mentioned above.

Now, the configuration is complete!

5) use "java: comp/env/jdbc/mysql" to reference JNDI ";

Create a file to test. jsp:

 
 
  1. <% @ Page contentType = "text/html; charset = UTF-8" %>
  2. <% @ Page import = "java. SQL. *" %>
  3. <% @ Page import = "javax. SQL. *" %>
  4. <% @ Page import = "javax. naming. *" %>
  5. <Html>
  6. <Head>
  7. <Title> Tomcat connection pool test </title>
  8. </Head>
  9. <Body>
  10. <%
  11. Context ctx = new InitialContext ();
  12. Connection conn = null;
  13. DataSource ds = (DataSource) ctx. lookup ("java: comp/env/jdbc/mysql ");
  14. Conn = ds. getConnection ();
  15. Statement stmt = conn. createStatement (ResultSet. CONCUR_READ_ONLY, ResultSet. CONCUR_UPDATABLE );
  16. ResultSet rs1_stmt.exe cuteQuery ("select * from testexample ");
  17. While (rs. next ()){
  18. Out. println (rs. getInt (1 ));
  19. Out. println (rs. getString (2 ));
  20. Out. println (rs. getString (3 ));
  21. }
  22. Out. println ("database operation successful! ");
  23. Rs. close ();
  24. Stmt. close ();
  25. Conn. close ();
  26. %>
  27. </Body>
  28. </Html>

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.