[英]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.