gpt4 book ai didi

java - 程序中得到意外的输出

转载 作者:行者123 更新时间:2023-12-01 18:59:57 24 4
gpt4 key购买 nike

这里是代码:

 String sql_1 = "select emp_id,password from regid";
ResultSet rs = st.executeQuery(sql_1);

while(rs.next())
{

if(((employee.equals(rs.getString("emp_id"))) && (password.equals(rs.getString("password"))))==true)
{

// String sql2="update regid set regid='"+Datastore.regIds.add(regId)+"' where emp_id='"+employee+"'";
// st.executeUpdate(sql2);
System.out.println("2> Employee Id : "+employee+" && Password : "+password);
System.out.println("3> This employee "+employee+" exsists in the database and registration-password id will be Updated");

// resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.print("<html><body>");
out.print("<head>");
out.print("<title>Policy Page</title>");
out.print("<link rel='icon' href='../images/favicon.png'/>");
out.print("</head>");
String status = (String) req.getAttribute(ATTRIBUTE_STATUS);
if (status != null)
{
out.print("Status :"+status);
}
List<String> devices = Datastore.getDevices();
if (devices.isEmpty())
{
out.print("<h2>No devices registered!</h2>");
}
else
{

out.print("<h2>" + devices.size() + " device(s) registered!</h2>");
out.print("<form name='form' method='POST' action='sendAll'>");
out.print("<input type='text' name='policy'>");
resp.setStatus(HttpServletResponse.SC_OK);
out.print("<input type='submit' value='Apply Policy'>");
out.print("</form>");
// System.out.println(HTTP_STATUS);
System.out.println(HttpServletResponse.SC_OK);
getServletContext().getRequestDispatcher("/home").forward(req, resp);

}
out.print("</body></html>");
resp.setStatus(HttpServletResponse.SC_OK);

}

else {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
System.out.println(HttpServletResponse.SC_BAD_REQUEST);
System.out.println("4> This employee "+employee+" does not exsist in the database");
}

}

// rs.close();
}

但是我得到的输出类似于,但我输入了正确的 emp_id 和密码(仍然显示 4> + java.lang.illegalstateexception (不知道为什么??:( )):

1> Employee : P1 && Password : ppp
400
4> This employee P1 does not exsist in the database
2> Employee Id : P1 && Password : ppp
3> This employee P1 exsists in the database and registration-password id will be Updated
400
4> This employee P1 does not exsist in the database

知道......为什么会发生这种情况?

最佳答案

发生这种情况是因为您的算法包含:

  1. 遍历所有员工
  2. 如果员工与 ID/密码匹配,则打印 2>,否则打印 4>

因此,您将获得一个匹配的 2>, 3> 输出,而所有其他输出将为您提供错误 400。

相反,您可以迭代所有员工(尽管最好在 SQL 中添加条件以通过密码和员工 ID 缩小结果集范围),除非您已经用尽了所有员工,否则不要输出错误结果没有找到匹配的。

PreparedStatement stmt = null;
try {
stmt = new PreparedStatement("select * from regis where emp_id=? and password=?");
stmt.setString(1, employee);
stmt.setString(2, password);

ResultSet rs = stmt.executeQuery();
if(rs.next()) {
System.out.println("2> Employee Id : "+employee+" && Password : "+password);
System.out.println("3> This employee "+employee+" exsists in the database and
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.print("<html><body>");
out.print("<head>");
out.print("<title>Policy Page</title>");
out.print("<link rel='icon' href='../images/favicon.png'/>");
out.print("</head>");
String status = (String) req.getAttribute(ATTRIBUTE_STATUS);
if (status != null)
{
out.print("Status :"+status);
}
List<String> devices = Datastore.getDevices();
if (devices.isEmpty())
{
out.print("<h2>No devices registered!</h2>");
}
else
{

out.print("<h2>" + devices.size() + " device(s) registered!</h2>");
out.print("<form name='form' method='POST' action='sendAll'>");
out.print("<input type='text' name='policy'>");
resp.setStatus(HttpServletResponse.SC_OK);
out.print("<input type='submit' value='Apply Policy'>");
out.print("</form>");
// System.out.println(HTTP_STATUS);
System.out.println(HttpServletResponse.SC_OK);
getServletContext().getRequestDispatcher("/home").forward(req, resp);

}
out.print("</body></html>");
resp.setStatus(HttpServletResponse.SC_OK);

}

else {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
System.out.println(HttpServletResponse.SC_BAD_REQUEST);
System.out.println("4> This employee "+employee+" does not exsist in the database");
}
}
catch(Exception e) {
e.printStackTrace();
}
finally {
try {
stmt.close();
} catch(Exception x) {}
}

关于java - 程序中得到意外的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12622718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com