jdbc try块不执行

8yoxcaq7  于 2021-06-25  发布在  Mysql
关注(0)|答案(3)|浏览(268)

所以问题是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));
  }
ajsxfq5m

ajsxfq5m1#

你的代码应该是这样的。

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端代码应该是这样的。

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 ");
    }

}
nafvub8i

nafvub8i2#

Connection con = null;
Connection co=DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel" ,"naila","pass");

声明了这2个连接,但在准备的语句中使用:

stmt = con.prepareStatement("select pseudo,mdp from recp.hotel");

只要去掉骗局,用co,就可以了
另外,当您执行prepared语句时,会得到一个游标。光标需要移动到下一个位置。例如,如果您认为将返回多个记录,请执行以下操作:

while(rs.next()){  
  psDB=res.getString("pseudo");
  passDB=res.getString("mdp");
}
pcrecxhr

pcrecxhr3#

你只是忘了打电话 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();
}

相关问题