Search code examples
jdbcparametersiteratorjava-8operator-precedence

Order of evaluation when using an increment operator to set the value of statement parameters


Is this the proper syntax in order to set the parameters of a previously generated preparedstatement? Which occurs first, the addition of 1 to the variable i, or the usage of the variable in setting the parameter?

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());
}

If the order of operations is that the value is incremented first, I would assume I can just add 1 AFTER setting the parameter. I'm merely asking for the sake of brevity in my code.


Solution

  • [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.

    Nonsense. All you would need to do is ...

    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);
        }
    }
    

    ... which produces ...

    i is 1
    i++ returned 1
    i is now 2