【发布时间】:2018-10-22 16:59:33
【问题描述】:
所以问题是 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));
}
【问题讨论】:
-
您的代码中有两个
Connection对象(co和con),而您使用的是null... -
你没有移动光标你必须调用
res.next()尝试使用if(res.next()){psDB=res.getString(1).toString(); passDB=res.getString(2).toString();} -
他们在哪里?
-
除了
recp.hotel是什么意思schema.tablename?或者到底是什么? -
好的,请尝试在 catch 块中使用
e.printStackTrace();可能有错误
标签: java mysql servlets jdbc prepared-statement