1

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Getting following error while running the application javax.naming.NamingException: Cannot create resource instance can any body help me on this
Post the whole stack trace.
1

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.

1 Comment

Thanks for your response and suggestion after adding your stmt in context.xml, getting below error javax.naming.NamingException: Cannot create resource instance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.