简体   繁体   English

org.apache.tomcat.dbcp.dbcp.PoolingDataSource $ PoolGuardConnectionWrapper无法强制转换为com.mysql.jdbc.Connection

[英]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. geeting org.apache.tomcat.dbcp.dbcp.PoolingDataSource $ PoolGuardConnectionWrapper无法使用tomcat服务器和mysql数据库强制转换为com.mysql.jdbc.Connection,请在下面找到代码。 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: Java代码:

        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. 如果您使用DataSource,您的连接将被池包装,请使用接口。

Use com.mysql.jdbc.jdbc2.optional.MysqlDataSource instead of com.mysql.jdbc.Driver in your context.xml. 在context.xml中使用com.mysql.jdbc.jdbc2.optional.MysqlDataSource而不是com.mysql.jdbc.Driver You're creating a datasource, not a direct connection. 您正在创建数据源,而不是直接连接。

See the Connector-J docs for more information. 有关更多信息,请参阅Connector-J文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Apache Commons DBCP 连接对象问题,线程:org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper 中的 ClassCastException - Apache Commons DBCP connection object problem, Thread: ClassCastException in org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper org.apache.tomcat.dbcp.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper 无法转换为 oracle.jdbc.OracleConnection - org.apache.tomcat.dbcp.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to oracle.jdbc.OracleConnection java.lang.ClassCastException:org.apache.tomcat.dbcp.dbcp2.PoolingDataSource $ PoolGuardConnectionWrapper无法转换为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无法强制转换为com.mysql.jdbc.PreparedStatement - org.apache.commons.dbcp.DelegatingPreparedStatement cannot be cast to com.mysql.jdbc.PreparedStatement 未捕获的可抛出 java.lang.AbstractMethodError: org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.createArrayOf - Uncaught Throwable java.lang.AbstractMethodError: org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.createArrayOf 即使连接成功后,org.apache.derby.client.net.NetConnection40也不能转换为com.mysql.jdbc.Connection错误? - 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无法转换为oracle.jdbc.OracleConnection - java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.PoolableConnection cannot be cast to oracle.jdbc.OracleConnection 使用org.apache.commons.dbcp.PoolingDataSource $ PoolGuardConnectionWrapper的最里面的类时引发异常 - 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; - java.lang.AbstractMethodError: org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.getClientInfo()Ljava/util/Properties; 如何解决这个 java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to 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?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM