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 ):
- D: mywebomcat5in; d: mywebjdk1.4; d: myweb
- Jdk1.4in;
- D: mywebomcat5commonlibservlet-api.jar; C: Program Files
- MySQL (the best combination with PHP) Server4.1in
- CLASSPATH: d: mywebomcat5commonlibservlet-api.jar; d: myweb
- Tomcat (a very useful JSP running platform) 5 commonlibjsp (preferred for SUN Enterprise Applications)-api. jar
- JAVA_HOME: d: mywebjdk1.4
- 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:
- create database forumdb;
- create table member
- (
- id int,
- name varchar(6)
- );
Insert the following two test data:
- insert into member values(1,"zhang");
- 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:
- <Context path = "/myapps" docBase = "d: mywebmyapps" debug = "0"
- Reloadable = "true">
- <Resource name = "jdbc/DBConnection"
- Auth = "Container"
- Type = "javax. SQL. DataSource"/>
- <ResourceParams name = "jdbc/DBConnection">
- <Parameter>
- <Name> factory </name>
- <Value> org. apache (the most popular WEB server platform on the Unix platform). commons. dbcp. BasicDataSourceFactory </value>
- </Parameter>
- <! -- Maximum number of dB connections in pool. Make sure you
- Configure your MySQL (the best combination with PHP) d max_connections large enough to handle
- All of your db connections. Set to 0 for no limit.
- -->
- <Parameter>
- <Name> maxActive </name>
- <Value> 10 </value>
- </Parameter>
- <! -- Maximum number of idle dB connections to retain in pool.
- Set to 0 for no limit.
- -->
- <Parameter>
- <Name> maxIdle </name>
- <Value> 3 </value>
- </Parameter>
- <! -- Maximum time to wait for a dB connection to become available
- In MS, in this example 10 seconds. An Exception is thrown if
- This timeout is exceeded. Set to-1 to wait indefinitely.
- Maximum time to wait for a dB connection to become available
- In MS, in this example 10 seconds. An Exception is thrown if
- This timeout is exceeded. Set to-1 to wait indefinitely.
- -->
- <Parameter>
- <Name> maxWait </name>
- <Value> 10000 </value>
- </Parameter>
- <! -- MySQL (the best combination with PHP) dB username and password for dB connections -->
- <Parameter>
- <Name> username </name>
- <Value> root </value>
- </Parameter>
- <Parameter>
- <Name> password </name>
- <Value> 1234 </value>
- </Parameter>
- <! -- Class name for mm. MySQL (the best combination with PHP) JDBC driver -->
- <Parameter>
- <Name> driverClassName </name>
- <Value> com. MySQL (the best combination with PHP). jdbc. Driver </value>
- </Parameter>
- <! -- The JDBC connection url for connecting to your MySQL (The best combination with PHP) dB.
- The autoReconnect = true argument to the url makes sure that
- Mm. MySQL (the best combination with PHP) JDBC Driver will automatically reconnect if MySQL (the best combination with PHP) d closed
- Connection. MySQL (the best combination with PHP) d by default closes idle connections after 8 hours.
- -->
- <Parameter>
- <Name> url </name>
- <Value> jdbc: MySQL (the best combination with PHP): // localhost: 3306/forumdb? AutoReconnect = true </value>
- </Parameter>
- </ResourceParams>
- </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)
- <resource-ref>
- <description>DB Connection</description>
- <res-ref-name>jdbc/DBConnection</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </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:
- <%@ page import="java.io.*,java.util.*,java.sql.*,javax.sql.*,javax.naming.*"%>
- <%@ page contentType="text/html;charset=GB2312"%>
-
-
- <body>
- <%
- try{
- java.sql.Connection con;
- Statement stmt;
- ResultSet rs;
- Context ctx = new InitialContext();
- DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/DBConnection");
- con=ds.getConnection();
- stmt=con.createStatement();
- rs=stmt.executeQuery("select * from member");
- while(rs.next()){
- out.print(rs.getInt(1);
- out.print(rs.getString(2));
- }
- rs.close();
- stmt.close();
- con.close();
- }catch(Exception e){
- out.print(e.getMessage());
- }
- %>
- </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.