在server.xml中</host>上面加入:
<Context
            docBase="D:\eclipse3.2\workspace\test\WebRoot"
            path="/test"
            reloadable="true" >
    <Resource name="jdbc/rat" auth="Container" type="javax.sql.DataSource"
  maxActive="100" maxIdle="30" maxWait="10000"
  driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
  username="sa" password=""
  url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=test"
  defaultAutoCommit="true" removeAbondoned="true"
  removeAbondonedTimeout="60" logAbondoned="true"/>
</Context>
在web.xml文件中加入:
<resource-ref>
  <description>Sql Server Datasource</description>
  <res-ref-name>jdbc/rat</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
 </resource-ref>
项目文件夹是test
数据库:test
测试页:test.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="javax.naming.*" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.sql.DataSource" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="java.sql.*" %>
<%
 DataSource ds=null;
 try
 {
  Context initCtx=new InitialContext();
  Context envCtx=(Context)initCtx.lookup("java:comp/env");
  file://从Context中lookup数据源
  ds=(DataSource)envCtx.lookup("jdbc/rat");
  
  if(ds!=null)
  {
   out.println("DataSource!OK!");
   out.println("<br>");
   Connection conn=ds.getConnection();
   Statement stmt=conn.createStatement();
   ResultSet rst=stmt.executeQuery("select * from test");
   out.println("DATA out!");
   while(rst.next())
   {
    out.println("bookName:"+rst.getString("test"));
    out.println("<br>");
   }
  }
  else
   out.println("失败!");
 }
 catch(Exception ne)
 {
  out.println(ne); 
 }   
%>
tomcat5.5.20解压版
JDK5
请问问题出在哪?

解决方案 »

  1.   

    页面的异常如下:
    org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.microsoft.jdbc.sqlserver.SQLServerDriver' 
      

  2.   

    是不能加载驱动
    你要把mssql server的jdbc驱动放到tomcat的common\lib目录下
      

  3.   

    把驱动放到tomcat的common\lib目录下可以连接上,有没有其他方法?
      

  4.   

    你的test目录下面应该紧跟WEB-INF目录
      

  5.   

    把驱动放到你的项目的lib下。
    WEB-INF\lib\
      

  6.   

    连接池我是用tomcat的管理器配置了,它不修改web.xml,驱动放在tomcat的common\lib目录下才可以连接上
    把驱动放在应用项目WEB-INF\lib\下时,需要改web.xml
      

  7.   

    把驱动放到你这个应用的lib下去
      

  8.   

    web.xml 加入 根据自己情况加
    <resource-ref>    
      <description>mysql1 Connection</description>    
      <res-ref-name>jdbc/mysql1</res-ref-name>    
      <res-type>javax.sql.DataSource</res-type>    
      <res-auth>Container</res-auth> 
       </resource-ref>
      

  9.   

    有个哥们说遇到过1433端口屏蔽也报这种错误,你进CMD netstat -an 检查下吧,我遇到都是设置的问题。
      

  10.   

    LZ你在WEB。XML中已经加了<resource-ref> 了
      

  11.   

    在%TOMCAT_HOME%/conf/Catalina/localhost下新建一个与你web文件夹同名的xml文件
    <?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>
    还有可能是JDBC驱动版本问题,希望对LZ有帮助
      

  12.   

    MSSQL2000:com.microsoft.jdbc.sqlserver.SQLServerDriver
    MSSQL2005:com.microsoft.sqlserver.jdbc.SQLServerDriver
    不知道你是用2000还是用2005了。这个问题我以前遇到过希望帮得到你吧。