Ten steps to fix the mssql2000 database connection pool in the servlet

Source: Internet
Author: User
Tags add copy microsoft sql server sql server driver sql stmt tomcat
servlet|sql| Data | database | database connection

1. From aspx? Familyid=4f8f2f01-1ed7-4c4d-8f7b-3d47969e66ae&displaylang=en#filelist ">http://www.microsoft.com/ Downloads/details.aspx? familyid=4f8f2f01-1ed7-4c4d-8f7b-3d47969e66ae&displaylang=en#filelist Download "Microsoft SQL Server Driver for JDBC ", and install, get Msbase.jar,mssqlserver.jar and Msutil.jar three files, copy three files to Tomcat 4.1 Common\lib folder

2. Add the following code to the host domain in the server.xml of Tomcat 4.1

  Factory
        org.apache.commons.dbcp.BasicDataSourceFactory
    
    
  maxactive
        
    
    
  Maxidle
        
    
    
         maxwait
        10000
    
&NBSP
  username
  SA
 
 
  password
  your password
 
 
  driverclassname
  com.microsoft.jdbc.sqlserver.SQLServerDriver
 
 
  URL
  jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=pubs
       &NBSP
 3. Create a new folder under WebApps "MyWeb", "Myweb\web-inf", "myweb\web-inf\classes"


4. Under Web-inf, create a new file "Web.xml" and add the following

"Java.sun.com/dtd/web-app_2_3.dtd" >HTTP://JAVA.SUN.COM/DTD/WEB-APP_2_3.DTD ">

My Web

Invoker
/servlet/*


Jdbc/mydb
Javax.sql.DataSource
Container

5. Writing servlet program Jdbcpoolserv.java

Import java.sql.*;
Import Javax.naming.Context;
Import Javax.sql.DataSource;
Import Javax.naming.InitialContext;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.io.*;

public class Jdbcpoolserv extends HttpServlet
{
public void doget (HttpServletRequest request,
HttpServletResponse response) throws Servletexception,ioexception {
DataSource Ds=null;
Connection Con=null;
String val= "null", title= "JDBC pooling Test";

try{
Context Initctx = new InitialContext ();
if (Initctx = null)
throw new Exception ("boom-no context");

ds = (DataSource) initctx.lookup (
"Java:comp/env/jdbc/mydb");
if (ds!= null) {
con = ds.getconnection ();
if (con!= null) {
Statement stmt=con.createstatement ();
ResultSet rs=stmt.executequery ("select * from authors");
Rs.next ();

Val=rs.getstring ("au_id");
Rs.close ();
Stmt.close ();
}
Con.close ();
}

}
catch (Exception ex) {
System.out.println (Ex.getmessage ());
}

Response.setcontenttype ("text/html");
PrintWriter out = Response.getwriter ();
Out.println ("");
Out.println ("");
Out.println ("");
Out.println ("");
Out.println ("");
Out.println ("

"+ val +"
");
Out.println ("");
Out.println ("");
}
}


6. Compile Jdbcpoolserv.java get Jdbcpoolserv.class (Note add Servlet.jar package), copy it to "Myweb\web-inf\classes"

7. Start SQL SERVER2000

8. Start Tomcat

9. Browse Http://127.0.0.1:8080/myweb/servlet/JDBCPoolServ

10. In IE, you can see "172-32-1176"

Note: If you do not run correctly check the folder name above, and the URL and Java class name are in the same case



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.