There are two methods:
1. Simple Preparation Method
Add the following context between the Code
<Context Path = "/test" docbase = "test" DEBUG = "0" reloadable = "true" crosscontext = "true">
<Resource Name ="JDBC/test"Auth =" Container "type =" javax. SQL. datasource "maxactive =" 10 "maxidle =" 5 "maxwait =" 10000 "username =" sa "Password =" Sasa "driverclassname =" net. sourceForge. jtds. JDBC. driver "url =" JDBC: jtds: sqlserver: // localhost: 1433/mydb "/>
</Context>
2. officially recommended methods
First, configure the context. xml file under conf under tomcat, and add the connection pool as follows:
<Resource Name ="JDBC/test"Auth =" Container "type =" javax. SQL. datasource "maxactive =" 10 "maxidle =" 5 "maxwait =" 10000 "username =" sa "Password =" Sasa "driverclassname =" net. sourceForge. jtds. JDBC. driver "url =" JDBC: jtds: sqlserver: // localhost: 1433/test "/>
Add the following content to the Web. xml file of your application:
<Resource-ref>
<Description> dB connection </description>
<Res-ref-Name>JDBC/test</RES-ref-Name>
<Res-type> javax. SQL. datasource </RES-type>
<Res-auth> container </RES-auth>
</Resource-ref>
-----------------------------------------------------------
Test JSP page code:
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
<% @ Page import = "Java. SQL. *" %>
<% @ Page import = "javax. SQL. *" %>
<% @ Page import = "javax. Naming. *" %>
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
%>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Base href = "<% = basepath %>">
<Title> tomcat7 connection pool configuration test </title>
<Meta http-equiv = "Pragma" content = "no-Cache">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "Description" content = "this is my page">
</Head>
<Body>
<%
Try {
Datasource DS = NULL;
Initialcontext CTX = new initialcontext ();
DS = (datasource) CTX. Lookup ("Java: COMP/ENV/ JDBC/test ");
Connection conn = Ds. getconnection ();
Statement stmt = conn. createstatement ();
String strsql = "select * from T1 ";
Resultset rs = stmt.exe cutequery (strsql );
While (Rs. Next ()){
Out. println (Rs. getstring (1) + "..." + Rs. getstring (2) + "</BR> ");
}
Stmt. Close ();
Rs. Close ();
Conn. Close ();
} Catch (sqlexception SE ){
Out. Print ("exception:" + Se. tostring ());
}
%>
</Body>
</Html>