Tomcat Configuration SQL Server database

Source: Internet
Author: User
Tags auth

1. First make sure that the \common\lib (for Tomcat5.5) or the \lib (Tomcat6.0) directory in the Tomcat installation directory contains the three. jar files that are necessary for the JDBC Connection database (Msbase.jar, These three files are included in the Mssqlserver.jar and MSUTIL.JAR,JDBC drives by simply copying the three files in the Lib directory after the JDBC drive is installed to the Lib directory of the Tomcat installation directory.

2. Modify the Server.xml and Context.xml files in the Conf subdirectory of the Tomcat installation directory.

for the Server.xml file, add the following 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 fields have the following meanings:

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 idle time for database connection 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 the following to 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

The contents are as follows

I built it here under the C:\apache-tomcat-6.0.39\webapps\zhanglixuan application.

So we need to revise the C:\apache-tomcat-6.0.39\webapps\zhanglixuan\WEB-INF\web.xml document under this application.

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 access the JSP to

After the configuration is complete, you can use the following files to test the connection pool configuration correctly.

<%@ 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
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 instance tester obtains the current time of the server on which Ms Server is located through the database connection pool, and the result is as follows, indicating that the connection pool configuration was successful.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.