java 检查数据库中是否存在该记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14257368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
check if the record exists in database
提问by Sumeet Gavhale
I need to check the box no if exist in the database under the remittance id that I enter if the box no exists then i need to show the message that the box no already exists but if it doesn't the it should insert new box i have written some code but its showing error
我需要在我输入的汇款 ID 下的数据库中检查是否存在框,如果框不存在,那么我需要显示框没有已经存在的消息,但如果它不存在,它应该插入新框我写了一些代码,但显示错误
private void txtboxnoFocusLost(java.awt.event.FocusEvent evt) {
DBUtil util = new DBUtil();
try {
Connection con = util.getConnection();
PreparedStatement stmt = con.prepareStatement(
"select box_no from dbo.soil_det where rm_id = ? and box_no = ?");
stmt.setLong(1, Long.parseLong(tf_rm_id.getText()));
stmt.setString(1, (txtboxno.getText()));
ResultSet rs=stmt.executeQuery();
while(rs.next()){
rs.equals().txtboxno.getText());
}
JOptionPane.showMessageDialog(rootPane, "hello!S");
} catch (Exception ex) {
Logger.getLogger(DATAENTRY.class.getName()).log(Level.SEVERE, null, ex);
}
采纳答案by Rahul
Try this code
试试这个代码
private void txtboxnoFocusLost(java.awt.event.FocusEvent evt) {
DBUtil util = new DBUtil();
try {
Connection con = util.getConnection();
PreparedStatement stmt = con.prepareStatement(
"select box_no from dbo.soil_det where rm_id = ? and box_no = ?");
stmt.setLong(1, Long.parseLong(tf_rm_id.getText()));
stmt.setString(2, (txtboxno.getText()));
ResultSet rs=stmt.executeQuery();
bool recordAdded = false;
while(!rs.next()){
/// Do your insertion of new records
recordAdded = true;
}
if( recordAdded ){
JOptionPane.showMessageDialog(rootPane, "Record added");
}else{
JOptionPane.showMessageDialog(rootPane, "Record already exists");
}
} catch (Exception ex) {
Logger.getLogger(DATAENTRY.class.getName()).log(Level.SEVERE, null, ex);
}
回答by Brian Agnew
You need to get the appropriate record from the ResultSet
e.g.
您需要从ResultSet
eg 中获取适当的记录
boolean found = rs.getString(1).equals().txtboxno.getText());
At the moment you're simply comparing the ResultSet
object itself to a string, and that won't work. The above pulls the first record from the ResultSet
and performs the comparison on that (note: your datatype may be different and you may need rs.getInt(1)
etc.)
目前,您只是将ResultSet
对象本身与字符串进行比较,这是行不通的。以上从 中提取第一条记录ResultSet
并对其进行比较(注意:您的数据类型可能不同,您可能需要rs.getInt(1)
等)
Perhaps its sufficient in your case just to check if you have a ResultSet
result (via rs.next()
)
也许在您的情况下足以检查您是否有ResultSet
结果(通过rs.next()
)
回答by B11
or you could use a count:
或者您可以使用计数:
String query = "select count(*)
from dbo.soil_det where rm_id = ? and box_no = ?";
then after executing the query you get the count with
然后在执行查询后你得到计数
rs.getInt(1)
using that you can decide which info to show to the user
使用它,您可以决定向用户显示哪些信息
回答by Prafulla
very First You have to get count using sql if count is greater than zero then do not insert records and show message like already exists and in else part insert record. see following example
首先,如果计数大于零,您必须使用 sql 获取计数,然后不要插入记录并显示消息,如已存在,在其他部分插入记录。看下面的例子
private boolean findCount(int rm_id,String box_no)
{
int count=0;
//write query here
count = assign query result;
//check count
if(count>0)
{
return false;//records exists
}else{
return true;//records do not exists
}
}
}
public void insertData()
{
if(findCount(1,"1")){//pass values
//Write down your insert logic
}else{
JOptionPane.showMessageDialog(rootPane, "Records Already Exists");
}
}
Note: Friend in Your Example you have not written the insert logic. only select was there
注意:您的示例中的朋友您还没有编写插入逻辑。只有选择在那里
回答by pgras
First you could add -- on the db table -- a unique constrain on the columns (rm_id, box_no), this is anyway a good thing to do.
首先,您可以添加 -- 在 db 表上 -- 对列 (rm_id, box_no) 的唯一约束,无论如何这是一件好事。
Then you could simply try to insert the box and catch the exception and check if it is a violation of the unique constraint.
然后您可以简单地尝试插入该框并捕获异常并检查它是否违反了唯一约束。
Another option (still keeping the unique constraint) would be to make a more complicated SQL insert statement that inserts only if not existing, you may google "sql insert if not exist" to find some examples...
另一种选择(仍然保持唯一约束)是制作一个更复杂的 SQL 插入语句,该语句仅在不存在时插入,您可以谷歌“sql insert if not exist”以查找一些示例...