Solved

JDBC ResultSet - JTable

Posted on 2004-08-19
9
1,377 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Arrays.asList  VS  ArrayList 4 54
JavaFX TableView not displaying correctly 3 16
Chrome and Firefox Java 5 27
Problem to Alipay 10 18
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 …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

786 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