在程序中获得意外的输出 [英] Getting unexpected output in program

查看:192
本文介绍了在程序中获得意外的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是代码:

 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(不知道为什么?? :()):

But I'm getting output like,but I'm putting the correct emp_id & password(still it's showing 4> + java.lang.illegalstateexception (don't know why ?? :( )):

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

任何想法.....为什么会发生?

any idea.....why it's happening ?

推荐答案

这是因为您的算法包括:

It's happening because you algorithm consists of:


  1. 通过所有员工迭代

  2. 如果员工匹配ID /密码,请打印2>,否则打印4>

所以你会有一个 2>,3> 输出匹配,所有其他的输出将给您错误400。

So you'll have one 2>, 3> output for the one that matches and all the others will give you the error 400.

相反,您可以迭代你所有的员工(尽管我t可能最好添加一个条件到您的SQL以缩小结果集由密码和员工ID),不要输出错误,除非您已经用尽所有的结果,没有找到匹配的。

Instead, you can iterate through all your employees (although it might be best to add a criteria to your SQL to narrow down the result set by password and employee ID), don't output an error unless you have exhausted all the results and did not find the matching one.

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) {}
}

这篇关于在程序中获得意外的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆