如何检查 jFrame 的 MySQL table 中是否已经存在唯一值?
How can I check if a unique value is already in a MySQL table from jFrame?
我在 jFrame 按钮上有这个:
label.setVisibility(false);
ResultSet rs = state.executeQuery("SELECT Str_Column FROM Table WHERE Str_Column = '"+ jTextField.getText() +"'");
//check for the existance
if(rs.getString("Str_Column").equals(jTextField.getText())){
label.setVisibility(true);
}
else{
new frameForSucceedInput.setVisibility(true);
}
如果 if-case 为真或不总是显示成功 window 那是我不想发生的事情,它告诉我们一些关于 ResultSet
null 原因冲突所以我尝试了另一种方式:
if(state.execute("SELECT Str_Column FROM Table WHERE Str_Column = '"+ jTextField.getText() +"'")){
label.setVisibility(true);
}
else{
new frameForSucceedInput.setVisibility(true);
}
state.execute("SQL")
方法说:
return true if the query contains results, return false if the query is a update or no contain results
我已经尝试了 rs.next()
并且很多事情都喜欢而不是 else 做另一个 if(rs==null)
但不起作用,请帮助
一种非常通用且更轻松的方法是使用 SQL 计数函数。
select count(*) from table where str_Column = ?
这样你会得到一个记录和一个值,如果值大于 0 那么你可以显示框架。
注:
- 您应该使用 prepared statement 以便您可以在查询中设置参数(您注意到问号了吗?)而不是连接查询的元素。
- 您不应该将与演示文稿 (Swing) 相关的代码与与数据库相关的代码混在一起...但我认为您稍后会学到它。
我在 jFrame 按钮上有这个:
label.setVisibility(false);
ResultSet rs = state.executeQuery("SELECT Str_Column FROM Table WHERE Str_Column = '"+ jTextField.getText() +"'");
//check for the existance
if(rs.getString("Str_Column").equals(jTextField.getText())){
label.setVisibility(true);
}
else{
new frameForSucceedInput.setVisibility(true);
}
如果 if-case 为真或不总是显示成功 window 那是我不想发生的事情,它告诉我们一些关于 ResultSet
null 原因冲突所以我尝试了另一种方式:
if(state.execute("SELECT Str_Column FROM Table WHERE Str_Column = '"+ jTextField.getText() +"'")){
label.setVisibility(true);
}
else{
new frameForSucceedInput.setVisibility(true);
}
state.execute("SQL")
方法说:
return true if the query contains results, return false if the query is a update or no contain results
我已经尝试了 rs.next()
并且很多事情都喜欢而不是 else 做另一个 if(rs==null)
但不起作用,请帮助
一种非常通用且更轻松的方法是使用 SQL 计数函数。
select count(*) from table where str_Column = ?
这样你会得到一个记录和一个值,如果值大于 0 那么你可以显示框架。
注:
- 您应该使用 prepared statement 以便您可以在查询中设置参数(您注意到问号了吗?)而不是连接查询的元素。
- 您不应该将与演示文稿 (Swing) 相关的代码与与数据库相关的代码混在一起...但我认为您稍后会学到它。