Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

searching in a jTable

Options
  • 26-03-2002 4:33pm
    #1
    Closed Accounts Posts: 4


    hi
    Can anyone help me please,

    I need to know how to click on a JTable to do a search from the value that is in the clicked column\row. The search is performed from data in a database. The results are sent back on a JTable. The search will involve two values a date which is the value from the JTable that has been clicked, I want to get the available rooms from the database for that date.

    thanks kathy

    seriously stressed


Comments

  • Registered Users Posts: 6,661 ✭✭✭Blitzkrieger


    JTable *shudder*
    ;)

    It's one of those classes where you have no real clue what's actually happening, and have to dig through mountains of code to find out.

    Could you give more information please? It's not real clear what you want.

    It sounds like you should check out the JDBC tutorials over at http://java.sun.com/docs/books/tutorial/

    Your query should be something like :

    SELECT rooms FROM table WHERE date = yourdate

    where yourdate is supplied by the JTable.

    Then execute the stamement (tutorial will show you how)

    ResultSet rs = stmt.executeQuery(String query);

    and you can loop through the results like this :

    while(rs.next())
    {
    room = rs.getString("rooms");
    //output the room somehow
    }


  • Registered Users Posts: 1,994 ✭✭✭lynchie


    You may also want to add a MouseListener on the JTable so that you can listen for when the user selects a cell. Once they have selected the cell the mousePressed() method will be called. In here, you can get the selected Row and Column that was cliecked.

    i.e.

    public void mousePressed(MouseEvent me)
    {
    Object source = me.getSource();
    if(source instanceof JTable)
    {
    JTable table = (JTable) source;
    String value = table.getValueAt(table.getSelectedRow(),table.getSelectedColumn());
    //Now do DB stuff with your value.
    }
    }


Advertisement