The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
December 2000

Advertisement

Advertisement
where you can ask and answer Java programming questions.">
artima.com - a resource for Java and Jini developers
Artima | Search | Java | Design | JVM | Jini | Books | Seminars | Subscribe 


Java Answers Forum
December 2000

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Here is the table you need!!!

Posted by Kishori Sharan on December 10, 2000 at 1:03 PM

Yes, the resultset of a query can be populated in a JTable. There are two ways of doing this.
1) You can store all the data from resultset in a two dimensional array of Object and then pass this array to Jtable constructor.
Object[]][] data ;;
// Populate the data array from resultset
String[] columnLabels = { "col1", "col2"... } ;
JTable tt = new JTable ( data, columnLabels )

2) You can build your own table model which will be queried by JTable for data in each cell.How to build a table model? Table model will be a class which inherits AbstractTableModel class and will override at least three of its methods viz.
public int getColumnCount ( ) ;
public int getRowCount ( ) ;
public Object getValueAt ( int rowNo, int coulnmNo ) ;
If you just override these three methods then your column's header will be given as A, B, C ... . So if you want to supply your own column headers ( certainly you would ) then you need to override another method
public String getColumnName ( int col ) ;
By methods names you can guess what do you need to write in those methods. However, I will give you hints for every methods. Your table model class will be as follows.
class MalaTableModel extends AbstractTableModel {
private ResultSet rs ;
private ResultSetMetaData rsmd ; // You need to populate it
public MalaTableModel ( ResultSet rs ) {
this.rs = rs ;
// Get the meta data for result set here
}
public int getRowCount ( ) {
// If you are using ODBC2.0 then you can get the
// row count from a result set as follows
rs.last ( ) ;
int rowCount = rs.getRow ( ) ;
return rowCount ;
// if rs.last ( ) is not supported then
/*
Keep scrolling from 1st to last row and return the total
no of rows
*/
}
public int getColumnCount ( ) {
return rsmd.getColumnCount ( ) ;
}
public getValueAt ( int row, int col ) {
// Scroll the result set to the row
rs.absolute ( row + 1 ) ;
return rs.getObject ( col + 1 ) ;
}
public String getColumnName ( int col ) {
return rsmd.getColumnName ( col + 1 ) ;
}
}

//////////////////////////////////////////////////////////////////
I haven't used try-catch in above code. But you need to use them
////////////////////////////////////////////////////////////////

Before using this table model you need to connect to database and fetch the resultset. Then you can write the following code
MalaTableModel mtm = new malaTableModel ( your_result_set ) ;
// Don't forget to populate the MetaResultSet object in constructor of
// your table model
// Create JTable
JTable jt = new JTable ( mtm ) ;
Now you can add this table jt anywhere you want and your data from resultset will be displayed in this table.
Note: Your resultset has to be scrollable in both direction ( forwatrd and backward ) .

Thanx
Kishori



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us