如何解决这个 java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource 不能转换为 org.apache.to
Posted
技术标签:
【中文标题】如何解决这个 java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource 不能转换为 org.apache.tomcat.jdbc.pool.DataSource?【英文标题】:how to solve this java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to org.apache.tomcat.jdbc.pool.DataSource? 【发布时间】:2012-06-11 04:52:49 【问题描述】:我正在尝试使用池在 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"/>
这就是我的 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>
这就是我连接数据库的方式:
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");
但它给了我以下例外:
java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to org.apache.tomcat.jdbc.pool.DataSource
请帮我解决这个问题,
问候
【问题讨论】:
【参考方案1】:您只是使用了错误的导入。
替换这个:
import org.apache.tomcat.jdbc.pool.DataSource;
用这个:
import javax.sql.DataSource;
javax.sql.DataSource
是所有DataSource
实现必须继承的基本接口。通常不建议针对除此接口之外的任何东西进行开发。
【讨论】:
【参考方案2】:如果遇到这个错误,如何解决这个java.lang.ClassCastException:
org.apache.tomcat.dbcp.dbcp.BasicDataSource 不能转换为 org.apache.tomcat.jdbc.pool.DataSource
尝试替换:
import org.apache.tomcat.jdbc.pool.DataSource;
与:
import javax.sql.DataSource;
【讨论】:
【参考方案3】:有两种选择
-
更改导入
替换
import org.apache.tomcat.jdbc.pool.DataSource;
与
import javax.sql.DataSource;
-
将 DataSourceFactory 添加到
Context.xml
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
【讨论】:
以上是关于如何解决这个 java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource 不能转换为 org.apache.to的主要内容,如果未能解决你的问题,请参考以下文章