IntelliJ Java IDE
The moose likes Swing / AWT / SWT / JFace and the fly likes Displaying a database table using Swing Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT / JFace
Reply Bookmark "Displaying a database table using Swing" Watch "Displaying a database table using Swing" New topic
Author

Displaying a database table using Swing

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: May 01, 2001
Posts: 8
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.
 
 
subject: Displaying a database table using Swing
 
Threads others viewed
Vectors - please explain
JTable type yes/no
JTable & JDBC
searching database
Displaying a database table using Swing
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture