简体   繁体   English

在 spring 中使用 jdbctemplate 触发 Sequence.nextval 查询

[英]Fire Sequence.nextval query using jdbctemplate in spring

Database : Oracle数据库: Oracle

I have table in which there are 10 columns and i want sequence next value when insert row and also use that sequence number which inserted.我有一个表,其中有 10 列,我想在插入行时对下一个值进行排序,并使用插入的序列号。

Now i have searched and find that KeyHolder of spring is useful but restrict for only less than 8 field so i can't use that.现在我搜索并发现 spring 的 KeyHolder 很有用,但仅限于少于 8 个字段,所以我不能使用它。

How can i fire "select MySequence.nextval from dual" query and get sequence using jdbctemplate(NamedParameterJDBCTemplate) ?如何使用jdbctemplate(NamedParameterJDBCTemplate)触发"select MySequence.nextval from dual"查询并获取序列?

Is other way to achieve for get inserted sequence value ?.是否有其他方法可以实现获取插入的序列值?。

Using a jdbctemplate you can just mention the sequence generator as a value, eg使用jdbctemplate你可以只提到序列生成器作为一个值,例如

jdbcTemplate.update("INSERT INTO TABLE (id, data) VALUES (MySequence.nextval, ?)", new Object[] { data });

A note regarding the sequence generation: for versions before Oracle 12c you should have a trigger which will increment the sequence for you.关于序列生成的注意事项:对于 Oracle 12c 之前的版本,您应该有一个触发器,它将为您增加序列。 From 12c you can use the auto-increment feature.从 12c 开始,您可以使用自动增量功能。

You can achieve this by using JdbcTemplate like this :您可以像这样使用 JdbcTemplate 来实现这一点:

final SqlRowSet sqlRowSet = jdbcTemplate.queryForRowSet(NEXT_VALUE_QUERY);
  sqlRowSet.next();// mandatory to move the cursor 
  sqlRowSet.getLong(1);// the value of the nextval

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查询以使用 Spring JPA 从序列中获取 nextval - Query to get nextval from sequence with Spring JPA 如何从JDBC中的SQL获取sequence.nextval? - How to get sequence.nextval from SQL in JDBC? tomcat环境中的H2数据库在select sequence.nextval上抛出RuntimeException“意外代码路径” - H2 database in tomcat environment throwing RuntimeException “unexpected code path” on select sequence.nextval 在Oracle 12c中使用rs.getInt()或getLong()的Sequence.NEXTVAL失败-那么它返回什么数据类型? - Sequence.NEXTVAL in Oracle 12c with rs.getInt() or getLong() fails - so what datatype it returns? 使用Spring JDBCTemplate用单引号进行SQL查询 - SQL query with single quotes using spring jdbctemplate mariadb 序列 nextval 使用 hibernate 递增 2 - mariadb sequence nextval is incrementing by 2 using hibernate 使用Spring JdbcTemplate - using Spring JdbcTemplate 在Spring jdbc中使用jdbctemplate在Select查询上返回布尔值 - Return boolean on Select query using jdbctemplate in Spring jdbc 使用spring“ jdbcTemplate.batchUpdate”进行动态插入查询的批处理 - batch processing with dynamic insert query using spring “jdbcTemplate.batchUpdate” 如何使用spring的jdbcTemplate在SQL查询中指定参数 - How to specify parameters in an SQL query using spring's jdbcTemplate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM