将 "invalid" 值 'Residential' 转换为数据类型 int 时转换失败

Conversion failed when converting the "invalid" value 'Residential' to data type int

如何转换从结果集字符串中给出的数据。 我试图将下拉框 gui 显示为文本,但给它一个值 1 和 2

我希望它显示的下拉选项是正确的: https://i.imgur.com/gDKAkDX.png

但我需要相应的实际值是 1 和 2

然后我需要将这些值放回只允许 int 的数据库 f 键中...

final ObservableList options = FXCollections.observableArrayList();


public void fillComboBox() {
    Connection c;


    try {
        Connection conn = null;
        Statement stmt = null;
        String queryx = "Select CusType_ID, CusTypeName from CustomerType";

        ResultSet jrs = c.createStatement().executeQuery(queryx);

        while (jrs.next()) {

            String custyID = jrs.getString("CusType_ID");
            String custyN = jrs.getString("CusTypeName");


            options.add(new MyObject(custyID,custyN));
            typeBox.setItems(options);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}



String sql = "INSERT INTO Customer [CustomerType_ID]) VALUES" typeBox.getValue() +"')";

首先创建一个 class 用于在下拉列表(组合框)中存储 CusType_ID 和 CusTypeName

public class MyObject {

private String key;
private String value;

public MyObject(String key, String value) {
    this.key = key;
    this.text = value;
}

public String getKey(){
return key ;
}

@Override
public String toString() {
    return value;
  }
}

然后存储您请求的值

     String queryx = "Select CusType_ID, CusTypeName from CustomerType";

    ResultSet rs = c.createStatement().executeQuery(queryx);

    while (rs.next()) {
        int recordNumber = 1;
        int recNumber =2;

        String sx = rs.getString("CusTypeName");
        String customerID = rs.getString("CusType_ID");


        typeBox.addItem(new MyObject(customerID, sx));


        }

最后,从所选项目中取回您的价值并将其存储在新的 sql

Object item = comboBox.getSelectedItem();
String value = ((MyObject)item).getKey();
String sql = "INSERT INTO Customer (CustomerType_ID) VALUES ("+ 
value +");" ;