else语句不使用Object方法调用

时间:2018-03-12 02:27:37

标签: java servlets java-ee

出于某种原因,我有一个不想运行的else if语句。条件(我认为)已经满足。

    int codeCheck = Integer.parseInt(itemCode);
    DataAccessImpl da = new DataAccessImpl();
    if(!rs.wasNull()) {
        while(rs.next()) {

            Products temp = new Products(rs.getString(1), rs.getString(2), rs.getDouble(3), rs.getBoolean(4));
            productsList.add(temp);
            request.setAttribute("productsList", productsList);
            request.getRequestDispatcher("Scan.jsp").forward(request, response);
        }
    } 
    else if(da.codeExists(codeCheck) == false){
        Products wrongCode = null;
        productsList.add(wrongCode);
        System.out.println("THIS IS EXECUTING!");
        request.setAttribute("productsList", productsList);
        request.getRequestDispatcher("Scan.jsp").forward(request, response);
    }

这是DataAccessImpl类

中的方法
public boolean codeExists(int code) {
    final String SELECT_QUERY = "SELECT * FROM productcatalogue WHERE code = ?";
        try {

            PreparedStatement statement = conn.prepareStatement(SELECT_QUERY); // Create query statement
            statement.setInt(1, code);

            ResultSet queryResult = statement.executeQuery();
            if (!queryResult.next()) { // Check if code entered exists
                return false;
            }
        } catch (SQLException sqlException) {

            sqlException.printStackTrace();

        }
        return true; //Otherwise the item exists
    }

1 个答案:

答案 0 :(得分:0)

如果你确定rs肯定是空的并且第一个如果不满足那么肯定它将会转到你的其他地方,如果并且将检查是否(da.codeExists(codeCheck) == false)如果这也是真的那么它肯定会执行所以你做确定第一个if是false,否则如果是真的,那么根据设计模式它将不会被执行。