📜  java 结果集到表 - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:12.697000             🧑  作者: Mango

代码示例1
public static DefaultTableModel buildTableModel(ResultSet rs)
        throws SQLException {

    ResultSetMetaData metaData = rs.getMetaData();

    // names of columns
    Vector columnNames = new Vector();
    int columnCount = metaData.getColumnCount();
    for (int column = 1; column <= columnCount; column++) {
        columnNames.add(metaData.getColumnName(column));
    }

    // data of the table
    Vector> data = new Vector>();
    while (rs.next()) {
        Vector vector = new Vector();
        for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
            vector.add(rs.getObject(columnIndex));
        }
        data.add(vector);
    }

    return new DefaultTableModel(data, columnNames);

}