Tomcat+sql Server2000 Connection Pool configuration

Source: Internet
Author: User
Tags auth connect sql resource stmt tostring tomcat tomcat server
Server|server2000|sql finally solved the problem of many days of connection pool, write down this article to share with you. I am configured under the tomcat5.5.9, tomcat5.5.x and previous versions have some differences, so the configuration is also different. I'll just say the basic steps in tomcat5.5.9 configuration: (Make sure you install tomcat5.5.9, sql2000)
  
   1. Copy database JDBC driver to%tomcat_home%/common/lib and%tomcat_home%/webapps/yourweb/web-inf/lib (my WEB folder name is QuickStart)
  

This step is known to all, otherwise Tomcat cannot connect to the database.
  
   2, modify the%tomcat_home%/conf/server.xml file, between <GlobalNamingResources></GlobalNamingResources>
  

Add the following:
<resource
Name= "Jdbc/quickstart"
Type= "Javax.sql.DataSource"
Password= "123456"
Driverclassname= "Com.microsoft.jdbc.sqlserver.SQLServerDriver"
Maxidle= "2"
maxwait= "5000"
Username= "SA"
Url= "Jdbc:microsoft:sqlserver://localhost;databasename=quickstart" maxactive= "4"/>
  
The above content according to everyone's specific situation to modify accordingly, for example: Name= "Jdbc/quickstart" in the QuickStart to and your Web folder name, the password= "123456" in the "123456" to your database password.
  
   3. Create a new XML file with the same name as your Web folder under%tomcat_home%/conf/catalina/localhost (Mine is quickstart.xml)
  

This step is very important, if you do not have this step will be wrong, will appear org.apache.tomcat.dbcp.dbcp.SQLNestedException:Cannot create JDBC driver of class ' for connect URL ' null '
  
Such an error, add the 2nd step in the file, as follows:
  
<?xml version= "1.0" encoding= "UTF-8"?><context>
<resource
Name= "Jdbc/quickstart"
Type= "Javax.sql.DataSource"
Password= "123456"
Driverclassname= "Com.microsoft.jdbc.sqlserver.SQLServerDriver"
Maxidle= "2"
maxwait= "5000"
Username= "SA"
Url= "Jdbc:microsoft:sqlserver://localhost;databasename=quickstart"
Maxactive= "4"/> </Context>
  
According to the specific circumstances of the individual change accordingly.
  
   4, modify the Web.xml file under%tomcat_home%/webapps/yourweb/web-inf, add the following content between <web-app></web-app>
  

<resource-ref>
<description>sqlserverdb connection</description>
<res-ref-name>jdbc/quickstart</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
  
Change the QuickStart in <res-ref-name>jdbc/quickstart</res-ref-name> to your Web folder name.
  
   5. Test, create a new index.jsp file under QuickStart
  

<%@ page contenttype= "text/html; charset=gb2312 "%><%@ page import=" javax.naming.* "%><%@ page import=" javax.sql.* "%><%@ page import= "Java.sql.*"%>Connection Conn=null;
Statement Stmt=null;
ResultSet Rs=null;
ResultSetMetaData Md=null;
Try
{
Context Initctx=new InitialContext ();
DataSource ds= (DataSource) initctx.lookup ("Java:comp/env/jdbc/quickstart");
if (ds!=null)
{
Out.println ("Already obtained DataSource");
Out.println (Ds.tostring ());
Conn=ds.getconnection ();
Stmt=conn.createstatement ();
Out.println ("AA");
Rs=stmt.executequery ("SELECT * from Cat");
Md=rs.getmetadata ();
Out.println ("<table border=1>");
Out.println ("<tr>");
for (int i=0;i<md.getcolumncount (); i++)
{
Out.println ("<td>" +md.getcolumnname (i+1) + "</td>");
}
while (Rs.next ())
{
Out.println ("<tr>");
Out.println ("<td>" +rs.getstring (1) + "</td>");
Out.println ("<td>" +rs.getstring (2) + "</td>");
Out.println ("<td>" +rs.getstring (3) + "</td>");
Out.println ("<td>" +rs.getstring (4) + "</td>");
Out.println ("</tr>");
}
Out.println ("</table>");
Conn.close ();
}
}
catch (Exception e)
{
Out.println (E.tostring ());
System.out.println (E.tostring ());
}%></body>   
Run the Tomcat server and enter http://localhost:8080/quickstart/index.jsp in the browser to see if it succeeded.

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.