Configuration of JSP Servelet data source connection pool

  • 2020-05-07 20:14:15
  • OfStack

1. Configure the Context.xml file

<Resource 
name="jdbc/books" // The reference name can be customized  
auth="Container" // Specify the management DataSource the Manager 
type="javax.sql.DataSource" // Specify the package name  
maxActive="100" // Maximum amount of activity  
maxIdle="30" // Maximum space  
maxWait="10000" // Maximum waiting time  
username="sa" // The user  
password="sa" // password  
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDrier"; // Connected driver classes  
url="jdbc:sqlserver://localhost:1433;DatabaseName="books" // Of the connection URL 
/> 

2 configures the Web.xml file (the contents must be the same as the information in 1)

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

3 adds Sql driver file
Copy the driver file to the common\lib folder of Tomcat
4 adds the following code to the class to get the data source

// Import packages  
import javax.naming.Context; 
import javax.naming.InitialContext; 
import javax.naming.NamingException; 
import javax.sql.DataSource; 
// Gets a link to the data source  
Context ic = new InitialContext(); 
DataSource ds = (DateSource)ic.lookup("java:comp/env/jdbc/books"); 
// To obtain Connection 
Connection conn = ds.getConnection(); 

Note: in higher versions of Tomcat, some may omit step 2, others may not, if not, an exception will be thrown and the driver class will not be found

Related articles: