1. Configure Context.xml Files
Copy Code code as follows:
<resource
Name= "Jdbc/books"//reference name can be customized
Auth= "Container"//Specify Manager for admin datasource
Type= "Javax.sql.DataSource"//Specify Package name
Maxactive= "100"//maximum active capacity
maxidle= "30"//Maximum empty limit
Maxwait= "10000"//MAX Wait time
Username= "sa"/user
password= "sa"//password
Driverclassname= "Com.microsoft.jdbc.sqlserver.SQLServerDrier"; Connected Drive Classes
Url= "jdbc:sqlserver://localhost:1433;databasename="//Connection URL
/>
2 Configure the Web.xml file (the content must be the same as the information in 1)
Copy Code code as follows:
<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 Adding SQL driver files
Copy the driver files to Tomcat's Common\lib folder
4 Add the following code to the class to get the data source
Copy Code code as follows:
Import Package
Import Javax.naming.Context;
Import Javax.naming.InitialContext;
Import javax.naming.NamingException;
Import Javax.sql.DataSource;
Get a link to a data source
Context IC = new InitialContext ();
DataSource ds = (datesource) ic.lookup ("Java:comp/env/jdbc/books");
Get connection
Connection conn = Ds.getconnection ();
Note: Some of the higher versions of Tomcat can omit step 2nd, others cannot, and if they cannot, throw an exception and not find a driver class