尽管插入了行,但 GetgeneratedKeys 不起作用
GetgeneratedKeys not working although row inserted
我使用 HSQLDB - 尝试过版本 2.3.4 和 2.4。
我有这个 Java 代码:
String sqlquery = "MERGE INTO bewertung pu USING "
.concat("(VALUES ?,?,?) ")
.concat("temp (tid,jurorid,runde) ")
.concat("ON temp.tid = pu.tid and temp.jurorid=pu.jurorid and temp.runde=pu.runde ")
.concat("WHEN NOT MATCHED THEN ")
.concat("INSERT (tid,jurorid,runde) ")
.concat("VALUES (temp.tid,temp.jurorid,temp.runde)");
PreparedStatement preparedStatement = sql.getConnection().prepareStatement(sqlquery,
Statement.RETURN_GENERATED_KEYS);
preparedStatement.setInt(1, eineBewertung.getTanzID()); //TID
preparedStatement.setInt(2, eineBewertung.getJurorID()); //JURORID
preparedStatement.setInt(3, eineBewertung.getRunde()); //RUNDE
int rowsAffected = preparedStatement.executeUpdate();
if (rowsAffected == 0) {
//UPDATE
//DO SOMETHING
}else{
//INSERT
try (ResultSet rs = preparedStatement.getGeneratedKeys()) {
if (rs.next()) {
eineBewertung.setBewertungsid(rs.getInt(1));
}
}catch (SQLException ex) {
this.controller.error_ausgeben(ex);
}
}
有效。如果我插入一个新行,我得到 rowsAffected
= 1。我检查数据库并且插入有效。
但是,我没有在结果集中得到任何结果 getGeneratedKeys()
每次都是空的。
我发现了一些用主键替换 Statement.RETURN_GENERATED_KEYS
的技巧。但这对我不起作用。
PreparedStatement preparedStatement = sql.getConnection().prepareStatement(sqlquery,
new String[]{"BEWERTUNGSID"});
这就是我创建 table:
的方式
statement.execute("create table PUBLIC.BEWERTUNG"
.concat("(BEWERTUNGSID INTEGER IDENTITY,")
.concat("TID INTEGER FOREIGN KEY REFERENCES Tanz(Tanzid),")
.concat("JURORID INTEGER not null FOREIGN KEY References JUROR(JURORID),")
.concat("RUNDE INTEGER not null,")
.concat("primary key(BEWERTUNGSID)")
.concat(")"));
为什么我没有取回任何生成的密钥?谢谢
//编辑
如果我用插入语句替换我的 sqlquery,它正在工作。
sqlquery = "INSERT INTO BEWERTUNG(TID, JURORID, RUNDE) VALUES(22, 2, 2)";
为什么 merge
在 sqlquery 中不起作用?
在 HSQLDB 2.4.0 之前的版本中,生成的键在使用 MERGE 语句插入数据时不可用。代码已承诺在下一个版本中允许这样做。
我使用 HSQLDB - 尝试过版本 2.3.4 和 2.4。
我有这个 Java 代码:
String sqlquery = "MERGE INTO bewertung pu USING "
.concat("(VALUES ?,?,?) ")
.concat("temp (tid,jurorid,runde) ")
.concat("ON temp.tid = pu.tid and temp.jurorid=pu.jurorid and temp.runde=pu.runde ")
.concat("WHEN NOT MATCHED THEN ")
.concat("INSERT (tid,jurorid,runde) ")
.concat("VALUES (temp.tid,temp.jurorid,temp.runde)");
PreparedStatement preparedStatement = sql.getConnection().prepareStatement(sqlquery,
Statement.RETURN_GENERATED_KEYS);
preparedStatement.setInt(1, eineBewertung.getTanzID()); //TID
preparedStatement.setInt(2, eineBewertung.getJurorID()); //JURORID
preparedStatement.setInt(3, eineBewertung.getRunde()); //RUNDE
int rowsAffected = preparedStatement.executeUpdate();
if (rowsAffected == 0) {
//UPDATE
//DO SOMETHING
}else{
//INSERT
try (ResultSet rs = preparedStatement.getGeneratedKeys()) {
if (rs.next()) {
eineBewertung.setBewertungsid(rs.getInt(1));
}
}catch (SQLException ex) {
this.controller.error_ausgeben(ex);
}
}
有效。如果我插入一个新行,我得到 rowsAffected
= 1。我检查数据库并且插入有效。
但是,我没有在结果集中得到任何结果 getGeneratedKeys()
每次都是空的。
我发现了一些用主键替换 Statement.RETURN_GENERATED_KEYS
的技巧。但这对我不起作用。
PreparedStatement preparedStatement = sql.getConnection().prepareStatement(sqlquery,
new String[]{"BEWERTUNGSID"});
这就是我创建 table:
的方式 statement.execute("create table PUBLIC.BEWERTUNG"
.concat("(BEWERTUNGSID INTEGER IDENTITY,")
.concat("TID INTEGER FOREIGN KEY REFERENCES Tanz(Tanzid),")
.concat("JURORID INTEGER not null FOREIGN KEY References JUROR(JURORID),")
.concat("RUNDE INTEGER not null,")
.concat("primary key(BEWERTUNGSID)")
.concat(")"));
为什么我没有取回任何生成的密钥?谢谢
//编辑 如果我用插入语句替换我的 sqlquery,它正在工作。
sqlquery = "INSERT INTO BEWERTUNG(TID, JURORID, RUNDE) VALUES(22, 2, 2)";
为什么 merge
在 sqlquery 中不起作用?
在 HSQLDB 2.4.0 之前的版本中,生成的键在使用 MERGE 语句插入数据时不可用。代码已承诺在下一个版本中允许这样做。