十步搞定tomcat4.1.18 Servlet中的SQL SERVER2000数据库连接池

类别:Java 点击:0 评论:0 推荐:

1.从http://www.microsoft.com/downloads/details.aspx?FamilyID=4f8f2f01-1ed7-4c4d-8f7b-3d47969e66ae&DisplayLang=en#filelist下载"Microsoft SQL Server 2000 Driver for JDBC",并安装,得到msbase.jar,mssqlserver.jar和msutil.jar三个文件,将三个文件COPY到TOMCAT 4.1下common\lib文件夹中


2.在TOMCAT 4.1的SERVER.XML中HOST域中添加如下代码
<Context path="/myweb" docBase="myweb" debug="0" reloadable="true" crossContext="true">
 <Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"/>
 <ResourceParams name="jdbc/mydb">
 <parameter>
  <name>factory</name>
        <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
     </parameter>
     <parameter>
  <name>maxActive</name>
        <value>100</value>
     </parameter>
     <parameter>
  <name>maxIdle</name>
        <value>30</value>
     </parameter>
     <parameter>
        <name>maxWait</name>
        <value>10000</value>
     </parameter>
 <parameter>
  <name>username</name>
  <value>sa</value>
 </parameter>
 <parameter>
  <name>password</name>
  <value>你的密码</value>
 </parameter>
 <parameter>
  <name>driverClassName</name>
  <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
 </parameter>
 <parameter>
  <name>url</name>
  <value>jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs</value>
 </parameter>      
 </ResourceParams>
</Context>


3.在webapps下新建文件夹"myweb","myweb\WEB-INF","myweb\WEB-INF\classes"


4.在WEB-INF下新建文件"web.xml",并添加如下内容
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
 <display-name>My Web</display-name>
 <servlet-mapping>
  <servlet-name>invoker</servlet-name>
  <url-pattern>/servlet/*</url-pattern>
 </servlet-mapping>
 <resource-ref>
  <res-ref-name>jdbc/mydb</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
 </resource-ref>
</web-app>


5.编写servlet程序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("<html>");
        out.println("<head>");
        out.println("<title>" + title + "</title>");
        out.println("</head>");
        out.println("<body bgcolor=\"white\">");
        out.println("<h1>" + val + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}


6.编译JDBCPoolServ.java得到JDBCPoolServ.class(注意加入servlet.jar包),将其COPY到"myweb\WEB-INF\classes"下


7.启动SQL SERVER2000


8.启动TOMCAT


9.浏览http://127.0.0.1:8080/myweb/servlet/JDBCPoolServ


10.在IE中可看到"172-32-1176"

注意:如果无法正常运行请检查以上文件夹名,URL和JAVA类名大小写是否一致

[email protected]

BluePenguin
2003-2-11

本文地址:http://com.8s8s.com/it/it18009.htm