0 Replies - 2958 Views - Last Post: 26 December 2011 - 12:49 PM

#1 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8380
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

A TableModel from a ResultSet

Posted 26 December 2011 - 12:49 PM

Description: Once you have a ResultSet you can buid a TableModel by doing
TableModelFromRS tmfr = new TableModelFromRS(rs);
With that tmfr you can build a new JTable by doing
JTable table = new JTable(tmfr);
or if the JTable already exists you can change its model by doing
table.setModel(tmfr);Greatly inspired from by code by Charles at
http://technojeeves....t-to-tablemodel
How to build a TableModel from a ResultSet
import javax.swing.table.*;
import java.sql.*;
import java.util.*;

public class TableModelFromRS extends AbstractTableModel {

	private static final long serialVersionUID = 1L;
	// the Column names based on the DB
	private String[] columnName;
	// ArrayList to hold each row composed of an array of Object
	private ArrayList<Object[]> al;
	
	TableModelFromRS(ResultSet rs) {
		try {
			// meta data to get info on the database
			ResultSetMetaData metaData = rs.getMetaData();
			
			// number of columns present in the ResultSet
			int nbCol = metaData.getColumnCount();
			columnName = new String[nbCol];
			// Load the Array of column names
			for (int i = 0; i < nbCol; ++i) {
				columnName[i] = metaData.getColumnLabel(i + 1); // +1 SQL columns start at 1
			}

			// Now get the rows
			al = new ArrayList<Object[]>();
			// while there are still rows in the ResultSet
			while (rs.next()) {
				Object[] row = new Object[nbCol];
				// retreive each column
				for (int i = 0; i < nbCol; i++) {
					row[i] = rs.getObject(i+1);				// +1 SQL columns start at 1
				}
				// insert into arrayList
				al.add(row);
			}

		} catch (Exception e) {
			System.out.println("Error building TableModel: " + e);
			e.printStackTrace();
		}
	}

    // the number of rows is the number of entries in the ArrayList
	public int getRowCount() {
		return al.size();
	}

	// the number of columms is the size of the the Array of column name
	public int getColumnCount() {
		return columnName.length;
	}

	// we retreive the array of Object[] at that index in the ArrayList
	// then the object at the required column
	public Object getValueAt(int rowIndex, int columnIndex) {
		return al.get(rowIndex)[columnIndex];
	}
	
	// the column name is in the String[] array of column name
	public String getColumnName(int columnIndex) {
		return columnName[columnIndex];
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: A TableModel from a ResultSet

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8380
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: A TableModel from a ResultSet

Posted 26 December 2011 - 12:49 PM

Description: Once you have a ResultSet you can buid a TableModel by doing
TableModelFromRS tmfr = new TableModelFromRS(rs);
With that tmfr you can build a new JTable by doing
JTable table = new JTable(tmfr);
or if the JTable already exists you can change its model by doing
table.setModel(tmfr);Greatly inspired from code by Charles at
http://technojeeves....t-to-tablemodel
How to build a TableModel from a ResultSet
import javax.swing.table.*;
import java.sql.*;
import java.util.*;

public class TableModelFromRS extends AbstractTableModel {

	private static final long serialVersionUID = 1L;
	// the Column names based on the DB
	private String[] columnName;
	// ArrayList to hold each row composed of an array of Object
	private ArrayList<Object[]> al;
	
	TableModelFromRS(ResultSet rs) {
		try {
			// meta data to get info on the database
			ResultSetMetaData metaData = rs.getMetaData();
			
			// number of columns present in the ResultSet
			int nbCol = metaData.getColumnCount();
			columnName = new String[nbCol];
			// Load the Array of column names
			for (int i = 0; i < nbCol; ++i) {
				columnName[i] = metaData.getColumnLabel(i + 1); // +1 SQL columns start at 1
			}

			// Now get the rows
			al = new ArrayList<Object[]>();
			// while there are still rows in the ResultSet
			while (rs.next()) {
				Object[] row = new Object[nbCol];
				// retreive each column
				for (int i = 0; i < nbCol; i++) {
					row[i] = rs.getObject(i+1);				// +1 SQL columns start at 1
				}
				// insert into arrayList
				al.add(row);
			}

		} catch (Exception e) {
			System.out.println("Error building TableModel: " + e);
			e.printStackTrace();
		}
	}

    // the number of rows is the number of entries in the ArrayList
	public int getRowCount() {
		return al.size();
	}

	// the number of columms is the size of the the Array of column name
	public int getColumnCount() {
		return columnName.length;
	}

	// we retreive the array of Object[] at that index in the ArrayList
	// then the object at the required column
	public Object getValueAt(int rowIndex, int columnIndex) {
		return al.get(rowIndex)[columnIndex];
	}
	
	// the column name is in the String[] array of column name
	public String getColumnName(int columnIndex) {
		return columnName[columnIndex];
	}

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1