Configure the MySQL Data Source in tomcat5

Source: Internet
Author: User

The following article describes how to correctly configure the MySQL Data Source in Tomcat 5. The following article describes how to correctly configure the MySQL Data Source in Tomcat 5, the following describes the procedure of the document.

1. Configure MySQL environment variables:

Softe version: tomcat (a very useful JSP running platform) 5.0.12/MySQL (the best combination with PHP) 4.1.7/MySQL (the best combination with PHP) _ driver ---

MySQL (best combination with PHP)-connector-java-3.1.4-beta-bin.jar

The MySQL (best combination with PHP) driver can be downloaded from the official website www. MySQL (the best combination with PHP). com

Path:

Tomcat (a very useful JSP running platform) 5 in d: mywebomcat5;

MySQL (the best combination with PHP) in C: Program FilesMySQL (the best combination with PHP) MySQL (the best combination with PHP) Server 4.1

Path (added on the basis of the original ):

 
 
  1. D: mywebomcat5in; d: mywebjdk1.4; d: myweb
  2. Jdk1.4in;
  3. D: mywebomcat5commonlibservlet-api.jar; C: Program Files
  4. MySQL (the best combination with PHP) Server4.1in
  5. CLASSPATH: d: mywebomcat5commonlibservlet-api.jar; d: myweb
  6. Tomcat (a very useful JSP running platform) 5 commonlibjsp (preferred for SUN Enterprise Applications)-api. jar
  7. JAVA_HOME: d: mywebjdk1.4
  8. CATALINA_HOME: d: mywebomcat5

2. Establish a Test Database

Create a forumdb database in MySQL (the best combination with PHP) and create a table member as follows:

 
 
  1. create database forumdb;  
  2. create table member  
  3. (  
  4. id int,  
  5. name varchar(6)  
  6. ); 

Insert the following two test data:

 
 
  1. insert into member values(1,"zhang");  
  2. insert into member values(2,"wang"); 

Now, the database is ready.

Iii. configure server. xml (standardization is getting closer and closer)

Note: My web in d: mywebmyapps

First, put the MySQL Driver (the best combination with PHP) under d: mywebomcat5commonlib ". jar files, such ". the zip file is changed. jar.

Open D: omcat5confserver. xml in a text editor (standardization is getting closer and closer), locate the end mark, and add the following statement before:

 
 
  1. <Context path = "/myapps" docBase = "d: mywebmyapps" debug = "0"
  2. Reloadable = "true">
  3. <Resource name = "jdbc/DBConnection"
  4. Auth = "Container"
  5. Type = "javax. SQL. DataSource"/>
  6. <ResourceParams name = "jdbc/DBConnection">
  7. <Parameter>
  8. <Name> factory </name>
  9. <Value> org. apache (the most popular WEB server platform on the Unix platform). commons. dbcp. BasicDataSourceFactory </value>
  10. </Parameter>
  11. <! -- Maximum number of dB connections in pool. Make sure you
  12. Configure your MySQL (the best combination with PHP) d max_connections large enough to handle
  13. All of your db connections. Set to 0 for no limit.
  14. -->
  15. <Parameter>
  16. <Name> maxActive </name>
  17. <Value> 10 </value>
  18. </Parameter>
  19. <! -- Maximum number of idle dB connections to retain in pool.
  20. Set to 0 for no limit.
  21. -->
  22. <Parameter>
  23. <Name> maxIdle </name>
  24. <Value> 3 </value>
  25. </Parameter>
  26. <! -- Maximum time to wait for a dB connection to become available
  27. In MS, in this example 10 seconds. An Exception is thrown if
  28. This timeout is exceeded. Set to-1 to wait indefinitely.
  29. Maximum time to wait for a dB connection to become available
  30. In MS, in this example 10 seconds. An Exception is thrown if
  31. This timeout is exceeded. Set to-1 to wait indefinitely.
  32. -->
  33. <Parameter>
  34. <Name> maxWait </name>
  35. <Value> 10000 </value>
  36. </Parameter>
  37. <! -- MySQL (the best combination with PHP) dB username and password for dB connections -->
  38. <Parameter>
  39. <Name> username </name>
  40. <Value> root </value>
  41. </Parameter>
  42. <Parameter>
  43. <Name> password </name>
  44. <Value> 1234 </value>
  45. </Parameter>
  46. <! -- Class name for mm. MySQL (the best combination with PHP) JDBC driver -->
  47. <Parameter>
  48. <Name> driverClassName </name>
  49. <Value> com. MySQL (the best combination with PHP). jdbc. Driver </value>
  50. </Parameter>
  51. <! -- The JDBC connection url for connecting to your MySQL (The best combination with PHP) dB.
  52. The autoReconnect = true argument to the url makes sure that
  53. Mm. MySQL (the best combination with PHP) JDBC Driver will automatically reconnect if MySQL (the best combination with PHP) d closed
  54. Connection. MySQL (the best combination with PHP) d by default closes idle connections after 8 hours.
  55. -->
  56. <Parameter>
  57. <Name> url </name>
  58. <Value> jdbc: MySQL (the best combination with PHP): // localhost: 3306/forumdb? AutoReconnect = true </value>
  59. </Parameter>
  60. </ResourceParams>
  61. </Context>

Reminder again: Be sure to put it before!

4. Configure web. xml (standardization is getting closer and closer)

Web. xml (standardization is getting closer and closer) is located under d: mywebmydomainweb-INF, that is, WEB. xml in your web (standardization is getting closer and closer ).

Use a text editor to open web. xml (standardization is getting closer and closer), and then add the following statement between and)

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

5. Compile the test jsp page (preferred for SUN Enterprise applications.

Write a dbtest. jsp file under d: mywebmyapps (preferred for SUN Enterprise Applications). The Code is as follows:

 
 
  1. <%@ page import="java.io.*,java.util.*,java.sql.*,javax.sql.*,javax.naming.*"%> 
  2. <%@ page contentType="text/html;charset=GB2312"%> 
  3. <body> 
  4. <%  
  5. try{  
  6. java.sql.Connection con;  
  7. Statement stmt;  
  8. ResultSet rs;  
  9. Context ctx = new InitialContext();  
  10. DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/DBConnection");  
  11. con=ds.getConnection();  
  12. stmt=con.createStatement();  
  13. rs=stmt.executeQuery("select * from member");  
  14. while(rs.next()){  
  15. out.print(rs.getInt(1);  
  16. out.print(rs.getString(2));  
  17. }  
  18. rs.close();  
  19. stmt.close();  
  20. con.close();  
  21. }catch(Exception e){  
  22. out.print(e.getMessage());  
  23. }  
  24. %> 
  25. </body> 

6. Start testing

Run tomcat (a good JSP running platform ). bat, open IE and enter: http: // localhost: 8080/myapps/dbtest in the address bar. jsp (preferred for SUN Enterprise applications)

If the following data is displayed, the MySQL data source is successfully configured.

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.