当前位置:主页>jsp>文章内容
十步搞定Servlet中的mssql2000数据库连接池
来源: 作者: 发布时间:2007-04-04  

1.从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下载"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域中添加如下代码

  factory
        org.apache.commons.dbcp.BasicDataSourceFactory
    
    
  maxActive
        100
    
    
  maxIdle
        30
    
    
        maxWait
        10000
    
 
  username
  sa
 
 
  password
  你的密码
 
 
  driverClassName
  com.microsoft.jdbc.sqlserver.SQLServerDriver
 
 
  url
  jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs
       
 3.在webapps下新建文件夹"myweb","myweb\WEB-INF","myweb\WEB-INF\classes"


4.在WEB-INF下新建文件"web.xml",并添加如下内容

    "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.编写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("");
        out.println("");
        out.println("");
        out.println("");
        out.println("");
        out.println("

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


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类名大小写是否一致


 
上一篇:TOMCAT用https替换http的方法   下一篇:实现基于Spring框架应用的权限控制系统
 
  相关文章
·TOMCAT用https替换http的方法
·实现基于Spring框架应用的权限控制系统
·JAVA基础:java中数值变量与字符串互换
·基于MVC设计模式的WEB应用框架研究
·步入SWT/JFace世界,你还等什么
·Eclipse 3.2 Java开发工具的新特性
·Oracle回滚表空间数据文件误删除处理
·使用Eclipse Callisto分析应用程序
·Oracle数据库的常用函数列表快速查
·EJB 3.0持久化规范之实体类的要求
·在Oracle JDBC访问中加入Spring特性
·Spring框架下实现基于组的用户权限管理
 
【关闭窗口】
推荐本站资源
最新文章