gpt4 book ai didi

tomcat7 - java.lang.ClassCastException : org. apache.tomcat.dbcp.dbcp.BasicDataSource 无法转换为 org.apache.tomcat.jdbc.pool.DataSource

转载 作者:行者123 更新时间:2023-12-04 22:50:18 26 4
gpt4 key购买 nike

我正在运行 Tomcat 7.0.22,我编写了一个简单的 servlet,它连接到 SQL Anywhere 12.0 数据库。当我运行 servlet 时,我得到 java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to org.apache.tomcat.jdbc.pool.DataSource。我的 ./META-INF/content.xml 文件如下所示:

<Context>
<Resource name="jdbc/FUDB"
auth="Container"
type="javax.sql.DataSource"
username="dba"
password="sql"
driverClassName="sybase.jdbc.sqlanywhere.IDriver"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"

url="jdbc:sqlanywhere:uid=dba;pwd=sql;eng=BTH476331A_FedUtilization;" accessToUnderlyingConnectionAllowed="true" maxActive="8" maxIdle="4" />



我的 webapp web.xml 看起来像这样:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">
<display-name>FedUtilization</display-name>
<servlet>
<servlet-name>Report1</servlet-name>
<display-name>Report1</display-name>
<servlet-class>com.sapgss.ps.servlet.Report1</servlet-class>

Report1 /Report1
SQL Anywhere 12.0.1 server jdbc3 jdbc/FUDB javax.sql.DataSource Container



servlet代码如下:

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import org.apache.catalina.core.StandardContext.*;
import org.apache.tomcat.jdbc.pool.*;
import com.sapgss.ps.dbutil.*;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

public class Report1 extends HttpServlet {

public void doGet(HttpServletRequest request,

HttpServletResponse response) throws IOException, ServletException { try { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println("Hello Elaine!"); out.println(""); out.println(""); out.println("

Hello Elaine!

");
// This is how to code access to the database in Java Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); DataSource ds = (DataSource) envCtx.lookup("jdbc/FUDB"); Connection conn = ds.getConnection(); . . .
} }



当我尝试在此行获取 DataSource 时发生错误:
DataSource ds = (DataSource) envCtx.lookup("jdbc/FUDB");

提前谢谢我正在拔头发。

最佳答案

就我而言,我只是忘记输入:

factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"

在我的 /tomcat7/conf/context.xml 中。刚刚添加,一切正常。

我的context.xml:
<Context> 
<Resource name="jdbc/gestrel" auth="Container"
type="javax.sql.DataSource"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://127.0.0.1:5432/g...."
username="postgres"
password="....." maxActive="20" maxIdle="10"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
maxWait="-1"/>
</Context>

关于tomcat7 - java.lang.ClassCastException : org. apache.tomcat.dbcp.dbcp.BasicDataSource 无法转换为 org.apache.tomcat.jdbc.pool.DataSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7667008/

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