0

How do you go about putting records into a jTextField when an item is selected from a jComboBox? For example, I'm making an airline reservation system and I have a combo box with the available flights. Below it are text fields with designated info like departure date, departure time, arrival date, etc. How do I make it so that when the user selects an item from the the combo box, (ex. flight name is CX9005) the corresponding info from the same row is shown in the text fields? (ex. departure date is November 12 2015)

EDIT:

So I tried doing that with the ff. code, but I got a syntax error and a ResultSet not open error.

private void combo_FlightItemStateChanged(java.awt.event.ItemEvent evt) {                                              

    try{
    flightID = combo_Flight.getSelectedItem().toString();
    String flightName = combo_Flight.getSelectedItem().toString();  
    String query = "Select * from ACCOUNTS where flightName = \'"+flightName+"\';";

    rs = stmt.executeQuery(query);
    }
    catch(SQLException err){
        JOptionPane.showMessageDialog(UserModule.this, err.getMessage());
    }
}                  

Also, I use this function to connect to my database if that matters.

public void DoConnect() {
    try{
        String host = "jdbc:derby://localhost:1527/UserAccounts";
        String uName = "Bryan";
        String uPass = "Cruz";
        con = DriverManager.getConnection(host, uName, uPass);

        stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
        ResultSet.CONCUR_UPDATABLE);
        String sql = "SELECT * FROM ACCOUNTS";
        rs = stmt.executeQuery(sql);
    }catch(SQLException err){
        JOptionPane.showMessageDialog(Connect.this, err.getMessage());
    }
}

Also, I might not have been too clear with my original post. I want to make it so that when the user selects a flight from the combo box, all of that flight's details show up in the appropriate text fields. (ex. departure date, departure time, destination, etc.) I'm confused on how to make this work so help would be much appreciated!

2 Answers 2

0

You get the text from the comboBox using getSelectedItem() or getSelectedValue().

You can then create a MySQL query like:

      String flightName = yourComboBox.getSelectedItem().toString();  
      String query= "Select * from yourTable where flightName = \'"+flightName+"\';"; 

Then you can execute this query using executeQuery()

Now for the part with setting results in all testfields

First store the result in a ResultSet object

      rs = stmt.executeQuery(query);
      rs.next();

Now you have the result in object rs

Use rs.getString(columnNumber) to get records from the result

Eg.

String departureDate = rs.getString(4);
// assuming the column number is 4

Do this to get all required strings. And set them in respected columns using setText() method

2
  • Hello, thank you for taking the time to answer. I added more details to the op.
    – Bryan Cruz
    Sep 27, 2015 at 10:43
  • There see the added part. That must solve your problem. I think you missed the rs.next(); statement
    – CoderBrain
    Sep 27, 2015 at 13:10
0
flightID = combo_Flight.getSelectedItem().toString();
String flightName = combo_Flight.getSelectedItem().toString();  

What is the point of the above two statements? You can't get two values from the same combo box. Either you get the flightID or the flighName.

String query = "Select * from ACCOUNTS where flightName = \'"+flightName+"\';";

but I got a syntax error

Don't try to build the SQL string manually. Instead you can use a PreparedStatement. It will make sure the proper delimiters are used so you just concentrate on the SQL statement:

String flightName = combo_Flight.getSelectedItem().toString();  

String query = "Select * from ACCOUNTS where flightName = ?";
PreparedStatement stmt = connection.prepareStatement(query);
stmt.setString( 1, flightName );

ResultSet rs = stmt.executeQuery();

So you will need access to the Connection object you established in the doConnect() method. Note, method names should NOT start with an upper case character.

Also, I believe the standard for database column names is to capitalize the words of the column, so you should be using "FlightName" not "flightName" as the column name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.