首页 > 解决方案 > 即使在导入 mysql JDBC 连接器(驱动程序)API 后,我的 Servlet 程序也无法识别 mySQL 驱动程序类

问题描述

我是 J2EE 的新手,我正在尝试使用 servlet API 开发一个小型应用程序。我只需要从 mysql 数据库中获取相同的详细信息并将其显示在浏览器上。在我的项目中,我已经导入了 JDBC API(就像我为 servlet api 所做的那样,build-path->Configure External Archive)。但是,如果我尝试执行该项目的 war 文件,我会收到以下错误(我没有收到任何语法错误)。

我收到如下所示的错误

2018 年 6 月 26 日上午 10:57:05 org.apache.catalina.core.StandardWrapperValve 调用严重:Servlet.service() for servlet [checkingCredential] 在路径 [/StudentsApp] 的上下文中引发异常 [Servlet 执行引发异常]根本原因 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1308) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java :1136) 在 com.konman01.studentApp.servlet.CheckCredential.doPost(CheckCredential.java:52) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:661) 在 javax.servlet.http.HttpServlet.service( HttpServlet.java:742) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter( ApplicationFilterChain.java:193) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) 在 org.apache.catalina .core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140 ) 在 org.apache.catalina 的 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)。Valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)在 org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) 在 org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) 在 org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol .java:790) 在 org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468) 在 org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 在 java.util.concurrent.ThreadPoolExecutor$Worker。在 org.apache.tomcat.util.threads.TaskThread 运行(ThreadPoolExecutor.java:624)$WrappingRunnable.run(TaskThread.java:61)在 java.lang.Thread.run(Thread.java:748)

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Driver;

/*
 * Authentication of the login user
 * The user input will be checked with the database username and password information           
 * If the correct username and password is provided, then the profile 
 * containing the user details will be displayed else the login html       page
 *  will be provided  
*/
public class CheckCredential extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet res = null;
    String oldPassword = null;

    String regNum = req.getParameter("regNo");
    String password = req.getParameter("password");
    int regNum_int = Integer.parseInt(regNum);

    /*
     * JDBC code starts here
     */
    try{
        /*
         * 1. Load the Driver
         */

        java.sql.Driver drivRef = new Driver();
        DriverManager.registerDriver(drivRef);

        /*
         * 2. Get the Connection via Driver
         */
        String dBurl = "jdbc:mysql://localhost:3306/jdbc_db?useSSL=false";
        con = DriverManager.getConnection(dBurl, "root", "root");

        /*
         * Issue SQL query via connection
         */

        String query = " select password from students_info "+
                        " where reg_no = ? ";

        pstmt = con.prepareStatement(query);
        pstmt.setInt(1, regNum_int);
        res = pstmt.executeQuery();

        if(res.next())
        {
            oldPassword = res.getString("password");
        }
        else
        {
            String htmlres = "<html>"+
                                "<body>"+
                                    "<p>There is no records in the database for the given register number</p>"+
                                 "</body>"+
                             "</html>";

            resp.setContentType("text/html");
            PrintWriter out = resp.getWriter();
            out.print(htmlres);

            if (con != null)
            {
                try {
                    con.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(pstmt != null)
            {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(res!=null){
                try {
                    res.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            return;
        }       
}
catch(SQLException e){
    e.printStackTrace();
}
finally{
    if (con != null)
    {
        try {
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if(pstmt != null)
    {
        try {
            pstmt.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if(res!=null){
        try {
            res.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      }

     }

    }
   }

我尝试仅使用 servlet 和 JDBC API 执行一些示例代码,它运行良好。请帮帮我。

下图将显示 API 是如何导入到项目中的: 图片

标签: servletsjdbc

解决方案


这就是导致您的错误的原因:

java.sql.Driver drivRef = new Driver();
DriverManager.registerDriver(drivRef);

你不需要实例化这个类。你可以简单地这样称呼它:

Class.forName("com.mysql.jdbc.Driver");

此外,如果这不起作用,请尝试将 .jar 文件数据库连接器移动到 WEB-INF 内的 lib 文件夹


推荐阅读