java.sql.SQLException:没有为jdbc找到合适的驱动程序:mysql:// localhost:3306 / db

时间:2015-01-29 16:36:36

标签: java mysql sql-server jdbc

页面总是转发到“custLogin”但数据是正确的。

以下代码:

班级登录:

private CustomerDB db;

public void init() {
    String dbUrl = "jdbc:mysql://localhost:3306/fyp";
    String dbUser = "root";
    String dbPassword = "";
    db = new CustomerDB(dbUrl, dbUser, dbPassword);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String action = request.getParameter("action");

    if (!isAuthenticated(request) && !("authenticate".equals(action))) {
        doLogin(request, response);
        return;
    }
    if ("authenticate".equals(action)) {
        doAuthenticate(request, response);
    } else if ("logout".equals(action)) {
        doLogout(request, response);
    } else {
        response.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
    }
}

private void doAuthenticate(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String username = request.getParameter("custID");
    String password = request.getParameter("custPW");
    String targetURL;
    init();
    boolean isValid = db.isValidUser("chan123", "123456");

    if (isValid) {
        targetURL = "/index.jsp";
    } else {
        targetURL = "/custLogin.jsp";
    }
    RequestDispatcher rd;
    rd = getServletContext().getRequestDispatcher("/" + targetURL);
    rd.forward(request, response);
}

public boolean isAuthenticated(HttpServletRequest request) {
    boolean result = false;
    HttpSession session = request.getSession();
    if (session.getAttribute("userInfo") != null) {
        result = true;
    }
    return result;
}

private void doLogin(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String targetURL = "login.jsp";
    RequestDispatcher rd;
    rd = getServletContext().getRequestDispatcher("/" + targetURL);
    rd.forward(request, response);
}

private void doLogout(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    HttpSession session = request.getSession(false);
    if (session != null) {
        session.removeAttribute("userInfo");
        session.invalidate();
    }
    doLogin(request, response);
}

chan123是custID。 123456是custPW。

Class CustomerDB:

private String dbUrl;
private String dbUser;
private String dbPassword;

public CustomerDB() {
}

public CustomerDB(String dburl, String dbUser, String dbPassword) {
    this.dbUrl = dbUrl;
    this.dbUser = dbUser;
    this.dbPassword = dbPassword;
}

public Connection getConnection() throws SQLException, IOException {
    System.setProperty("jdbc.drivers", "com.mysql.jdbc.Driver");
    Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/fyp", "root", "");
    return conn;
}

public boolean isValidUser(String custID, String custPW) {
    boolean isValid = false;
    Connection cnnct = null;
    PreparedStatement pStmnt = null;

    try {
        cnnct = getConnection();
        String preQueryStatement = "SELECT * FROM customer WHERE custID=? and custPW=?";
        pStmnt = cnnct.prepareStatement(preQueryStatement);
        pStmnt.setString(1, custID);
        pStmnt.setString(2, custPW);
        ResultSet rs = null;
        rs = pStmnt.executeQuery();
        if (rs.next()) {
            isValid = true;
        }
        pStmnt.close();
        cnnct.close();
    } catch (SQLException ex) {
        while (ex != null) {
            ex.printStackTrace();
            ex = ex.getNextException();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return isValid;
}

我创建了测试java。它可以加载正确的结果。

public static void main(String[] arg) {
    String dbUrl = "jdbc:mysql://localhost:3306/fyp";
    String dbUser = "root";
    String dbPassword = "";
    CustomerDB db = new CustomerDB(dbUrl, dbUser,dbPassword);
    boolean n = db.isValidUser("chan123", "123456");
    if (n) {
        System.out.println("TTTTTT");
    }else 
        System.out.println("f");
}

}

显示“TTTTTT”。

但是页面转发到“custLogin”。 输出:

SEVERE:   java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/fyp
    at java.sql.DriverManager.getConnection(DriverManager.java:596)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at fyp.db.CustomerDB.getConnection(CustomerDB.java:29)
    at fyp.db.CustomerDB.isValidUser(CustomerDB.java:39)
    at fyp.servlet.Login.doAuthenticate(Login.java:52)
    at fyp.servlet.Login.doPost(Login.java:39)
...

请帮我解决这个问题。 谢谢!

1 个答案:

答案 0 :(得分:3)

您不需要使用DriverManager Javadoc 中的Class.forName("com.mysql.jdbc.Driver");应用程序不再需要使用Class.forName()明确加载JDBC驱动程序。当前使用Class.forName()加载JDBC驱动程序的现有程序将继续工作而无需修改

您需要将mysql连接器/ j jar文件(例如,可用的here)添加到服务器类路径中。