简体   繁体   English

如何检查输入的值是否等于sql数据库中的值

[英]How to check if entered value is equal to a value in a sql database

edit2: I've wrote this method to solve the problem: edit2:我已经编写了此方法来解决问题:

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; // Tells that the author doesn't exist
            }
        } catch (SQLException sqlException) {

            sqlException.printStackTrace();

        }
        return true; //Otherwise the item exists
    }

edit: I now know that ResultSet never returns null. 编辑:我现在知道ResultSet永远不会返回null。 It will return an empty ResultSet. 它将返回一个空的ResultSet。

I have a database with 7 rows, and 4 columns in each. 我有一个数据库,其中有7行,每行4列。 I am prompting a user to enter a code, and I need to cross reference the database to see if the entered code equals one of the codes in the database (first column in each row). 我提示用户输入代码,我需要交叉引用数据库以查看输入的代码是否等于数据库中的代码之一(每行的第一列)。

At first, I was checking if the result set came up as null, but that was not working. 起初,我正在检查结果集是否为空,但这没有用。 I was trying if(rs.wasNull()) but it never seemed to execute. 我正在尝试if(rs.wasNull())但它似乎从未执行过。

Does anybody have any advice on what to look at to solve my issue? 有人对解决我的问题有什么建议吗? Here's a code snippet if helps at all. 如果有帮助,请参见以下代码段。

    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);
            // For testing purposes
            //out.println(temp.toString());
            request.setAttribute("productsList", productsList);
            request.getRequestDispatcher("Scan.jsp").forward(request, response);
        }
    }

    else if(rs.wasNull()) {
        Products wrongCode = null;
        productsList.add(wrongCode);    
        request.setAttribute("productsList", productsList);
        request.getRequestDispatcher("Scan.jsp").forward(request, response);
    }

If the result is empty it will never go into while loop. 如果结果为空,它将永远不会进入while循环。 This is a condition which is used to separate code logic. 这是用于分隔代码逻辑的条件。 Calling additional code on the result set is useless. 在结果集上调用其他代码是没有用的。

You should check the jdbc api for ResultSet#wasNull() : 您应该检查JDBC API的ResultSet#wasNull()

wasNull boolean wasNull() throws SQLException Reports whether the last column read had a value of SQL NULL. wasNull boolean wasNull()throws SQLException报告最后读取的列是否具有SQL NULL值。 Note that you must first call one of the getter methods on a column to try to read its value and then call the method wasNull to see if the value read was SQL NULL. 请注意,必须首先在列上调用getter方法之一以尝试读取其值,然后调用wasNull方法以查看读取的值是否为SQL NULL。 Returns: true if the last column value read was SQL NULL and false otherwise Throws: SQLException - if a database access error occurs or this method is called on a closed result set 返回:如果最后读取的列值为SQL NULL,则返回true;否则,返回false。抛出:SQLException-如果发生数据库访问错误或在封闭的结果集上调用此方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查输入的值是否存在于数据库android中 - Check if value entered exists in database android 如何检查是否输入了值,如果没有输入则重新提示它们 - How to check if value is entered, and if nothing is entered reprompt them 如何检查一个值是否等于三个值中的任何一个 - How to check a value is equal to any of the three values 检查数据库字符串中的int是否等于某个值 - Check if int from database string is equal to certain value 您如何检查用户输入的是字符串值而不是数字? - How do you check that the user entered a String value instead of a number? 如何使用Java中的hasNextInt来检查输入的值是否为integer? - How to use hasNextInt in Java to check if the entered value is integer or not? 如何检查输入的字符串是否包含Unicode:0x1a值 - How to check that entered string contains Unicode: 0x1a value 如何检查Java中的文本字段中是否输入了某个值 - How to check whether a certain value is entered in a textfield in Java 如何检查两个不同数组的相等索引并输入正确的值 - How to check the equal indexes of two different Arrays and put the correct value 如何检查ArrayList的元素是否等于Java中的另一个值? - How can I check if the elements of an ArrayList equal another value in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM