gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:53:55 25 4
gpt4 key购买 nike

我已经设置了 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>`

最佳答案

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

关于java - JDBC 连接池中的 JDBC 驱动程序加载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9176903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com