简体   繁体   English

ResultSet仅将最后一行返回JTable

[英]ResultSet returns only last row into JTable

I have done enough searches to solve my problem which i have done partly but there's this one bug that keeps disturbing me.I am trying to fetch data from a database based on a condition.I have a table 'user_branch' with a foreign key column branchID which is supposed to fetch the coresponding branchNames in another table 'branches' and I am supposed to display the results into a JTable.When i do System.out.println i get all my results but it returns only the last row when i display in a JTable(branchJTable).This is the code i am using 我已经做了足够的搜索来解决我已经部分完成的问题,但是这个错误一直困扰着我。我试图根据条件从数据库中获取数据。我有一个带有外键列的表'user_branch' branchID应该在另一个表'branches'中获取核心对应的branchNames,并且我应该将结果显示到JTable中。当我执行System.out.println时,我得到了所有结果,但是当我显示时它仅返回最后一行在JTable(branchJTable)中。这是我正在使用的代码

 int row = user2BAssignedJTable.getSelectedRow();
    assignUserID.setText(user2BAssignedJTable.getModel().getValueAt(row, 0).toString());
    user2BAssignedField.setText(user2BAssignedJTable.getModel().getValueAt(row, 1).toString());

    try {

        String userBrQry = "SELECT branchID FROM user_branch WHERE userID IN(?) ";
        String brQ = "SELECT branchName FROM branches WHERE branchID IN(%s) ";

        pstmt = con.prepareStatement(userBrQry);
        pstmt.setString(1, assignUserID.getText());
        results = pstmt.executeQuery();

        results.last();
        int nRows = results.getRow();
        results.beforeFirst();

        while (results.next()) {
            String branchIDS = results.getString("branchID");

            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < nRows; i++) {
                builder.append("?");
                if (i + 1 < nRows) {
                    builder.append(",");
                }
            }
            brQ = String.format(brQ, builder.toString());
            PreparedStatement ps = con.prepareStatement(brQ);
            for (int i = 0; i < nRows; i++) {
                ps.setString(i + 1, branchIDS);
            }
            ResultSet rs = ps.executeQuery();
            //branchJTable.setModel(DbUtils.resultSetToTableModel(rs));                

            javax.swing.table.DefaultTableModel model = new javax.swing.table.DefaultTableModel();
            model.setColumnIdentifiers(new String[]{"Branch Name"});
            branchJTable.setModel(model);

            while (rs.next()) {
                String branchname = rs.getString("branchName");
                model.addRow(new Object[]{branchname});                  

            }

        }

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    } 

Forget about the first 3 rows as it is a another JTable event i use to get the userID to use as a condition for getting a particular user's branches assigned to him. 忘记前3行,因为这是另一个JTable事件,我用它来获取userID,以用作获取分配给他的特定用户分支的条件。 The branches assigned to a user is dynamic hence using StringBuilder. 分配给用户的分支是动态的,因此使用StringBuilder。 I am supposed to display the results into another JTable called branchJTable which only displays the last row.Any HELP would be appreciated! 我应该将结果显示到另一个名为branchJTable的JTable中,该JTable仅显示最后一行。任何帮助将不胜感激!

From your question, I think you should declare the JTable 根据您的问题,我认为您应该声明JTable

javax.swing.table.DefaultTableModel model = new javax.swing.table.DefaultTableModel();
model.setColumnIdentifiers(new String[]{"Branch Name"});
branchJTable.setModel(model);

before your first loop - 在您的第一个循环之前-

ie before while (results.next()) { in your code. 即在代码中的while (results.next()) {之前。

Otherwise in loop, for each loop execution, 否则在循环中,对于每个循环执行,

the JTable Model is initialising and you are getting the last inserted row in Jtable. JTable模型正在初始化,您将在Jtable中获得最后插入的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM