Tomcat8 configures the database connection pool and tomcat8 database connection
1,All tomcat projects share one connection pool configuration.
1.1 modify the conf-> context. xml file and configure it under the Context node.
<Resource
Name = "jdbc/myDataSource"
Type = "javax. SQL. DataSource"
DriverClassName = "com. microsoft. sqlserver. jdbc. SQLServerDriver"
MaxIdle = "2"
MaxWait = "5000"
Username = "sa"
Password = "xx"
Url = "jdbc: sqlserver: // 127.0.0.1 \ SQLEXPRESS: 1433; DatabaseName = ShopSystem"
MaxActive = "4"/>
Note:
<! -- Name: DataSource name -->
<! -- Type: the java type corresponding to the data source. It is generally designed as javax. SQL. DataSource -->
<! -- Username: Database login name -->
<! -- Password: Database login password -->
<! -- DriverClassName: Specifies the database JDBC driver -->
<! -- Url: Specifies the URL of the database -->
<! -- MaxIdle: Maximum number of database connections in idle state in the connection pool. If this parameter is set to 0, no restriction is imposed. -->
<! -- MaxWait: the maximum time (in milliseconds) for idle database connections in the connection pool. The value 0 indicates unlimited waiting time. -->
<! -- MaxActive: Maximum number of database connections in the active state of the connection pool. Removing 0 indicates no restriction. -->
1.2 modify tomcat's web. xml file
Configure under the web-app node:
<Resource-ref>
<Res-ref-name> jdbc/myDataSource </res-ref-name>
<Res-type> javax. SQL. DataSource </res-type>
<Res-auth> Container </res-auth>
</Resource-ref>
2. Each Web project is configured independently with its own connection pool
2.1 create the context. xml file in the META-INF directory of the Web project with the following content:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Context path = "/">
<Resource
Name = "jdbc/myDS"
Type = "javax. SQL. DataSource"
DriverClassName = "com. microsoft. sqlserver. jdbc. SQLServerDriver"
MaxIdle = "2"
MaxWait = "5000"
Username = "sa"
Password = "xx"
Url = "jdbc: sqlserver: // 127.0.0.1 \ SQLEXPRESS: 1433; DatabaseName = ShopSystem"
MaxActive = "4"/>
</Context>
2.2 modify the Web. xml file under the WEB-INF directory of the web Project
Configure under the web-app node:
<Resource-ref>
<Res-ref-name> jdbc/myDS </res-ref-name>
<Res-type> javax. SQL. DataSource </res-type>
<Res-auth> Container </res-auth>
</Resource-ref>
3. No matter which connection pool is used, copy the database driver jar file to the tomcat lib directory.
4. Test Procedures
<%
Context initContext = new InitialContext ();
Context envContext = (Context) initContext. lookup ("java: comp/env ");
DataSource ds = (DataSource) envContext. lookup ("jdbc/myDataSource ");
Connection connection = ds. getConnection ();
Statement statement = connection. createStatement ();
ResultSet resultSet=statement.exe cuteQuery ("select * from admin ");
While (resultSet. next ()){
Out. print (resultSet. getString ("a_name "));
Out. print ("<br/> ");
}
ResultSet. close ();
Statement. close ();
Connection. close ();
%>