简体   繁体   中英

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

 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. The branches assigned to a user is dynamic hence using StringBuilder. I am supposed to display the results into another JTable called branchJTable which only displays the last row.Any HELP would be appreciated!

From your question, I think you should declare the 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.

Otherwise in loop, for each loop execution,

the JTable Model is initialising and you are getting the last inserted row in Jtable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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