java - JSP中IF语句执行错误

标签 java mysql jsp if-statement

这是我的 JSP 代码,用于使用数据库获取用餐的结束时间并将其与系统时间进行比较。我使用 if 语句来查看时间是否在结束时间之前,重定向到另一个页面,但如果时间超过用餐结束时间,则显示错误。

这里有一个问题,if 语句可以工作,但不是按照我刚才解释的方式工作。

有什么想法吗?提前致谢...

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <%
        String meal = request.getParameter("meal");

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        try{
            con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/eoms?autoReconnect=true&useSSL=false","root","eoms");
                 st=con.createStatement();

               String select="SELECT * FROM eoms.schedule;";

                rs = st.executeQuery(select);

                String mealName = null;
                Time dueTime = null;
                Time time = null;
                int found = 0;

                while(rs.next()){
                    mealName = rs.getString(1);
                    dueTime = rs.getTime(3);
                    java.util.Date d = new java.util.Date();
                    time = new Time(d.getTime());

                    if(meal.equalsIgnoreCase(mealName) && time.before(dueTime)){
                        session.setAttribute("mealName", mealName);
                        con.close();
                        found = 1;
                        response.sendRedirect("header.jsp");
                    }
                    else
                        continue;
                }
                if(found == 0){
                    session.setAttribute("orderror",meal+mealName+time+dueTime+ "The time for taking the order is over...");
                    con.close();
                    response.sendRedirect("placeOrder.jsp");
                }
        }
        catch(Exception e){
            out.println(e.getMessage());
        }
        %>
    </body>
</html>

最佳答案

正如@Andreas在评论中提到的,在response.sendRedirect("header.jsp")之后添加break并删除else continue; .

对于其余的,我建议使用java.time.LocalTime来获取实际时间,并使用这个Time将其转换为java.sql.Time .valueOf(java.time.LocalTime.now())

用这个替换最后一个java代码

 <%
        String meal = request.getParameter("meal");

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        try{
            con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/eoms?autoReconnect=true&useSSL=false","root","eoms");
            st=con.createStatement();

            String select="SELECT * FROM eoms.schedule;";

            rs = st.executeQuery(select);

            String mealName = null;
            Time dueTime = null;
            Time time = null;
            int found = 0;

            while(rs.next()){
                mealName = rs.getString(1);
                dueTime = rs.getTime(3);
                time = Time.valueOf( java.time.LocalTime.now() );
                if(meal.equalsIgnoreCase(mealName) && time.before(dueTime)){
                    session.setAttribute("mealName", mealName);
                    con.close();
                    found = 1;
                    response.sendRedirect("header.jsp");
                    break;
                }
            }
            if(found == 0){
                session.setAttribute("orderror",meal+mealName+time+dueTime+ "The time for taking the order is over...");
                con.close();
                response.sendRedirect("placeOrder.jsp");
            }
        }
        catch(Exception e){
            out.println(e.getMessage());
        }
%>

关于java - JSP中IF语句执行错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60619488/

相关文章:

java - JAXB 更新字符串列表

java - Intellij 构建时间太长。 Gradle 没有帮助

java - 是否可以在Java中构造一个Either,允许flatMap返回不同的Left值?

php - 根据 JSON 数据创建包含左侧和顶部标题的表格

mysql - 检索每组中的最后一条记录 - MySQL

javascript - onchange、onclick 事件在 Internet Explorer 11 中不起作用

jsp - Tomcat JSP 身份验证失败

java - 验证保存在 txt 文件 Java 中的用户名和密码

python - Django ORM 是否将删除的字段映射到 sql where 子句中的其他字段之前?

java - 如何在 HTML 中将任意文本水平和垂直居中放置在图像上?