How to Mount a Jtable with ResultSet?

-1

I have an example:

Object rowData[][] = { { "Row1-Column1", "Row1-Column2", "Row1-Column3" },{ "Row2-Column1", "Row2-Column2", "Row2-Column3" } };
Object columnNames[] = { "Posição", "Nome", "Pontuação" };
JTable table = new JTable(rowData, columnNames);

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(255, 400);
frame.setVisible(true);

I need to do rowData get a ResultSet , do not dominate java, and even now while could not make rowData get my ResultSet

if(rs != null) {
    while(rs.next()) {
        // rowData = ?? 
    }
}
    
asked by anonymous 20.11.2014 / 18:05

1 answer

-1

I managed this way:

String[] colunasTabela = new String[]{ "Posição", "Nome", "Pontuação" };  
DefaultTableModel modeloTabela = new DefaultTableModel(null,colunasTabela);

if(rs != null) {
    while(rs.next()) {
        modeloTabela.addRow(new String[] {  
                rs.getInt("ranking"),  
                rs.getString("nome"),  
                rs.getInt("score")
            }); 
    }
}

JTable table = new JTable();
table.setModel(modeloTabela);

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(255, 400);
frame.setVisible(true);
    
20.11.2014 / 18:54