将“无效”值“住宅”转换为数据类型int时转换失败

时间:2018-11-10 01:10:22

标签: java sql scenebuilder

如何转换从结果集字符串给出的数据。 我试图将下拉框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() +"')";

1 个答案:

答案 0 :(得分:0)

首先创建一个用于在下拉列表(combobox)中存储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 +");" ;