根据另一个 JCombobox 填充 JCombobox 值

Populate JCombobox values depending on another JCombobox

我有两个 JComboboxes A 和 B,所以如果我 select 任何项目形成 A 那么与项目相关的值 selected 在 A 中应该填写 JCombobox B。我试过这个但是得到一个错误:

java.lang.ArrayIndexOutOfBoundsException: 0

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();
}

你做错了,

不需要使用'?' , 它应该只是 Client_Name=?

当您使用 setString()

时,PresparedStatement 负责引用参数

尝试

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

我想当你使用 '?' 时,准备好的语句不会将其视为参数,这就是你得到 IndexOutOfBounce

的原因