使用递增运算符设置语句参数值时的求值顺序
Order of evaluation when using an increment operator to set the value of statement parameters
这是设置先前生成的准备语句参数的正确语法吗?哪个先出现,是给变量i加1,还是变量在设置参数时的用法?
int i=1;
for (TagInfo tag : scannedTags){
//Pull the tag from the dbo.Tags table that matches the SiteID and TagOffSet values of the current tag in the alarm_tags list
//Set parameters for the prepared statement
dbStmtTag.setInt(i++, tag.getSiteID());
dbStmtTag.setInt(i++, tag.getTagOffset());
}
如果操作顺序是值先递增,我假设我可以在设置参数后只加1。我只是为了代码简洁起见。
[To test the behaviour of i++
for myself] I'd have to write an entire test application that talks to a test database, which I would need to create.
废话。您需要做的就是...
public static void main(String[] args) {
try {
int i = 1;
System.out.printf("i is %d%n", i);
System.out.printf("i++ returned %d%n", i++);
System.out.printf("i is now %d%n", i);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
... 产生 ...
i is 1
i++ returned 1
i is now 2
这是设置先前生成的准备语句参数的正确语法吗?哪个先出现,是给变量i加1,还是变量在设置参数时的用法?
int i=1;
for (TagInfo tag : scannedTags){
//Pull the tag from the dbo.Tags table that matches the SiteID and TagOffSet values of the current tag in the alarm_tags list
//Set parameters for the prepared statement
dbStmtTag.setInt(i++, tag.getSiteID());
dbStmtTag.setInt(i++, tag.getTagOffset());
}
如果操作顺序是值先递增,我假设我可以在设置参数后只加1。我只是为了代码简洁起见。
[To test the behaviour of
i++
for myself] I'd have to write an entire test application that talks to a test database, which I would need to create.
废话。您需要做的就是...
public static void main(String[] args) {
try {
int i = 1;
System.out.printf("i is %d%n", i);
System.out.printf("i++ returned %d%n", i++);
System.out.printf("i is now %d%n", i);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
... 产生 ...
i is 1
i++ returned 1
i is now 2