How to access db2 sequence in Spring
How to access db2 se开发者_开发百科quence in Spring? Need to access a sequence to generate an id and pass it to an insert statement using spring using jdbcTemplate.
First, you have to define a bean that handles the sequence. Something like this:
<bean id="incrementer" class="org.springframework.jdbc.support.incrementer.DB2SequenceMaxValueIncrementer">
<property name="dataSource" ref="dataSource" />
<property name="incrementerName" value="YOUR_SEQUENCE_NAME" />
</bean>
Then, you have to wire this bean to the bean that's going to invoke it. e.g.:
@Autowired
private DataFieldMaxValueIncrementer incrementer;
Finally, you use the sequence bean to get a value:
Long identifier = incrementer.nextLongValue();
Have you tried queryForInt() with the SQL select sequence.nextval from dual
?
http://static.springsource.org/spring/docs/2.5.6/api/org/springframework/jdbc/core/simple/SimpleJdbcTemplate.html#queryForInt(java.lang.String, java.lang.Object...)
精彩评论