Page 1 of 1

About Question enthuware.ocpjp.v11.2.3368 :

Posted: Thu Jan 07, 2021 8:21 am
by futurecap
I am wondering, if the explanation is correct: "...indexing of database columns starts with 1..."

http://tutorials.jenkov.com/jdbc/prepar ... %20updates.

I read in the link above that you replace "?" of a PreparedStatement with the values from setXXX(). And there counting starts with 1.

So the other values should be incremented as well, right?

Code: Select all

ps.setObject(2, "Ally A", JDBCType.VARCHAR); //2
ps.setObject(3, "101 main str"); //3
ps.executeUpdate(); //4
ps.setObject(2, "Bob B"); //5

Re: About Question enthuware.ocpjp.v11.2.3368 :

Posted: Thu Jan 07, 2021 8:28 am
by admin
Yes, numbering of both query params and the database columns in a ResultSet, starts with 1. So, others indexes will also change to 2 and 3 (instead 1 and 2). The explanation implies that other than the incorrect indexing issue there is no other issue with the given code. It has now been made clear.
thank you for your feedback!

Re: About Question enthuware.ocpjp.v11.2.3368 :

Posted: Tue Apr 13, 2021 11:27 am
by maria_maria

Code: Select all

String qr = "insert into USERINFO values( ?, ?, ?)";
        try (PreparedStatement ps = c.prepareStatement(qr);) {
            ps.setObject(0, 1); //1   
            ps.setObject(1, "Ally A", JDBCType.VARCHAR); //2  
            ps.setObject(2, "101 main str"); //3   
            ps.executeUpdate(); //4   
            ps.setObject(1, "Bob B"); //5   
            ps.setNull(2, java.sql.Types.String); //5  
            ps.executeUpdate(); //6 
        }
I agree that parameter index should start at 1 otherwise there will be a runtime error but what about the second line 5? My compiler complains about java.sql.Types.String ...

Re: About Question enthuware.ocpjp.v11.2.3368 :

Posted: Tue Apr 13, 2021 7:39 pm
by admin
You are right. It should be VARCHAR. Fixed.
thank you for your feedback!