About Question enthuware.ocpjp.v11.2.3368 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

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: 10384
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!

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: 10384
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!

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests