Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

searching in a jTable

  • 26-03-2002 04: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, Registered Users 2 Posts: 6,676 ✭✭✭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, Registered Users 2, Paid Member Posts: 2,032 ✭✭✭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