1, install Tomcat
After the JDK is installed, refer to the Tomcat for Window's installation wizard, basically directly installed, Note: Enter the management user name and password when installing, this is the username and password used in the future, remember.
2, install mysql
The default is installed.
3, (a) Configuring data sources using Tomcat's Web Management Application
Start the Tomcat server, open the browser, enter http: // localhost: 8080 / admin / (where localhost is the name server or the host), enters the management interface login page, at this time, please enter the user name that is required to enter when installing. Password, log in to the management interface,
Select Resources-Data Source to enter the configuration data source interface, select Data Source Actions -> Select Create New Data Source to enter the configuration details interface, the content is as follows: JNDI Name: JDBC / XXX (XXX is the name of yourself)
Data Source URL: JDBC: MySQL: // localhost / test (connection data name)
JDBC Driver Class: com.mysql.jdbc.driver
User name: User name to connect to the database
Password: Database password
Max. Active Connections: Maximum connection
Max. Idle Connections: is the maximum number of idle connections
Max. Wait for Connection: Maximum Waiting Connection
JDBC / MYSQLDATA SOURCE URL: JDBC: mysql: // localhost: 3306 / cs jdbc driver class: com.mysql.jdbc.driver odser name: rootpassword: ******** Max. Active Connections: 4max. IDle Connections: 2max. Wait for Connection: 5000Validation Query:
Request input
JNDI Name
And other information, among them
JDBC Driverclass
Other, other can be filled in according to your needs. such as
Data Source URL
The content may be:
JDBC: MySQL: // ip
Or name
/ DatabaseName
,among them
DatabaseName
Is your database name,
IP
Is your database of the server
IP
Or name. Finally click
Save
-
> Commit change.
This is half the basic data of your data source.
(b)
By modifying the configuration file
Configuring data sources
Add:
Open% tomcat_home% / conf / web.xml, add the following in front of web-app>::
resource-ref>
Note that the content fill in RES-REF-NAME is consistent with the JNDI Name name mentioned above.
Enter% Tomcat_Home% / Conf / Catalina / LocalHost, find your web application corresponding to the .xml file (below with my web directory structure), such as MyWeb.xml, and add the code under this file:
Here, the configuration work is basically completed.
5, other considerations
Don't forget the JDBC driver mysql-connection-bin.ja must be placed to Tomcat's corresponding directory, your JDBC driver may be high, but as long as you can correspond to the mysql used Yes, because I found that the version of the JDBC driver that does not support 4.0. * Version of the MySQL database, it is recommended to place under the% Tomcat_Home / Common / LIB and the application of Web-INF / LIB. What is the difference between the two? In fact, I will understand that Common / lib is all applications that can be used by all applications.
Restart your Tomcat service.
6, write test code
Create a Test.jsp file in the application's directory, the code is as follows:
"http://www.w3.org/tr/rec-html40/strict.dtd">
<% @ Page Import = "java.sql. *"%>
<% @ Page Import = "javax.sql. *"%>
<% @ Page Import = "javax.naming. *"%>
<% @ Page session = "false"%>
<%
Out.print ("My Test Start");
DataSource DS = NULL;
Try {
InitialContext CTX = New InitialContext ();
DS = (DataSource) CTX.lookup ("Java: Comp / Env / JDBC / MySQL");
Connection conn = ds.getConnection ();
Statement Stmt = conn.createstatement ();
// Tip: Users must be a table for the database,
// The database mentioned in the Data Source URL configuration herein is included in the database.
String strsql = "select * from users";
ResultSet RS = Stmt.executeQuery (strsql);
While (rs.next ()) {
Out.print (Rs.getstring (1));
}
Out.print ("My Test ends");
}
Catch (Exception EX) {
Out.print ("Exception Exceptions, information is:" ex.getMessage ());
EX.PrintStackTrace ();
}
%>
hEAD>
body>
html>
operation result:
My test starts 12345678 My test ends because my RS.GetString (1) is stored in the database 12345678
7, summary
Thank Jemlee2002 Note: Bean under H: / WebSpp / MyWeb / Web-INF / CLASSES
H: / WebSpp / MyWeb / Web-INF / with Web.xml and 8 TLD files. Mysql's drive is placed in% Tomcat_Home% / COMMON / LIB / Under% Tomcat_Home% / Conf / Catalina / LocalHost / Crete MyWeb.xml, as shown below XML Version = '1.0' Encoding = 'UTF-8'?>
Context>