1. First make sure that the \common\lib (for Tomcat5.5) or the \lib (Tomcat6.0) folder in the Tomcat installation folder contains the three. jar files that are necessary for the JDBC Connection database (Msbase.jar, The Mssqlserver.jar and MSUTIL.JAR,JDBC drives include these three files, just copy the three files under the Lib folder installed in the JDBC drive to the Lib folder of the Tomcat installation folder.
2. Change the Server.xml and context.xml files in the Conf subfolder of the Tomcat installation folder.
for the Server.xml file, add the following content to the <GlobalNamingResources> element:
<resource name=" database name "
auth=" Container "
type= "Javax.sql.DataSource"
username= "sa"
password= "123"
driverclassname= "Com.microsoft.jdbc.sqlserver.SQLServerDriver"
maxidle= "
maxwait= "10000"
maxactive= "+"
url= "Jdbc:microsoft:sqlserver://localhost:1433;databasename=test1"
/>
Note: Test1 is a well-established database.
The meanings of each field are as follows:
Name: Define the names of the database connections Driverclassname: Specifies the class of the JDBC Drive
Username: User name used when logging into database password: password to login database
Maxidle: Maximum spare time for database connections maxwait: Indicates maximum connection wait time
Maxactive: Maximum number of database connections for a connection pool URL: Represents the address and name of the database that needs to be connected
For the context.xml file, add for example the following in the <Context> element:
<resourcelink global= "database name" Name= "Jdbc/testdb" type= "Javax.sql.DataSource"/>
Two database names should be consistent
3. See a. JSP document to test
Content such as the following,
I built it here under the C:\apache-tomcat-6.0.39\webapps\zhanglixuan application.
So you need to change the C:\apache-tomcat-6.0.39\webapps\zhanglixuan\WEB-INF\web.xml document in this app
Add between <web-app> tags
<resource-ref>
<description>sqlserverdb connection</description>
<res-ref-name>jdbcTestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
Then replace the following JSP code with the original index.jsp, and visit this JSP to
After the configuration is complete, you can use the following files for example to test whether the connection pool configuration is correct.
<%@ page language= "java" contenttype= "text/html; charset=gb2312 "
pageencoding= "gb2312"%>
<%@ page import= "java.sql.*,javax.naming.*"%>
<%
try{
Context initctx=new InitialContext ();
Context ctx= (context) Initctx.lookup ("java:comp/env");
Get Connection Pool Object
Object Obj= (Object) ctx.lookup ("Jdbc/testdb");
Type conversions
Javax.sql.DataSource ds= (javax.sql.DataSource) obj;
Get the database connection object from the connection pool
Connection conn=ds.getconnection ();
Statement stmt=conn.createstatement ();
Gets the server-side time that the SQL statement applies to SQL Server
String strsql= "Select GetDate ()";
ResultSet rs=stmt.executequery (strSQL);
Rs.next ();
Date date=rs.getdate (1);
Out.println (Date.tostring ());
Rs.close ();
Stmt.close ();
Conn.close ();
}
catch (Exception e) {
Out.println (e);
}
%>
The sample test program obtains the current time of the server of MS SQL Server through the database connection pool, and the execution results are as follows, indicating the connection pool configuration is successful.
Tomcat Configuration SQL Server database