Converting query into a jTable

Fahn7
Fahn7 used Ask the Experts™
on
Does anyone have an example which explain how I can put the result set into a JTable during the programm running.

I have the resultSet and now I want to put it into the JTable. I think the creation of the table is the problem.
It is also in the form of a ResultSetMetaData hoe do I convert that to a JTable.
Thank You
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mick BarryJava Developer
Top Expert 2010

Commented:
can u post what you've already got?

One approach would be to build a vector of vectors from your resultset and use it to construct your jtable.
Java Developer
Top Expert 2010
Commented:
something like this:

   public void executeQuery(String query) {
       if (connection == null || statement == null) {
           System.err.println("There is no database to execute the query.");
           return;
       }
       try {
           resultSet = statement.executeQuery(query);
           metaData = resultSet.getMetaData();

           int numberOfColumns =  metaData.getColumnCount();
           columnNames = new String[numberOfColumns];
           // Get the column names and cache them.
           // Then we can close the connection.
           for(int column = 0; column < numberOfColumns; column++) {
               columnNames[column] = metaData.getColumnLabel(column+1);
           }

           // Get all rows.
           Vector rows = new Vector();
           while (resultSet.next()) {
               Vector newRow = new Vector();
               for (int i = 1; i <= getColumnCount(); i++) {
                 newRow.addElement(resultSet.getObject(i));
               }
               rows.addElement(newRow);
           }
       }
       catch (SQLException ex) {
           System.err.println(ex);
       }
   }
Mick BarryJava Developer
Top Expert 2010

Commented:
did that code answer your question?
Fahn7:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for objects

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial