SoFunction
Updated on 2025-04-07

Configuration of JSP Servlet Data Source Connection Pool

1. Configuration file
Copy the codeThe code is as follows:

<Resource
name="jdbc/books" //The reference name can be customized
auth="Container" //Specify the Manager that manages DataSource
type="" //Specify the package name
maxActive="100" //Maximum activity
maxIdle="30" //Maximum empty limit
maxWait="10000" //Maximum waiting time
username="sa" //User
password="sa" //Password
driverClassName=""; //Connected driver class
url="jdbc:sqlserver://localhost:1433;DatabaseName="books" //Connection URL
/>

2 configuration file (the content must be the same as the information in 1)
Copy the codeThe code is as follows:

<resource-ref>
<discription>E-books DataSource</discription>
<res-ref-name>jdbc/books</res-ref-name>
<res-type></res-type>
<res-auth>Container</res-auth>
</resource-ref>

3Add Sql driver file
Copy the driver file to Tomcat's common\lib folder
4 Add the following code to the class to get the data source
Copy the codeThe code is as follows:

//Import package
import ;
import ;
import ;
import ;
//Get the link to the data source
Context ic = new InitialContext();
DataSource ds = (DateSource)("java:comp/env/jdbc/books");
//Get Connection
Connection conn = ();

Note: Some of the higher versions of Tomcat can omit step 2, while others cannot. If they cannot, an exception will be raised and the driver class cannot be found.