2012-02-07 166 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