简体   繁体   English

在Java预准备语句中使用DB2序列

[英]Using DB2 Sequence in Java Prepared Statement

I want to get the next value of a db2 sequence if the placeholder in the preparedstatement is 0 . 如果preparestatement中的占位符为0我想获取db2序列的下一个值。 How can i do that . 我怎样才能做到这一点 。

PreparedStmt.setLong(1, attributeID);

So here if attributeID is 0 , i want to get the next value from the sequence, other wise i use the attribute ID . 所以在这里,如果attributeID0 ,我想从序列中获取下一个值,否则我将使用attribute ID。

I tried using 我尝试使用

PreparedStmt.setObject(1, attributeID > 0 ? attributeID : next val for my seq);

but this does not work ! 但这行不通!

It's to easy, just get the sequence like you get a result set with executeQuery() method. 这很容易,只要像使用executeQuery()方法获得结果集那样获得序列即可。 Then the select statement is: 然后,select语句为:

SELECT SEQ_NAME.NEXTVAL FROM SYSIBM.SYSDUMMY1

The other part is an if sentence. 另一部分是if语句。 That's all you need to do. 这就是您需要做的。

You can declare the primary key column as 您可以将主键列声明为

ATTRIBUTEID BIGINT GENERATED BY DEFAULT AS IDENTITY

To insert a row with a specific value, include the primary key column in the sql and set its value with: 要插入具有特定值的行,请在sql中包括主键列,并使用以下命令设置其值:

PreparedStmt.setLong(1, attributeID);

To insert a row where database generates a value for key, omit the primary key column in the sql and do not set its value in the prepared statement. 要在数据库为键生成值的行中插入行,请在sql中省略主键列,并且不要在prepared语句中设置其值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM