【问题标题】:data being overwritten in **Result.next()** loop在 **Result.next()** 循环中被覆盖的数据
【发布时间】:2013-12-13 02:37:32
【问题描述】:

我有一个搜索按钮,可以进入我的数据库并搜索一个名称,但是当有两个相同名称的条目时,它只会让我返回一个,我不知道为什么。下面是我现在使用的代码。

 public boolean search(String str1, String username){//search function for the access history search with two parameters
    boolean condition = true;
    String dataSourceName = "securitySystem";//setting string datasource to the securitySystem datasource
    String dbUrl = "jdbc:odbc:" + dataSourceName;//creating the database path for the connection
    try{
        //Type of connection driver used    
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        //Connection variable or object param: dbPath, userName, password
        Connection con = DriverManager.getConnection(dbUrl, "", "");

        Statement statement = con.createStatement();//creating the statement which is equal to the connection statement

        if (!(username.equals(""))){
            PreparedStatement ps = con.prepareStatement("select * from securitysystem.accessHistory where name = ?");//query to be executed
            ps.setString(1, username);//insert the strings into the statement
            ResultSet rs=ps.executeQuery();//execute the query
            if(rs.next()){//while the rs (ResultSet) has returned data to cycle through
                JTable table = new JTable(buildTableModel(rs));//build a JTable which is reflective of the ResultSet (rs)
            JOptionPane.showMessageDialog(null, new JScrollPane(table));//put scrollpane on the table
        }
        else{
                JOptionPane.showMessageDialog(null,"There has been no system logins at this time");// else- show a dialog box with a message for the user
            }
}
statement.close();//close the connection
    } catch (Exception e) {//catch error
        System.out.println(e);
    }
    return condition;   
}

【问题讨论】:

  • 您使用的是if 而不是while。如果您正在循环结果集,请显示 buildTableModel
  • "buildTableModel(...) 到底做了什么?这种方法在这里是不是有点关键?
  • 因为你不显示buildTableModel() 没有人可以帮助你。
  • 如需尽快获得更好的帮助,请发帖SSCCE

标签: java swing jdbc jtable resultset


【解决方案1】:

这是一个示例,说明如何使用 TableModel 向表中添加数据。

假设你有这些初始模型条件

String[] columnNames = {"Column1", "Column2", "Column3"};
DefaultTableModel model = new DefaultTablModel(columnNames, 0);
JTable table = new JTable(model);

...

你应该做这样的事情

while (rs.next()){
    String s1 = rs.getString(1);
    String s2 = rs.getString(2);
    String s3 = rs.getString(3);

    Object[] row = {s1, s2, s3};

    model.addRow(row);
}

编辑:要直接从数据库中获取列名,您需要使用ResultSetMetaData

试试这个方法。

public DefaultTableModel buildTableModel(ResultSet rs){

    ResultSetMetaData rsMeta = rs.getMetaData();
    int cloumns = rsMeta.getColumnCount();

    String columnNames = new String[columns];
    for (int i = 1; i <= columns; i++){
        columnNames[i - 1] = rsMeta.getColumnName(i);
    }

    DefaultTableModel model = new DefaultTableModel(columnNames, 0);

    while (rs.next()){
        // Just an example retrieving data. Fill in what you need
        String s1 = rs.getString(1);
        String s2 = rs.getString(2);
        String s3 = rs.getString(3);

        Object[] row = {s1, s2, s3};

        model.addRow(row);
        // End example
    }

    return model;
}

那么,

if (rs != null){
    JTable table = new JTable(buildTableModel(rs));
}

【讨论】:

    【解决方案2】:

    您的代码调用rs.next() 的次数过多。在上面的搜索方法中调用 buildTableModel(...) 方法之前调用它,实际上是在浪费一行你从不使用的数据。

    相反,只需将 ResultSet 传递给方法而不调用 next,然后使用 buildTableModel(...) 内的 while 循环来填充数据向量。如果向量为空,则抛出异常,或者返回一个 0 行的 TableModel。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      相关资源
      最近更新 更多