Forums Register Login

Loading a JTable with SQL result set

+Pie Number of slices to send: Send
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());
}
}
+Pie Number of slices to send: Send
Just one example of several I found when googling "resultset tablemodel":

http://www.oreillynet.com/pub/a/oreilly/java/news/javaex_1000.html
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 13611 times.
Similar Threads
regarding JTable
JTable
Guys and Girls use this cool program for output
Populating hashmap does not work
Slight JTable problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:20:44.