1. Configure the context. xml file
CopyCode The Code is as follows: <Resource
Name = "JDBC/books" // The reference name can be customized.
Auth = "Container" // specify the manager for datasource Management
Type = "javax. SQL. datasource" // specify the package name
Maxactive = "100" // maximum amount of activity
Maxidle = "30" // maximum empty limit
Maxwait = "10000" // maximum wait time
Username = "sa" // user
Password = "sa" // Password
Driverclassname = "com. Microsoft. JDBC. sqlserver. sqlserverdrier"; // connected Driver Class
Url = "JDBC: sqlserver: // localhost: 1433; databasename =" books "// URL of the Connection
/>
2. Configure the Web. xml file (the content must be the same as the information in 1) Copy codeThe Code is 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. Add an SQL driver File
Copy the driver file to the common \ Lib folder of Tomcat.
4. Add the following code to the class to obtain the data source.
Copy codeThe Code is as follows: // import the package
Import javax. Naming. context;
Import javax. Naming. initialcontext;
Import javax. Naming. namingexception;
Import javax. SQL. datasource;
// Obtain the data source link
Context Ic = new initialcontext ();
Datasource DS = (datesource) IC. Lookup ("Java: COMP/ENV/jdbc/books ");
// Obtain the connection
Connection conn = Ds. getconnection ();
Note: Some Tomcat versions in high versions can omit step 1, while others cannot. If not, an exception is thrown and the driver class cannot be found.