Java ResultSet to TableModel resultSetToTableModel(ResultSet rs)

Here you can find the source of resultSetToTableModel(ResultSet rs)

Description

result Set To Table Model

License

Open Source License

Declaration

public static TableModel resultSetToTableModel(ResultSet rs) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;

public class Main {
    public static TableModel resultSetToTableModel(ResultSet rs) {
        try {/*from  w  w  w .j a v  a2  s . c  o  m*/
            ResultSetMetaData metaData = rs.getMetaData();
            int columns = metaData.getColumnCount();
            Vector<String> columnNames = new Vector<>();

            for (int i = 0; i < columns; i++) {
                columnNames.addElement(metaData.getColumnLabel(i + 1));
            }

            Vector<Vector<Object>> rows = new Vector<>();

            while (rs.next()) {
                Vector<Object> newRow = new Vector<>();

                for (int i = 1; i <= columns; i++) {
                    newRow.addElement(rs.getObject(i));
                }

                rows.add(newRow);
            }

            return new DefaultTableModel(rows, columnNames);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related

  1. resultSetToTableModel(ResultSet paramResultSet)
  2. resultSetToTableModel(ResultSet rs)