2012-02-07 184 views
1

当我执行尝试使用连接的示例JSP页时,我已经设置了JDBC连接池并显示以下错误。JDBC连接池中的JDBC驱动程序加载问题

Error occurred org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load 
    JDBC driver class 'com.sybase.jdbc3.jdbc.SybDriver' 

我已经放在​​无论是在common/libweb-inf/lib为好。 如何纠正错误?

The context.xml 

<Context> 
     <Resource name="jdbc/mysybase" auth="Container" 
        type="javax.sql.DataSource" driverClassName="com.sybase.jdbc3.jdbc.SybDriver" 
        url="jdbc:sybase:Tds:H2S33.studtrack.com:2025/student" 
        username="scott" password="tiger" maxActive="20" maxIdle="10" 
        maxWait="-1"/> 
    </Context> 


    In The web.xml file 
    <resource-ref> 
    <description>Sybase Datasource example</description> 
    <res-ref-name>jdbc/mysybase</res-ref-name> 
    <res-type>javax.sql.DataSource</res-type> 
    <res-auth>Container</res-auth> 
    </resource-ref> 


    And the jsp page 

    <%@page import="java.sql.*"%> 
    <%@page import="javax.naming.Context"%> 
    <%@page import="javax.naming.InitialContext"%> 
    <%@page import="java.sql.Connection"%> 
    <%@page import="java.sql.SQLException"%> 
    <%@page import="java.sql.ResultSet"%> 
    <%@page import="javax.sql.DataSource"%> 
    <html> 
    <head> 
    <title>Obtaining a Connection</title> 
    </head> 
    <body> 

    <% 
     Connection conn = null; 
     ResultSet result = null; 
     Statement stmt = null; 
     try { 
      Context initContext = new InitialContext(); 
     Context envContext = (Context)initContext.lookup("java:/comp/env"); 
      DataSource ds = (DataSource)envContext.lookup("jdbc/mysybase"); 
      conn = ds.getConnection(); 
     if (conn != null) 
     { 
      String message = "Got Connection " + conn.toString() + ", "; 
      out.write(message); 
     } 
     else 
     { 
      out.write("hello no conn obtained"); 

     } 

      stmt = conn.createStatement(); 
      result = stmt.executeQuery("SELECT * FROM Student"); 
     while(result.next()) 
     { 
      out.write(result.getString("name")); 
     } 

     } 
     catch (SQLException e) { 
      out.write("Error occurred " + e); 
      } 

    %> 

    </body> 
    </html>` 
+0

你有没有其他相关的JAR文件?如commons dbcp,collections,pool在你的lib中? – 2012-02-07 13:27:38

+0

你有完整的堆栈跟踪吗?它应该包含根异常。 – yair 2012-02-07 13:33:05

+0

我有我所有的这些jar文件在我的common/lib.What其他我需要aprt从下面提到。 servlet-api.jar,naming-resources.jar,naming-factory-dbcp.jar,naming-factory.jar,jtds2.jar,jsp-api.jar,jconn2.jar,jasper-runtime.jar,jasper-compiler- jdt.jar,jasper-compiler.jar,commons-el.jar。 – 2012-02-07 13:33:36

回答

2

Sybase JDBC驱动程序在不同版本中具有不同的软件包命名。您正试图加载较新版本com.sybase.jdbc3.jdbc.SybDriver,而您的jar很可能包含较旧的com.sybase.jdbc2.jdbc.SybDriver

+0

最后一件事。该池正在为jsp/servlets工作。我将被要求对jdbc池进行任何更改,以供普通java类使用。 – 2012-02-08 04:18:03

+0

@gautamvegeta它应该适用于任何基于jdbc的api。 – mrembisz 2012-02-08 07:43:41