gpt4 book ai didi

java - 从 JComboBox 插入外键

转载 作者:行者123 更新时间:2023-12-01 11:14:46 24 4
gpt4 key购买 nike

我想从java组合框选定的项目中保存 key ,我做了这个函数,这样我就可以返回表“国籍”的主键并将其作为外键插入其他表中,但它不起作用,它总是返回0 :

public int getNat() throws Exception{
String query = "SELECT code_nat FROM nationality WHERE nationality=?";
try ( PreparedStatement stat = cnx.prepareStatement(query) ) {

stat.setString(1, (String)cmbNat.getSelectedItem());
rslt = stat.executeQuery();
return rslt.getInt(1);

}catch(SQLException ex){
ex.printStackTrace();
return 0;
}
}

最佳答案

您无法选择表格的所有字段:

String query = "SELECT * FROM nationality WHERE nationality=?";
// ↑ here

使用字段名称:

String query = "SELECT nationality_id FROM nationality WHERE nationality=?";

也可发送至retrieve data check here但这必须有效

return rs.getInt("nationality_id");

关于java - 从 JComboBox 插入外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31969656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com