Derby - LAST_INSERT_ID() 不适用于 JDBC
Derby - LAST_INSERT_ID() doesn't work with JDBC
我无法使用带有 spring 框架的 JdbcTemplate 获取最后插入 ID,在使用 last_insert_id() 函数之前,我已经搜索了很多其他方法,但无法使用它.
顺便说一下,我的 table 有
private static final String insertResponse =
"INSERT INTO response (" +
" idReceiver) " +
"VALUES (?)";
public static void saveResponse(Response response, User user) {
dataSource = getDataSource();
JdbcTemplate template = new JdbcTemplate(dataSource);
//"select id from users where username ='"+ user.getUsername +"'";
// define query arguments
Object[] params = new Object[] { 1 };
int[] responseTypes = new int[] { Types.INTEGER };
int row = template.update(insertResponse, params, responseTypes);
if (row >0){
SqlRowSet rowSet = template.queryForRowSet("SELECT LAST_INSERT_ID() AS id");
int lastInsertedID=0;
if (rowSet.next())
lastInsertedID = rowSet.findColumn("id");
System.out.println("last insert row is : "+lastInsertedID);
}
我的回复 table 是通过此命令创建的:
CREATE TABLE RESPONSE (
ID INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY
(START WITH 1, INCREMENT BY 1),
idReceiver INT NOT NULL
);
当我 运行 这段代码时,我的 JVM 说“嘿,伙计,你有这个例外:
org.springframework.jdbc.BadSqlGrammarException:语句回调;错误 SQL 语法 [SELECT LAST_INSERT_ID() AS id];嵌套异常是 java.sql.SQLSyntaxErrorException:语法错误:在第 1 行第 29 列遇到“”。
如果有人遇到我同样的问题,我已经解决了,按照这个answer..
KeyHolder keyHolder = new GeneratedKeyHolder();
template.update(
new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(java.sql.Connection connection) throws SQLException {
PreparedStatement ps =
connection.prepareStatement(insertResponse, new String[] {"id"});
ps.setInt(1, 1);
return ps;
}
},
keyHolder);
System.out.println("last inserted id is "+keyHolder.getKey());
我无法使用带有 spring 框架的 JdbcTemplate 获取最后插入 ID,在使用 last_insert_id() 函数之前,我已经搜索了很多其他方法,但无法使用它. 顺便说一下,我的 table 有
private static final String insertResponse =
"INSERT INTO response (" +
" idReceiver) " +
"VALUES (?)";
public static void saveResponse(Response response, User user) {
dataSource = getDataSource();
JdbcTemplate template = new JdbcTemplate(dataSource);
//"select id from users where username ='"+ user.getUsername +"'";
// define query arguments
Object[] params = new Object[] { 1 };
int[] responseTypes = new int[] { Types.INTEGER };
int row = template.update(insertResponse, params, responseTypes);
if (row >0){
SqlRowSet rowSet = template.queryForRowSet("SELECT LAST_INSERT_ID() AS id");
int lastInsertedID=0;
if (rowSet.next())
lastInsertedID = rowSet.findColumn("id");
System.out.println("last insert row is : "+lastInsertedID);
}
我的回复 table 是通过此命令创建的:
CREATE TABLE RESPONSE (
ID INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY
(START WITH 1, INCREMENT BY 1),
idReceiver INT NOT NULL
);
当我 运行 这段代码时,我的 JVM 说“嘿,伙计,你有这个例外: org.springframework.jdbc.BadSqlGrammarException:语句回调;错误 SQL 语法 [SELECT LAST_INSERT_ID() AS id];嵌套异常是 java.sql.SQLSyntaxErrorException:语法错误:在第 1 行第 29 列遇到“”。
如果有人遇到我同样的问题,我已经解决了,按照这个answer..
KeyHolder keyHolder = new GeneratedKeyHolder();
template.update(
new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(java.sql.Connection connection) throws SQLException {
PreparedStatement ps =
connection.prepareStatement(insertResponse, new String[] {"id"});
ps.setInt(1, 1);
return ps;
}
},
keyHolder);
System.out.println("last inserted id is "+keyHolder.getKey());