java resultset to table

Solutions on MaxInterview for java resultset to table by the best coders in the world

showing results for - "java resultset to table"
Luis
03 Nov 2018
1public static DefaultTableModel buildTableModel(ResultSet rs)
2        throws SQLException {
3
4    ResultSetMetaData metaData = rs.getMetaData();
5
6    // names of columns
7    Vector<String> columnNames = new Vector<String>();
8    int columnCount = metaData.getColumnCount();
9    for (int column = 1; column <= columnCount; column++) {
10        columnNames.add(metaData.getColumnName(column));
11    }
12
13    // data of the table
14    Vector<Vector<Object>> data = new Vector<Vector<Object>>();
15    while (rs.next()) {
16        Vector<Object> vector = new Vector<Object>();
17        for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
18            vector.add(rs.getObject(columnIndex));
19        }
20        data.add(vector);
21    }
22
23    return new DefaultTableModel(data, columnNames);
24
25}