在继续之前检查jtable中的重复数据 [英] Check duplicate data in jtable before proceeding

查看:279
本文介绍了在继续之前检查jtable中的重复数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我目前的项目中,我有一个包含4列的jtable。我不希望在第二列中存储重复数据,因此我在将其存储到数据库之前检查jtable中的每一行。

In my current project I have a jtable with 4 columns. I don't want store duplicate data in second column, so I check each row in the jtable before I store it into to database.

这是我的代码:

String s="";
int row = table.getRowCount();
for (int i=0; i<row; i++) 
{
    s = table.getValueAt(i, 1).toString().trim();
    if (name.getText().equals(s)) 
    {
      JOptionPane.showMessageDialog(null, "data alreadyexist.","message",JOptionPane.PLAIN_MESSAGE);
    break; 
    } else {
      add();
      break;
    }
} 


推荐答案

尝试这个:

    int row = jTable1.getRowCount();
    Object[] content = new Object[row];
    for (int i = 0; i < row; i++) {
        content[i] = jTable1.getValueAt(i, 0);
    }
    Object value_to_find= 2000;
    boolean exist = Arrays.asList(content).contains(value_to_find);
    if (exist){
        JOptionPane.showMessageDialog(null, "Value exist", "Duplicate", JOptionPane.ERROR_MESSAGE);
    } else {
        addRow();
    }

使用Arrays.asList(内容)保存数组中的所有行,而不是搜索value_to_find ).contains(value_to_find)

Save all row in an array than search for value_to_find using Arrays.asList(content).contains(value_to_find)

这篇关于在继续之前检查jtable中的重复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆