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

标签 tomcat7 sqlanywhere

我正在运行 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/

相关文章:

java - war 部署成功后tomcat给出404

java - hybris java代码更改无需重新启动服务器

java - 我如何运行两个 Tomcat 服务器 v7.0

sql-server - 对现有更新的 SQL Server

java - 在 Mac OSX 上运行项目时出现 404 错误

linux - Java.nio.Files "createSymbolicLink"删除目标目录内容?

sql - 使用 SQL Anywhere 创建 .db 文件 - 现在使用 RazorSQL 查询它

sql - 有什么好的 SQL Anywhere 数据库模式比较工具吗?

C++:创建对象的本地拷贝

c# - 更改c# WebReference url地址