简体   繁体   中英

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

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

<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 :

<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

You are just using the wrong import.

replace this:

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

with this:

import javax.sql.DataSource;

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

If you get this error, how to solve this java.lang.ClassCastException :

org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to org.apache.tomcat.jdbc.pool.DataSource

Try to replace:

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

with:

import javax.sql.DataSource;

There are Two options

  1. change import

replace import org.apache.tomcat.jdbc.pool.DataSource; with import.sql.DataSource;

  1. add Context.xml DataSourceFactory

    add Context.xml factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM