How to solve this Java lang.ClassCastException:org. apache. tomcat. dbcp. dbcp. Basicdatasource cannot be cast to org apache. tomcat. jdbc. pool. DataSource?

I tried to use pool for database connection in Tomcat. This 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"/>

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

This is how I connect to the 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,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 exceptions:

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

Please help me solve this problem,

to greet

Solution

You just used 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 generally not recommended to develop in any way other than this interface

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>