我已经设置了 JDBC 连接池,当我执行尝试使用连接的示例 JSP 页面时,它显示以下错误。
Error occurred org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load
JDBC driver class 'com.sybase.jdbc3.jdbc.SybDriver'
我也将 jconn2.jar
放置在 common/lib
和 web-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>`
最佳答案
Sybase JDBC驱动程序在不同版本中有不同的包命名。您正在尝试加载更新版本com.sybase.jdbc3.jdbc.SybDriver
,而您的 jar 很可能包含较旧的 com.sybase.jdbc2.jdbc.SybDriver
.
关于java - JDBC 连接池中的 JDBC 驱动程序加载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9176903/