Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在程序中获取意外输出_Java_Exception_Jdbc - Fatal编程技术网

Java 在程序中获取意外输出

Java 在程序中获取意外输出,java,exception,jdbc,Java,Exception,Jdbc,以下是代码: 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 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();
    }   

你知道……为什么会这样吗?

之所以会这样,是因为你的算法包括:

  • 遍历所有员工
  • 如果员工与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) {}
    }
    
    PreparedStatement stmt=null;
    试一试{
    stmt=新的PreparedStatement(“从regis中选择*,其中emp_id=?和password=?”;
    stmt.setString(1名员工);
    stmt.setString(2,密码);
    ResultSet rs=stmt.executeQuery();
    如果(rs.next()){
    System.out.println(“2>员工Id:+Employee+”&&Password:+Password);
    System.out.println(“3>此员工”+employee+”存在于数据库中,并且
    分别为setContentType(“文本/html”);
    PrintWriter out=resp.getWriter();
    输出。打印(“”);
    输出。打印(“”);
    打印(“保单页”);
    输出。打印(“”);
    输出。打印(“”);
    字符串状态=(字符串)请求getAttribute(属性_状态);
    如果(状态!=null)
    {
    打印(“状态:+状态”);
    }
    List devices=Datastore.getDevices();
    if(devices.isEmpty())
    {
    打印(“未注册设备!”);
    } 
    其他的
    {
    out.print(“+devices.size()+”已注册设备!”);
    输出。打印(“”);
    输出。打印(“”);
    分别设置状态(HttpServletResponse.SC_OK);
    输出。打印(“”);
    输出。打印(“”);
    //System.out.println(HTTP_状态);
    System.out.println(HttpServletResponse.SC_OK);
    getServletContext().getRequestDispatcher(“/home”).forward(req,resp);
    }
    输出。打印(“”);
    分别设置状态(HttpServletResponse.SC_OK);
    }
    否则{
    resp.setStatus(HttpServletResponse.SC_BAD_请求);
    System.out.println(HttpServletResponse.SC\u BAD\u请求);
    System.out.println(“4>此员工”+employee+“在数据库中不存在”);
    }
    }
    捕获(例外e){
    e、 printStackTrace();
    }
    最后{
    试一试{
    stmt.close();
    }捕获(异常x){}
    }
    
    之所以发生这种情况,是因为您的算法包括:

  • 遍历所有员工
  • 如果员工与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) {}
    }
    
    PreparedStatement stmt=null;
    试一试{
    stmt=新的PreparedStatement(“从regis中选择*,其中emp_id=?和password=?”;
    stmt.setString(1名员工);
    stmt.setString(2,密码);
    ResultSet rs=stmt.executeQuery();
    如果(rs.next()){
    System.out.println(“2>员工Id:+Employee+”&&Password:+Password);
    System.out.println(“3>此员工”+employee+”存在于数据库中,并且
    分别为setContentType(“文本/html”);
    PrintWriter out=resp.getWriter();
    输出。打印(“”);
    输出。打印(“”);
    打印(“保单页”);
    输出。打印(“”);
    输出。打印(“”);
    字符串状态=(字符串)请求getAttribute(属性_状态);
    如果(状态!=null)
    {
    打印(“状态:+状态”);
    }
    List devices=Datastore.getDevices();
    if(devices.isEmpty())
    {
    打印(“未注册设备!”);
    } 
    其他的
    {
    out.print(“+devices.size()+”已注册设备!”);
    输出。打印(“”);
    输出。打印(“”);
    分别设置状态(HttpServletResponse.SC_OK);
    输出。打印(“”);
    输出。打印(“”);
    //System.out.println(HTTP_状态);
    System.out.println(HttpServletResponse.SC_OK);
    getServletContext().getRequestDispatcher(“/home”).forward(req,resp);
    }
    输出。打印(“”);
    分别设置状态(HttpServletResponse.SC_OK);
    }
    否则{
    resp.setStatus(HttpServletResponse.SC_BAD_请求);
    System.out.println(HttpServletResponse.SC\u BAD\u请求);
    System.out.println(“4>此员工”+employee+“在数据库中不存在”);
    }
    }
    捕获(例外e){
    e、 printStackTrace();
    }
    最后{
    试一试{
    stmt.close();
    }捕获(异常x){}
    }
    
    您的缩进对您没有帮助。您正在遍历所有员工,并比较每个员工的用户名和密码-因此,有时您会找到匹配项,有时则不会

    此代码存在多个问题:

    • 如果只查找一个结果,不要向数据库询问所有行!您应该传递查询参数并在数据库中进行筛选。然后,您可以通过查看结果中是否有行来确定是否匹配
    • 你的凹痕让你很难看清发生了什么
    • 您使用了大量不必要的括号和与
      true
      的比较,例如

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

      if(employee.equals(rs.getString("emp_id") && 
         password.equals(rs.getString("password"))
      
    • 您似乎正在使用纯文本密码。不要这样做。


    你的缩进对你没有帮助。你在遍历所有员工,比较每个员工的用户名和密码,所以有时你会得到匹配,有时你不会

    有很多问题