如何检查 ResultSet 中是不是存在字段?
Posted
技术标签:
【中文标题】如何检查 ResultSet 中是不是存在字段?【英文标题】:How can I check if field exists in a ResultSet?如何检查 ResultSet 中是否存在字段? 【发布时间】:2021-08-19 08:57:47 【问题描述】:我想通过输入到文本字段中的 ID 来查找用户,如果找不到 ID,我想显示“未找到用户”。
这是我目前所拥有的,但只有在输入的文本字段大于表格中的 MAX id 时才会显示错误:
Statement stmt7 = connection.createStatement();
String query2 = "SELECT MAX(empid) AS MaxId FROM employee";
ResultSet rs2 = stmt7.executeQuery(query2);
while(rs2.next())
if(Integer.parseInt(searchEmployeeFld.getText()) > rs2.getInt("MaxId"))
// create a alert
Alert a = new Alert(AlertType.NONE);
// set alert type
a.setAlertType(AlertType.ERROR);
// set content text
a.setContentText("User Not Found");
// show the dialog
a.show();
else
Statement stmt6 = connection.createStatement();
System.out.println("Executing a Query...");
String query = "SELECT empname, empgrsal "
+ "FROM employee WHERE empid = " + searchEmployeeFld.getText();
ResultSet rs1 = stmt6.executeQuery(query);
while(rs1.next())
NameFld.setText(rs1.getString("empname"));
String test1 = Double.toString(rs1.getDouble("empgrsal"));
grossSalaryFld.setText(test1);
// calculate net salary
double grossSal = rs1.getDouble("empgrsal");
grossSal -= (grossSal * 0.3);
String test2 = Double.toString(grossSal);
netSalaryFld.setText(test2);
rs1.close();
rs2.close();
【问题讨论】:
请了解准备好的语句和参数化查询。当您将字符串值连接到查询字符串中时,您的代码容易受到 SQL 注入的影响。至于您的问题:您应该查询数据库中是否存在具有指定ID的记录(或者如果您有合适的约束,只需尝试插入并处理主键约束违规)。 为什么要先执行一个查询来找到最大的id?只需执行第二个查询并处理它没有返回任何行的情况。 这能回答你的问题吗? Fastest way to determine if record exists 【参考方案1】:这是您的代码的修改和注释版本,应该可以满足您的需求。
// Use a prepared statement, with a ? in the place of empid
// This will protect from SQL injection attacks, and more, it will
// prevent your database SGA from getting choked full of thousands of
// similar SQL statements. This is much more efficient from a DB perspective.
String query = "SELECT empname, empgrsal FROM employee WHERE empid = ?";
// A flag to see if we got the user
boolean found = false;
// Using "try with resources" makes life much easier
try (PreparedStatement ps1 = connection.prepareStatement(query))
// You have to set the value for the "?" in the SQL statement.
// If an employee ID is a number, then you really should convert
// the text to a number and use "setLong" instead of "setString"
ps1.setString(1,searchEmployeeFld.getText());
try(ResultSet rs1 = ps1.executeQuery())
// No need to loop. Your code is designed to find one user by key.
if(rs1.next())
found = true;
NameFld.setText(rs1.getString("empname"));
String test1 = Double.toString(rs1.getDouble("empgrsal"));
grossSalaryFld.setText(test1);
// calculate net salary
double grossSal = rs1.getDouble("empgrsal");
grossSal -= (grossSal * 0.3);
String test2 = Double.toString(grossSal);
netSalaryFld.setText(test2);
// Should catch SQLException somewhere and do something about it
if(!found)
// Didn't find the ID...do something about it
还有一些注意事项...
您(可能)在 Swing GUI 线程中执行 SQL,这不是最佳的。对于快速查询来说,它并没有那么糟糕,但是用户会感觉到任何延迟都是冻结的 UI。如果您不在 GUI 线程中,还有其他问题(对 Swing 对象的调用应该在 GUI 线程中)。
“使用资源尝试”技术将自动关闭准备好的语句和结果集。
【讨论】:
以上是关于如何检查 ResultSet 中是不是存在字段?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Spring JDBC ResultSetExtractor 中检查 ResultSet 是不是为空? [复制]