如何解决这个 java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to org.apache.tomcat.jdbc.pool.D

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10926659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:06:23  来源:igfitidea点击:

how to solve this java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to org.apache.tomcat.jdbc.pool.DataSource?

javaservletsjnditomcat7

提问by CodeNotFound

I am trying to do the database connection in tomcat using pooling, this is the what is my context :

我正在尝试使用池在 tomcat 中进行数据库连接,这就是我的上下文:

<Resource name="jdbc/slingemp" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/slingemp"/>

and this is what is my web.xml :

这就是我的 web.xml :

<description>MySQL JNDI Test</description>
  <resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/slingemp</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

and this is how i connect with database :

这就是我与数据库连接的方式:

 package org.slingemp.jnditest;

import java.io.IOException;
import java.sql.Connection;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.jdbc.pool.DataSource;

/**
 * Servlet implementation class JNDILookUpServlet
 */
@WebServlet("/JNDILookUpServlet")
public class JNDILookUpServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public JNDILookUpServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        try {
            Context initContext = new InitialContext();
            Context envContext  = (Context)initContext.lookup("java:/comp/env");
            DataSource ds = (DataSource)envContext.lookup("jdbc/slingemp");
            Connection conn = ds.getConnection();
            if(conn != null)System.out.println("Connected..");
            else System.out.println("Not connected...");
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.sendRedirect("index.jsp");
    }


}

but it gives me the following exception :

但它给了我以下例外:

java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to org.apache.tomcat.jdbc.pool.DataSource

Please help me to resolve this,

请帮我解决这个问题,

Regards

问候

回答by Sean Patrick Floyd

You are just using the wrong import.

您只是使用了错误的导入。

replace this:

替换这个:

import org.apache.tomcat.jdbc.pool.DataSource;

with this:

有了这个:

import javax.sql.DataSource;

javax.sql.DataSourceis the base interface that all DataSourceimplementations must inherit. It is usually not advisable to develop against anything else than this interface.

javax.sql.DataSource是所有DataSource实现都必须继承的基本接口。通常不建议针对此接口以外的任何其他内容进行开发。