1. Configure the Context. xml file
Copy codeThe 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: In a later version of Tomcat, some can omit step 1, while others cannot. If not, an exception is thrown and the driver class cannot be found.