Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying a database table using Swing

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need to display a database table using Swing.
I am using the following code but it doesn't display anything:
Vector currentRow = new Vector();
Vector columnHeads = new Vector();
String query = "SELECT username, password FROM Admin";
statement = connection.createStatement();
resultSet = statement.executeQuery( query );
while(resultSet.next()) {
String a = resultSet.getString(1);
String b = resultSet.getString(2);
ResultSetMetaData rsmd = resultSet.getMetaData();

for ( int i = 1; i <= rsmd.getColumnCount(); i++ ) {
columnHeads.addElement( rsmd.getColumnName( i ) );
currentRow.addElement( resultSet.getString( i ) );
}
table = new JTable( currentRow, columnHeads);
JScrollPane scroller = new JScrollPane( table );
getContentPane().add(scroller, BorderLayout.CENTER );

validate();
show();
 
Michael Fitzmaurice
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I think you are trying to do too much inside the one while loop. Have a look at the following code excerpt, which comes directly from "Java How To Program (Third Edition)" by Deitel & Deitel:
<code>
private void displayResultSet( ResultSet rs )
throws SQLException
{
// position to first record
boolean moreRecords = rs.next();
// If there are no records, display a message
if ( ! moreRecords ) {
JOptionPane.showMessageDialog( this,
"ResultSet contained no records" );
setTitle( "No records to display" );
return;
}
setTitle( "Authors table from Books" );
Vector columnHeads = new Vector();
Vector rows = new Vector();
try {
// get column heads
ResultSetMetaData rsmd = rs.getMetaData();

for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
columnHeads.addElement( rsmd.getColumnName( i ) );
// get row data
do {
rows.addElement( getNextRow( rs, rsmd ) );
} while ( rs.next() );
// display table with ResultSet contents
table = new JTable( rows, columnHeads );
JScrollPane scroller = new JScrollPane( table );
getContentPane().add(
scroller, BorderLayout.CENTER );
validate();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
private Vector getNextRow( ResultSet rs,
ResultSetMetaData rsmd )
throws SQLException
{
Vector currentRow = new Vector();

for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
switch( rsmd.getColumnType( i ) ) {
case Types.VARCHAR:
currentRow.addElement( rs.getString( i ) );
break;
case Types.INTEGER:
currentRow.addElement(
new Long( rs.getLong( i ) ) );
break;
default:
System.out.println( "Type was: " +
rsmd.getColumnTypeName( i ) );
}

return currentRow;
}
</code>
Hope this helps
Michael

------------------
"One good thing about music - when it hits, you feel no pain"
Bob Marley
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic