Solved

JDBC ResultSet - JTable

Posted on 2004-08-19
9
1,376 Views
Last Modified: 2012-08-14
1) I would like to be able to display the resultset from a given query in a JTable.
2) I need to have the ability to reflect any changes made in the data back to the database.


Database Server:  MS SQL 2000
Database Driver: Microsoft SQL JDBC Driver
JDK 1.4

Thanks,
David
0
Comment
Question by:lomidien
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11844386
0
 

Author Comment

by:lomidien
ID: 11845166
Pretty solid links, but one more question.

I've read and I believe I understand the extension of AbstractTableModel, but I'm a little unclear as to how I will be able to syncronize any changes made in the JTable back to the database.  What if my JTable is comprised of data built from several sql tables?

Thanks,
Dave
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11846463
There's no reason, on the face of it, to have to extend anything. Just *use* a TableModel. A DefaultTableModel will give you good functionality. The following will give you a (Default)TableModel from a ResultSet:

      public TableModel resultSetToTableModel(ResultSet rs) {
            try {
                  // The below just for searches
                  ResultSetMetaData metaData = rs.getMetaData();
                  int numberOfColumns = metaData.getColumnCount();
                  Vector columnNames = new Vector();
                  // Get the column names
                  for (int column = 0; column < numberOfColumns; column++) {
                        columnNames.addElement(metaData.getColumnLabel(column + 1));
                  }
                  // Get all rows.
                  Vector rows = new Vector();
                  while (rs.next()) {
                        Vector newRow = new Vector();
                        for (int i = 1; i <= numberOfColumns; i++) {
                              newRow.addElement(rs.getObject(i));
                        }
                        rows.addElement(newRow);
                  }
                  return new DefaultTableModel(rows, columnNames);
            }
            catch (Exception e) {
                  e.printStackTrace();
                  return null;
            }
      }


>>What if my JTable is comprised of data built from several sql tables?

There's no universal, one-stop way of persisting changes to the table afaik. This must be tailored for you particular requirement


0
 

Author Comment

by:lomidien
ID: 11847555
It works at displaying the data just fine, but how do I "fire" it to make updates in the database if a user makes a change to a field in the table?

Thanks,
David
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 11848593
> but how do I "fire" it to make updates in the database if a user makes a change to a field in the table?

You need to either implement that in your model's setValueAt() method, or using a TableModelListener.
0
 
LVL 35

Accepted Solution

by:
girionis earned 500 total points
ID: 11849724
> but how do I "fire" it to make updates in the database if a user makes a change to a field in the table?

I think the link I posted explains that, does it not?
0
 

Author Comment

by:lomidien
ID: 11852162
You're right, it did.

Thanks,
David
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11852197
Don't mention it ;-)
0
 
LVL 35

Expert Comment

by:girionis
ID: 11852320
:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now