【问题标题】:org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to com.mysql.jdbc.Connectionorg.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper 无法转换为 com.mysql.jdbc.Connection
【发布时间】:2017-07-31 01:26:56
【问题描述】:

geeting org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper 无法使用 tomcat 服务器和 mysql 数据库强制转换为 com.mysql.jdbc.Connection 请查找以下代码。请帮我解决这个问题。

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

        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();  
        }  
    }

【问题讨论】:

    标签: java servlets jakarta-ee datasource jndi


    【解决方案1】:

    应删除这些导入:

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

    改为使用

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

    如果您使用 DataSource,您的连接会被池包装,请使用接口。

    【讨论】:

    • 在运行应用程序 javax.naming.NamingException 时出现以下错误:无法创建资源实例任何人都可以帮助我解决这个问题
    • 发布整个堆栈跟踪。
    【解决方案2】:

    在您的 context.xml 中使用 com.mysql.jdbc.jdbc2.optional.MysqlDataSource 而不是 com.mysql.jdbc.Driver。您正在创建数据源,而不是直接连接。

    更多信息请参见the Connector-J docs

    【讨论】:

    • 感谢您在 context.xml 中添加 stmt 后的回复和建议,出现以下错误 javax.naming.NamingException: Cannot create resource instance
    猜你喜欢
    • 2017-08-13
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 2016-06-30
    • 2016-01-14
    相关资源
    最近更新 更多