gpt4 book ai didi

sql - 为什么 getGeneratedKeys() 在 INSERT 之后不返回任何结果?

转载 作者:行者123 更新时间:2023-12-01 23:24:47 31 4
gpt4 key购买 nike

我有一个表,其中有一列定义为自动生成键。执行以下代码时,我希望生成一个新 key ,但事实并非如此。插入一行,但 ps.getGeneratedKeys() 的结果集为空。

我正在使用 DB2:SQLLIB_ADCL_V97FP1。

        conn = getConnection();
conn.setAutoCommit(false);
String query =
"INSERT INTO MyTable " +
" (messe_nr, land5, variable_nr, bezeichnung_en, bezeichnung_de) " +
"VALUES (?,?,?,?,?)";

PreparedStatement ps = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
ps.setInt(1, fair.getFairId());
ps.setString(2, variable.getCountryCode());
ps.setInt(3, variable.getSort());
ps.setString(4, variable.getLabel_en());
ps.setString(5, variable.getLabel_de());

log.debug(query);
int count = ps.executeUpdate();
if(count == 1) {
ResultSet rs = ps.getGeneratedKeys();
if(rs.next()) {
variable.setId(rs.getBigDecimal(1).intValue());
} else {
log.error("Could not get autogen key after variable insert");
throw new AVMSException(1500);
}
rs.close();
}
log.debug("inserted " + count);

ps.close();
conn.commit();

更新

我刚把项目部署到测试服务器上,没有出现这个问题。我的开发系统出了点问题——但是什么?我确实在一两天前安装了 Java 更新 - 1.7.0_13 - 这会导致这样的问题吗:-/?

最佳答案

试一试,它对我有用:

prepStmt = con.prepareStatement(sql, new String[]{"YOUR_ID_FIELD"});

//Setting Bind-Variables

prepStmt.executeUpdate();

rs = prepStmt.getGeneratedKeys();
while (rs.next())
{
tAdresse.getAdressid().setValue(rs.getInt(1));
System.out.println("Key: " + tAdresse.getAdressid().getIntValue());
}

关于sql - 为什么 getGeneratedKeys() 在 INSERT 之后不返回任何结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14748054/

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