Solved

JDBC ResultSet - JTable

Posted on 2004-08-19
9
1,379 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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
 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

837 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