Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Displaying a database table using Swing  RSS feed

 
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();
 
Prerna Bansal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are using the following constructor for creating a JTable.
JTable(Vector rowData, Vector columnNames)
The rowData shuld be a Vector of Vectors, whereas in your code rowData is a Vector of Strings.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!