About Question enthuware.ocpjp.v11.2.3368 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
futurecap
Posts: 7
Joined: Wed Aug 12, 2020 4:44 am
Contact:

About Question enthuware.ocpjp.v11.2.3368 :

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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!
If you like our products and services, please help us by posting your review here.

maria_maria
Posts: 10
Joined: Wed Mar 03, 2021 5:39 am
Contact:

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

Post 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 ...

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

You are right. It should be VARCHAR. Fixed.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 41 guests