Servlet.service()用于上下文中的servlet抛出异常eclipse

时间:2017-05-20 22:32:53

标签: java eclipse tomcat jdbc

我正在使用JDBC创建一个Web应用程序。我已将连接器.jar文件添加到Tomcat / lib文件夹中。我在META-INF和web.xml中配置了context.xml。但是,当我运行我的应用程序时,我收到此错误。我无法弄清楚它的原因。也许,一些连接问题。如果你帮助了我,我将非常感激。

的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输出:

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)

0 个答案:

没有答案