简体   繁体   中英

Populate JCombobox values depending on another JCombobox

I have two JComboboxes A and B, so if I select any item form A then values related to the item selected in A should fill in JCombobox B. I tried this but get an error:

java.lang.ArrayIndexOutOfBoundsException: 0

at pst.setString(1, client.getSelectedItem().toString());

try
{
    String query="select `User_Name` from Client where `Client_Name`='?' ";
    PreparedStatement pst=conn.prepareStatement(query);
    pst.setString(1, client.getSelectedItem().toString());

    ResultSet rs=pst.executeQuery();

    user.addItem("--Select--");
    while(rs.next())
    {
        user.addItem(rs.getString("User_Name"));            
    }
//      return;
    System.out.println(query);

}
catch(Exception g)
{
    g.printStackTrace();
}

you have done a mistake ,

no need to use '?' , it should be just Client_Name=?

The PresparedStatement takes care of quoting the parameter, when you use setString()

Try

String query="select User_Name from Client where Client_Name = ?";
PreparedStatement pst=conn.prepareStatement(query); 
pst.setString(1, String.valueOf(client.getSelectedItem()));

I guess that when you use '?' the prepared statement does not count this as parameter and this is why you get the IndexOutOfBounce

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM