JDBC try块不执行

时间:2018-05-12 12:56:03

标签: java mysql servlets jdbc prepared-statement

所以问题是Try块代码没有执行,我该怎么办?问题出在哪里?

public class verif {
    public static String checki(String pseudo, String mdp) {
        boolean check = false;
        String psDB = "", passDB = "";
        Connection con = null;
        PreparedStatement stmt;
        ResultSet res;
        try {

            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel", "naila", "pass");
            stmt = con.prepareStatement("select pseudo,mdp from recp");
            res = stmt.executeQuery();
            while (res.next()) {
                psDB = res.getString("pseudo");
                passDB = res.getString("mdp");
            }

            boolean us = psDB.equals(pseudo);
            boolean ps = passDB.equals(mdp);
            if (us && ps)
                check = true;
            else
                check = false;
        } catch (ClassNotFoundException | SQLException e) {
        }
        return psDB;
    }
}

这是servlet:

 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String pseudo=request.getParameter("username");
    String mdp = request.getParameter("password");
    PrintWriter p = response.getWriter();
    p.print("pseudo est "+verif.checki(pseudo, mdp));
  }

2 个答案:

答案 0 :(得分:0)

您忘记致电next()上的ResultSet res,将ResultSet的光标指向第一条记录(如果存在的话)。如果你没有调用next方法,那么该光标将指向第一个结果记录之前,然后res.getString(#)将不会返回任何内容。

这样做:

.
.
.
res = stmt.executeQuery();
while(res.next()){
    psDB = res.getString(1).toString();
    passDB = res.getString(2).toString();
}
.
.
.

另一个提示是调用res.getString(#)会返回String类型的值,而无需再次调用.toString()。所以最好像这样改变它:

.
.
.
res = stmt.executeQuery();
while(res.next()){
    psDB = res.getString(1);
    passDB = res.getString(2);
}
.
.
.

在这种情况下,将sql的结果尽可能缩小到只有一条记录(检查用户名和密码以便登录等)是一种很好的做法。这意味着只选择传递给checki方法的用户名的密码:

.
.
.
stmt = con.prepareStatement("select pseudo, mdp from recp where pseudo = ? ");
stmt.setString(1, pseudo);
res = stmt.executeQuery();
while(res.next()){
    psDB = res.getString(1);
    passDB = res.getString(2);
}
.
.
.

如果您在此之后有任何其他错误,您可以检查您的表中有多少条记录,并检查您在代码中选择的记录是否存在。

之后可能是因为代码中的某些异常被catch子句捕获但由于您在catch子句中什么都不做而无法发出任何信号通知您。像这样更改catch子句以查看是否发生任何异常:

}catch (ClassNotFoundException | SQLException e) {
    e.printStackTrace();
}

答案 1 :(得分:0)

您的代码应如下所示。

public class verif {
    public static boolean checki(String pseudo, String mdp) {
        boolean check = false;
        String psDB = "", passDB = "";
        Connection con = null;
        PreparedStatement stmt;
        ResultSet res;
        try {

            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel", "naila", "pass");
            stmt = con.prepareStatement("select pseudo,mdp from recp where pseudo=? AND mdp=?");
            stmt.setString(pseudo);
            stmt.setString(mdp);

            res = stmt.executeQuery();
           /* Not  Needed
         while (res.next()) {
                psDB = res.getString("pseudo");
                passDB = res.getString("mdp");
            }

            boolean us = psDB.equals(pseudo);
            boolean ps = passDB.equals(mdp);*/
            if (res.next())
                check = true;
            else
                check = false;
        } catch (Exception e) {
              e.printStackTrace();
        }
        return check;
    }
}

Servlet Side代码应该是这样的。

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{   
    String pseudo=request.getParameter("username");
    String mdp = request.getParameter("password");
    PrintWriter p = response.getWriter();
    if(verif.checki(pseudo, mdp)){
        // Put your Business Logic (Create session and redirect to Main Page)
        p.print("Successfully Logged in");
    }
    else{
        // Redirect to LogIn page
        p.print("Invalid Credential ");
    }

}