• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Loading a JTable with SQL result set

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());
}
}
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just one example of several I found when googling "resultset tablemodel":

http://www.oreillynet.com/pub/a/oreilly/java/news/javaex_1000.html
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!