Solved

JDBC ResultSet - JTable

Posted on 2004-08-19
9
1,374 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
changePi Challenge 15 76
pairs challenge 5 44
Eclipse Neon and jdk 1.8.0 11 69
Java Timer (static) 9 16
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

757 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

26 Experts available now in Live!

Get 1:1 Help Now