After more than half a day, I finally cleared the operations for tomcat5.5 to configure the data source, which is actually a few simple steps.
Tomcat5.5 connect to mysql5.0A. find the Tomcat server. XML file. Add the configuration file of the data source before <Resource Name = "JDBC/MySQL"
Auth = "Container"
Type = "javax. SQL. datasource"
Maxactive = "100"
Maxidle = "30"
Maxwait = "10000"
Username = "root"
Password = "hailang43"
Driverclassname = "com. MySQL. JDBC. Driver"
Url = "JDBC: mysql: // localhost: 3306/test? Useunicode = true "/>
</Context> the path and docbase in red are the names of the projects that use the data source. B. Introduce the data source to the Web. xml file in the project directory. The Code is as follows: <resource-ref>
<Res-ref-Name> JDBC/MySQL </RES-ref-Name>
<Res-type> javax. SQL. datasource </RES-type>
<Res-auth> container </RES-auth>
</Resource-ref> note that the code is added before </Web-app>. C. put the MySQL driver in the lib directory of the project, and use a JSP file to verify whether the connection is successful as follows: <% @ page Language = "Java" pageencoding = "UTF-8" %>
<% @ Page import = "Java. SQL. *" %>
<% @ Page import = "javax. SQL. *" %>
<% @ Page import = "javax. Naming. *" %>
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> test of Tomcat connection pool </title>
</Head>
<Body>
<%
Out. Print ("strat test connection pool! <Br/> ");
Try {
Context initctx = new initialcontext ();
Context CTX = (context) initctx. Lookup ("Java: COMP/ENV ");
Object OBJ = (object) CTX. Lookup ("JDBC/MySQL ");
Datasource DS = (javax. SQL. datasource) OBJ;
Connection conn = Ds. getconnection ();
Out. Print ("Tomcat MySQL connection pool runs perfectly! <Br/> ");
Out. Print ("the Conn is:" + conn );
Conn. Close ();
}
Catch (exception ex ){
Out. Print (ex. getmessage ());
Ex. printstacktrace ();
}
%>
</Body>
</Html> if no error is returned, the output is OK.
Code for connecting to SQL2000Web. xml: <resource-env-ref>
<Description> sql2k connection </description>
<Resource-env-ref-Name> JDBC/sql2k </resource-env-ref-Name>
<Resource-env-ref-type> javax. SQL. datasource </resource-env-ref-type>
</Resource-env-ref> server. XML: <context Path = "/sql2k" docbase = "sql2k" reloadable = "true" crosscontext = "true" DEBUG = "0">
<Resource Name = "JDBC/sql2k"
Auth = "Container"
Type = "javax. SQL. datasource"
Maxactive = "100"
Maxidle = "30"
Maxwait = "10000"
Username = "sa"
Password = "hailang43"
Driverclassname = "com. Microsoft. JDBC. sqlserver. sqlserverdriver"
Url = "JDBC: Microsoft: sqlserver: // localhost: 1433"/>
</Context> If useunicode = true is added to the URL, an error is returned. Please note that I do not know why. Test JSP <% @ page Language = "Java" pageencoding = "UTF-8" %>
<% @ Page import = "Java. SQL. *" %>
<% @ Page import = "javax. SQL. *" %>
<% @ Page import = "javax. Naming. *" %>
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> test of Tomcat connection pool </title>
</Head>
<Body>
<%
Out. Print ("strat test connection pool! <Br/> ");
Try {
Context init = new initialcontext ();
Context envcontext = (context) Init. Lookup ("Java:/COMP/ENV ");
Datasource DS = (datasource) envcontext. Lookup ("JDBC/sql2k ");
Connection conn = Ds. getconnection ();
Out. Print ("Tomcat MySQL connection pool runs perfectly! ");
Conn. Close ();
}
Catch (exception ex ){
Out. Print (ex. getmessage ());
Ex. printstacktrace ();
}
%>
</Body>
</Html> OK. It is finished. Hope to help you !!!