为什么以下servlet给出“没有找到合适的驱动程序错误”?

时间:2014-05-02 12:30:17

标签: java mysql servlets

我正在尝试为银行创建一个小型Web应用程序,作为学习servlet和jsps的一部分。

以下servlet发出以下错误。谁能解释一下?

  

代码:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    String dbURL = request.getServletContext().getInitParameter("dbURL");
    String userName = request.getServletContext().getInitParameter("userName");
    String passWord = request.getServletContext().getInitParameter("password");
    System.out.println("dbURL = " + dbURL);
    System.out.println("userName = " + userName);
    System.out.println("passWord = " + passWord);
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet rs = null;
    boolean isValid = false;

    String empName = (String) request.getAttribute("userName");
    System.out.println("empName = " + empName);
    String empPassCode = (String) request.getAttribute("password");
    System.out.println("empPassCode = " + empPassCode);

    try {
        connection = DriverManager.getConnection(dbURL, userName, passWord);
        String query = "select password from t_employees where emp_name = ?";
        preparedStatement = connection.prepareStatement(query);
        preparedStatement.setString(1, empName);
        rs = preparedStatement.executeQuery();

        while (rs.next()) {
            String passCode = rs.getString("password");
            System.out.println("passCode = " + passCode);
            if (empPassCode != null && empPassCode.equals(passCode)) {
                isValid = true;
            }
        }

        if (isValid) {
            request.getRequestDispatcher("emp-welcome.jsp");
        } else {
            request.getRequestDispatcher("login-error.jsp");
        }
    } catch (SQLException ex) {
        ex.printStackTrace();
    }
}
  

JBoss错误:

16:59:56,167 INFO  [stdout] (http--127.0.0.1-8080-2) empName = null
16:59:56,167 INFO  [stdout] (http--127.0.0.1-8080-2) empPassCode = null
16:59:56,185 ERROR [stderr] (http--127.0.0.1-8080-2) java.sql.SQLException: No suitable driver found for "jdbc:mysql://localhost:3306/bank_db"
16:59:56,323 ERROR [stderr] (http--127.0.0.1-8080-2)    at java.sql.DriverManager.getConnection(DriverManager.java:596)
16:59:56,324 ERROR [stderr] (http--127.0.0.1-8080-2)    at java.sql.DriverManager.getConnection(DriverManager.java:215)
16:59:56,325 ERROR [stderr] (http--127.0.0.1-8080-2)    at com.ct.LoginServlet.doPost(LoginServlet.java:30)
16:59:56,326 ERROR [stderr] (http--127.0.0.1-8080-2)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
16:59:56,327 ERROR [stderr] (http--127.0.0.1-8080-2)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
16:59:56,327 ERROR [stderr] (http--127.0.0.1-8080-2)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)
16:59:56,328 ERROR [stderr] (http--127.0.0.1-8080-2)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
16:59:56,329 ERROR [stderr] (http--127.0.0.1-8080-2)    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
16:59:56,329 ERROR [stderr] (http--127.0.0.1-8080-2)    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
16:59:56,330 ERROR [stderr] (http--127.0.0.1-8080-2)    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)
16:59:56,333 ERROR [stderr] (http--127.0.0.1-8080-2)    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
16:59:56,335 ERROR [stderr] (http--127.0.0.1-8080-2)    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
16:59:56,338 ERROR [stderr] (http--127.0.0.1-8080-2)    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
16:59:56,339 ERROR [stderr] (http--127.0.0.1-8080-2)    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)
16:59:56,341 ERROR [stderr] (http--127.0.0.1-8080-2)    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
16:59:56,342 ERROR [stderr] (http--127.0.0.1-8080-2)    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)
16:59:56,342 ERROR [stderr] (http--127.0.0.1-8080-2)    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)
16:59:56,343 ERROR [stderr] (http--127.0.0.1-8080-2)    at java.lang.Thread.run(Thread.java:724)

我已将MySQL连接器/ j复制到web-inf / lib中,项目模块依赖项也有jar mysql jar文件。提前谢谢。

2 个答案:

答案 0 :(得分:0)

请原谅我的愚蠢错误。一切都是正确的,除了在我的web.xml中我用双引号给出了contextInitParameters。因此,我收到了错误。

答案 1 :(得分:-1)

您需要在类路径中包含mysql-connector-java-5.1.11-bin.jar并包含此行,

try{
     Class.forName("org.gjt.mm.mysql.Driver");
     ------
     ------
}