Put the MySQL driver package in my: e:/tomcat 6.0/lib and the corresponding project WEB-INF/lib folder.
1. Modify CONF/server. xml and add the following content (some parameters ):
<Context Path = "/Web" docbase = "Web" DEBUG = "0" reloadable = "true" crosscontext = "true">
<Resource Name = "JDBC/query" auth = "Container" type = "javax. SQL. datasource" driverclassname = "com. MySQL. JDBC. Driver"
Url = "JDBC: mysql: // localhost: 3306/databasename"
Username = "XX" Password = "XX"
Maxidle = "10" maxwait = "500" maxactive = "200" removeabandoned = "true" removeabandonedtimeout = "60" logabandoned = "true"/>
</Context>
2. Modify the Web. xml of the corresponding application and add the following content before </Web-app>:
<Resourcelink name = "JDBC/query" Global = "JDBC/query" type = "javax. SQL. Cer CER"/>
Restart tomcat. OK!
Test the connection retrieval function as follows:
<% @ Page contenttype = "text/html; charset = UTF-8" %>
<% @ Page import = "javax. Naming. Context" %>
<% @ Page import = "javax. SQL. datasource" %>
<% @ Page import = "javax. Naming. initialcontext" %>
<% @ Page import = "Java. SQL. *" %>
<HTML>
<Body>
123
<%
Datasource DS = NULL;
Try {
Context initctx = new initialcontext ();
Context envctx = (context) initctx. Lookup ("Java: COMP/ENV ");
// Lookup data source from context.
DS = (datasource) envctx. Lookup ("JDBC/query ");
If (Ds! = NULL)
{
Out. println ("datasource obtained! ");
Out. println ("<br> ");
Connection conn = Ds. getconnection ();
Statement stmt = conn. createstatement ();
Resultset rst1_stmt.exe cutequery ("select * from table"); // read data from a table
Out. println ("the following data is read from the database: <br> ");
While (RST. Next ()){
Out. println ("<br> ");
Out. println (RST. getstring (8 ));}
}
}
Catch (exception ne)
{
Out. println ("exception: <br> ");
Out. println (NE );
}
%>
</Body>
</Html>