aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Loading a JTable with SQL result set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Loading a JTable with SQL result set" Watch "Loading a JTable with SQL result set" New topic
Author

Loading a JTable with SQL result set

Jim Buck
Greenhorn

Joined: Feb 05, 2004
Posts: 4
Hi,
I have listed the code that I use to return a recordset from an AS400 table. This code returns a recordset and I use a method to printout the results. I have searched the net to find an example of how to load this resultset into a JTable. This seems like a simple thing.... the only examples I can find is doing it with an array. How would I use the default table model to do this?
Thanks,
Jim

/**
* Load the AS/400 Toolbox for Java JDBC driver.
*/
DriverManager.registerDriver(new com.ibm.as400.access.AS400JDBCDriver());

// Get a JDBC connection to the System
connection = DriverManager.getConnection("jdbc:as400://" + system, user, password);
DatabaseMetaData dmd = connection.getMetaData();

// Execute the query.
Statement select = connection.createStatement();
ResultSet rs = select.executeQuery("SELECT * FROM " + collectionName + dmd.getCatalogSeparator() + tableName);

/**
* Get information about the result set.Set the column width to
* whichever is longer: the length of the label or the length of the
* data.
*/

ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();

int[] columnWidths = new int[columnCount];
String[] columnLabels = new String[columnCount];


/* Call a method to Print out the results of the SQL resulr set - used for testing */
printRecordSet(rs, rsmd, columnCount, columnWidths, columnLabels);


/* This is the method called above - it correctly ptints out the recordset */

private void printRecordSet(ResultSet rs, ResultSetMetaData rsmd,
int columnCount, int[] columnWidths, String[] columnLabels) throws SQLException
{
// Print out the Labels
for (int i = 1; i < columnCount; ++i)
{
//columnLabels[i - 1] = rsmd.getColumnLabel(i);
System.out.println("Label" + columnLabels[i]);
}
// myTableModel = jTableListData.tableChanged(e)bleModel;

for (int i = 1; i <= columnCount; ++i)
{
columnLabels[i - 1] = rsmd.getColumnLabel(i);
columnWidths[i - 1] = Math.max(columnLabels[i - 1].length(), rsmd.getColumnDisplaySize(i));
}
// Output the column headings.
for (int i = 1; i <= columnCount; ++i)
{
try
{
System.out.print(format(rsmd.getColumnLabel(i), columnWidths[i - 1]));

} catch (SQLException e)
{
System.out.println();
System.out.println("ERROR: " + e.getMessage());
}
System.out.print(" ");
}
System.out.println();

// Output a dashed line.
for (int i = 1; i <= columnCount; ++i)
{
for (int j = 1; j <= columnWidths[i - 1]; ++j)
System.out.print("-");
System.out.print(" ");
}
System.out.println();

// Iterate throught the rows in the result set and output
// the columns for each row.
try
{
while (rs.next())
{
for (int i = 1; i <= columnCount; ++i)
{
String value = rs.getString(i);
if (rs.wasNull())
value = "<null>";
System.out.print(format(value, columnWidths[i - 1]));
System.out.print(" ");
}
System.out.println();
}
} catch (SQLException e)
{
System.out.println();
System.out.println("ERROR: " + e.getMessage());
}
}
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

Just one example of several I found when googling "resultset tablemodel":

http://www.oreillynet.com/pub/a/oreilly/java/news/javaex_1000.html
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Loading a JTable with SQL result set