简体   繁体   中英

org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to com.mysql.jdbc.Connection

geeting org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to com.mysql.jdbc.Connection by using tomcat server and mysql database please find below code. Please help me how to resolve this.

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Context>
<Resource name="jdbc/MySQL_ds" auth="Container" type="javax.sql.DataSource"
               maxActive="50" maxIdle="30" maxWait="10000"
               username="root" password="root321"
               driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://162.70.211.17:3306/emp"
               accessToUnderlyingConnectionAllowed="true"/>

</Context>

web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>sample</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>Insert</display-name>
    <servlet-name>Insert</servlet-name>
    <servlet-class>Insert</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Insert</servlet-name>
    <url-pattern>/insert</url-pattern>
  </servlet-mapping>
  <resource-ref>
<description>MySQL Datasource example</description>
<res-ref-name>jdbc/MySQL_ds</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
  </resource-ref>
</web-app>

Java code:

        import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;






import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;






    import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

    public class Insert extends HttpServlet {  

    protected void service(HttpServletRequest request, HttpServletResponse   response) throws ServletException, IOException {
            doPost(request, response);
    }   
    public void doPost(HttpServletRequest request, HttpServletResponse response)  
                throws ServletException, IOException {  
     System.out.println("do post calling");
    response.setContentType("text/html");  
    PrintWriter out = response.getWriter();  
    String Username = request.getParameter("roll");
    Context ctx = null;
    Connection connection;
    try{  

        ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/MySQL_ds");  
        connection =   (Connection) ds.getConnection();
        System.out.println("Connection established");
        PreparedStatement ps=(PreparedStatement) connection.prepareStatement("insert into employee values(?)");
        ps.setString(1,Username);
        int i=ps.executeUpdate();  
        if(i>0)  
        out.print("You are successfully registered...");  


        }catch (Exception e2) {System.out.println(e2);}  

        out.close();  
        }  
    }

These imports should be removed:

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

Use instead

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;

If you use a DataSource your connection gets wrapped by the pool, use the interfaces.

Use com.mysql.jdbc.jdbc2.optional.MysqlDataSource instead of com.mysql.jdbc.Driver in your context.xml. You're creating a datasource, not a direct connection.

See the Connector-J docs for more information.

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.

Related Question Apache Commons DBCP connection object problem, Thread: ClassCastException in org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper org.apache.tomcat.dbcp.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to oracle.jdbc.OracleConnection java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to oracle.jdbc.OracleConnection org.apache.commons.dbcp.DelegatingPreparedStatement cannot be cast to com.mysql.jdbc.PreparedStatement Uncaught Throwable java.lang.AbstractMethodError: org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.createArrayOf org.apache.derby.client.net.NetConnection40 cannot be cast to com.mysql.jdbc.Connection error even after connection success? java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.PoolableConnection cannot be cast to oracle.jdbc.OracleConnection throwing exception while using innermost class of org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper java.lang.AbstractMethodError: org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.getClientInfo()Ljava/util/Properties; how to solve this java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to org.apache.tomcat.jdbc.pool.DataSource?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM