Arrays, Tables, and Databases in NetBeans

Hi,
I'm working on a project in NetBeans 6.0 but am having a little difficulty.  If anyone is able to help, I'd really appreciate it.

What I'd like to do is display 3 fields in my first java JTable from one MySQL database table.  After that, I'd like the user to select a record from the first java JTable.  The record they choose has a unique ID number attached.  I would like to use that ID number to display data from a second MySQL database table that shares the ID field in a second java JTable.

So if I have in the first JTable:
Jim | Sales | 30
Oscar | Accounting | 40
Toby | HR | 60

I would want someone to select a record, say Jim, whose ID number might be '1'.

Then, in another java JTable, I would like to pull information from another database table about Jim, where there would be 3 or 4 rows with information about Jim, each row containing Jim's ID number: '1.'

2004 | Jim | 30000
2005 | Jim | 40000
2006 | Jim | 50000
2007 | Jim | 70000

I've been told that this can be accomplished using an array to capture the ID number, but I have no idea how to do it.

If anyone would be able to help me out a little, it would be much appreciated.

Thanks!

Mark
Titanfan21Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NelliosCommented:
You could add a ListSelectionListener on your table so that when someone selects a specific row you get that event.

In the valueChanged method of the listener you could

 public void valueChanged(ListSelectionEvent e) {
   //Fetch the Selected Row.
    int row = table.getSelectedRow();

   //Implement and call a method which fetches the unique ID from the selected Row.
    int uniqueID = getUniqueIDFromRow(row);
 
//Create the content of the new table (implement a method for that)
 Data[][] newTableData = createContentOfTheNewTableGivenTheID(uniqueID);

//  attach the Data to the new table Model:
 newTable.setModel(new javax.swing.table.DefaultTableModel(
                newTableData ,
                new String[]{
            "Filed1", "Filed2", "Field3", "Field4"
        }) {

            Class[] types = new Class[]{
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean[]{
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
  }

Thats a very simplified approach but it worked for me in the past

Good Luck
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.