3

I am creating a web application with JDBC. I have added my connector .jar file to the Tomcat/lib folder. I configured context.xml in META-INF and web.xml. But when I run my application I get this error. I can not figure out the reason of it. Maybe, some connection problems. So I would be very grateful if you helped me with that.

Servlet:

@WebServlet("/QueryServlet")
public class QueryServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    DataSource ds;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QueryServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Set the MIME type for the response message
          response.setContentType("text/html");
          // Get a output writer to write the response message into the network socket
          PrintWriter out = response.getWriter();

          Connection conn = null;
          Statement stmt = null;

          String url = "jdbc:mysql://localhost:3306/mysql/ebookshop";
          String user = "***";
          String pwd = "***";
          try {
             // Step 1: Create a database "Connection" object
             // For MySQL

             InitialContext ctx = new InitialContext();
             ds = (DataSource) ctx.lookup("java:comp/env/jdbc/ebookshop");
            // 
             //DriverManager.registerDriver(new com.mysql.jdbc.Driver());
             Class.forName("com.mysql.jdbc.Driver");
             conn = ds.getConnection();  // <<== Check
             // For MS Access
             // conn = DriverManager.getConnection("jdbc:odbc:ebookshopODBC");

             // Step 2: Create a "Statement" object inside the "Connection"
             stmt = conn.createStatement();

             // Step 3: Execute a SQL SELECT query
             String sqlStr = "SELECT * FROM books WHERE author = "
                   + "'" + request.getParameter("author") + "'"
                   + " AND qty > 0 ORDER BY author ASC, title ASC";

             // Print an HTML page as output of query
             out.println("<html><head><title>Query Results</title></head><body>");
             out.println("<h2>Thank you for your query.</h2>");
             out.println("<p>You query is: " + sqlStr + "</p>"); // Echo for debugging
             ResultSet rset = stmt.executeQuery(sqlStr); // Send the query to the server

             // Step 4: Process the query result
             int count = 0;
             while(rset.next()) {
                // Print a paragraph <p>...</p> for each row
                out.println("<p>" + rset.getString("author")
                      + ", " + rset.getString("title")
                      + ", $" + rset.getDouble("price") + "</p>");
                ++count;
             }
             out.println("<p>==== " + count + " records found ====</p>");
             out.println("</body></html>");
          } catch (SQLException | NamingException | ClassNotFoundException ex) {
             ex.printStackTrace();
          } finally {
             out.close();
             try {
                // Step 5: Close the Statement and Connection
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
             } catch (SQLException ex) {
                ex.printStackTrace();
             }
          }
    }

context.xml:

<Context antiJARLocking="true" path="/DBConnectionPoolTest">
       <Resource name="jdbc/ebookshop"
                 auth="Container"
                 type="javax.sql.DataSource"
                 username="***" password="v"
                 driverclassname="com.mysql.jdbc.Driver"
                 url="jdbc:mysql://localhost:3306/ebookshop"
                 maxactive="10"
                 maxidle="4" />
   </Context>

pom.xml:

<display-name>ebookshop</display-name>
    <resource-ref>
      <description>DB Connection Pool</description>
      <res-ref-name>jdbc/ebookshop</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
      <res-sharing-scope>Shareable</res-sharing-scope>
   </resource-ref>

Eclipse output:

SEVERE: Servlet.service() for servlet [servlets.QueryServlet] in context with path [/ebookshop] threw exception [Servlet execution threw an exception] with root cause
java.lang.AbstractMethodError: com.mysql.jdbc.Connection.isValid(I)Z
    at org.apache.tomcat.dbcp.dbcp2.DelegatingConnection.isValid(DelegatingConnection.java:924)
    at org.apache.tomcat.dbcp.dbcp2.PoolableConnection.validate(PoolableConnection.java:282)
    at org.apache.tomcat.dbcp.dbcp2.PoolableConnectionFactory.validateConnection(PoolableConnectionFactory.java:359)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.validateConnectionFactory(BasicDataSource.java:2316)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2299)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2043)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1543)
    at servlets.QueryServlet.doGet(QueryServlet.java:61)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Cassie
  • 2,941
  • 8
  • 44
  • 92
  • duplicate? http://stackoverflow.com/questions/32783706/java-lang-abstractmethoderror-com-mysql-jdbc-connection-isvalidiz – ekaerovets May 20 '17 at 22:46
  • @ekaerovets I saw this question. But answers did not help me. Cause I have the latest mysql connector. – Cassie May 21 '17 at 05:26
  • 1
    Can you please provide us with MySQL Server version and the Connector/J JDBC driver version that you're using to help you further? – N00b Pr0grammer May 21 '17 at 07:16
  • You may **think** you have the latest version of the driver, but the error clearly demonstrates you don't use it (this method has been implemented since at least Connector/J 5.1.1). This could mean that you have two different versions of the driver on the classpath (eg one in the Tomcat lib folder and one in your application). – Mark Rotteveel May 21 '17 at 08:06
  • @MarkRotteveel I added the newest version as external jar. Also, I put into tomcat/lib folder and meta_inf/lib folder of my project, bu now I have `java.lang.ClassNotFoundException: com.mysql.jdbc.Driver`. – Cassie May 21 '17 at 08:41

0 Answers0