JDBC连接池中的JDBC驱动程序加载问题

时间:2012-02-07 13:15:38

标签: java java-ee jdbc connection-pooling

我设置了JDBC连接池,当我执行试图使用连接的示例JSP页面时,它显示以下错误。

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

我已将jconn2.jar同时放在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>`

1 个答案:

答案 0 :(得分:2)

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