Tomcat MySQL datasource data source configuration

Source: Internet
Author: User
It seems that the data source configuration of tomcat6 is relatively simple, but if you set it in the previous version, there will be many problems, such:
Name JDBC is not bound in this context
Or cannot create JDBC driver of class ''for connect URL 'null', etc.
I will write down the steps for configuring the data source with atat6 and mysql5.17. You can refer
1. Download The mysql-connector-java-5.1.7-bin.jar and put it in the Lib folder under the tomcat installation directory;
2. Create a context. xml file under the META-INF folder under your project (create one if not, same as the WEB-INF folder;
The content is as follows:
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Context>
  3. <Resource Name = "JDBC/performance_name" auth = "Container"
  4. Type = "javax. SQL. datasource" username = "root" Password = "xxxxxxx"
  5. Driverclassname = "com. MySQL. JDBC. Driver" url = "JDBC: mysql: // localhost/database_name"
  6. Maxactive = "8" maxidle = "4"
  7. Testonborrow = "true"
  8. Validationquery = "select 1"
  9. Timebetweenevictionrunsmillis = "30000"
  10. />
  11. </Context>

3. Replace the above performance_name with your data source name, database_name with your database name, and password with your
4. Check whether your Java code is successful.

  1. Initialcontext initcontext = new initialcontext ();
  2. Datasource DS = (datasource) initcontext. Lookup ("Java: COMP/ENV/jdbc/performance_name ");
  3. Connection conn = Ds. getconnection ();
  4. Statement PS = conn. createstatement ();
  5. Resultset rs = ps.exe cutequery ("select * From table_name ");
  6. While (Rs. Next ())
  7. {
  8. System. Out. println (Rs. getstring (2 ));
  9. }

5. The output information should be displayed.

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.