Solved

JDBC ResultSet - JTable

Posted on 2004-08-19
9
1,381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Title # Comments Views Activity
numbers ascending pyramid 101 265
Unhandled exception type Exception 18 65
add projects t working set in maven 2 71
Selenium WebElement Login button findElement 9 22
In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 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…
Suggested Courses

739 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