Arrays, Tables, and Databases in NetBeans

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.


Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

NelliosConnect With a Mentor Commented:
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
Forced accept.

EE Admin
All Courses

From novice to tech pro — start learning today.