MrZooYork
MrZooYork

Reputation: 17

Displaying multiple values from a ResultSet in a single JTable cell

im currently stuck on my project wherein i need to generate a daily report. I need to display all the fault number that is involved in a call for the whole day.

Heres what i currently have

| Number of calls from ISG |  2  |    |
| Fault Numbers            |     |  x |

Here x should be those 2 fault numbers i need to display all the fault numbers from my mySQL database. for i need to display 2 fault numbers from ISG IN A SINGLE LINE on the 3rd column separated by comma. How do i fetch those 2 results because on my first try, only the first row was retrieved.

For the number of calls, here is my method where q is the query.

public void dailyResult(String q, int x, int y){

    try{
        Statement stmtDr = (Statement)daily.createStatement();
        ResultSet rs = stmtDr.executeQuery(q);
    if(rs.next()){
        reportTable1.setValueAt(rs.getInt(1), x, y);
    }
    }catch(Exception e){
                   JOptionPane.showMessageDialog(rootPane, "Error 106\n\nAn error has occured with the resultset procedure method. Please try again later.", "Error!", JOptionPane.ERROR_MESSAGE);
    }
}

With regards to displaying those numbers in a single jtable cell, i dont know how to do such task. Im a beginner in mySQL queries and am very confused also with how the ResultSet class works, any help would be great, cheers!

Upvotes: 0

Views: 1146

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

You shouldn't be interacting with the table, but with the table's model.

If you're using a DefaulTableModel, you could take advantage of the addRow method, who would allow you to add a new row to the table (via the model), else you'll need to supply an implementation of the TablModel that has the appropriate methods to allow you to update the underlying data structure

Take a close look at How to use tables for more details

The ResultSet class basically has a concept of a "current" row, which allows you to extract individual values from the resulting columns (as specified by your original query). These can be extract either by order or by name, depending on your needs

Upvotes: 3

Related Questions