About Question enthuware.ocpjp.v8.2.1353 :

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

Moderator: admin

Post Reply
badbishop
Posts: 27
Joined: Fri Jul 22, 2016 9:14 am
Contact:

About Question enthuware.ocpjp.v8.2.1353 :

Post by badbishop »

In addition, shouldn't the Connection part also be included in try-with-resources?

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

Re: About Question enthuware.ocpjp.v8.2.1353 :

Post by admin »

Not necessarily. There could be more code below the given try block that reuses the same Connection.
If you like our products and services, please help us by posting your review here.

badbishop
Posts: 27
Joined: Fri Jul 22, 2016 9:14 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1353 :

Post by badbishop »

I mean, Connection should also ultimately stay inside try(-with-resources) block, shouldn't it?

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

Re: About Question enthuware.ocpjp.v8.2.1353 :

Post by admin »

May be I am not understanding you correctly. Can you show what you mean by putting it in code? And why do you think so?
FYI, it is possible to let Connection not be closed at all.
If you like our products and services, please help us by posting your review here.

badbishop
Posts: 27
Joined: Fri Jul 22, 2016 9:14 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1353 :

Post by badbishop »

Code: Select all

private ConnectIt() throws SQLException{
        connectionProps.put("user", "jstudent");
        connectionProps.put("password", "1234");
        conn = DriverManager.getConnection(
                    "jdbc:mysql://mysql.test.lan:3306/jtuto?user=jstudent&password=1234");
    }
It won't compile with throws clause removed - just checked with NetBeans.

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

Re: About Question enthuware.ocpjp.v8.2.1353 :

Post by admin »

Yes, but why does that matter? The question doesn't show the complete method. It shows just a snippet of code. You need to assume that if the validity of given snippet depends on something that is not specified, then that "unspecified" thing exists. In this case, it is either another wrapper try/catch block or the throws clause.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

alexm1980
Posts: 4
Joined: Tue Jul 25, 2017 12:05 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1353 :

Post by alexm1980 »

The numbering of columns in a ResultSet does not start with 1 and not with 0? Therefore, rs.getString(0) will not cause an SQLException every time the codes run?

philippe
Posts: 29
Joined: Sun Jul 16, 2017 4:24 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1353 :

Post by philippe »

Hi alexm1980,
Indeed, column numbering in a ResultSet start at 1. The explanation of the answer "It will throw an exception if the first column of the result is not a String." is correct, but of topic. It would be on topic if rs.getString(0) is replaced by rs.getString(1).

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

Re: About Question enthuware.ocpjp.v8.2.1353 :

Post by admin »

That's alright because it is an incorrect option anyway.
If you like our products and services, please help us by posting your review here.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1353 :

Post by __JJ__ »

philippe wrote:
Sat Jul 28, 2018 3:34 pm
Hi alexm1980,
Indeed, column numbering in a ResultSet start at 1. The explanation of the answer "It will throw an exception if the first column of the result is not a String." is correct, but of topic. It would be on topic if rs.getString(0) is replaced by rs.getString(1).
It's not off-topic at all. It's there as a red herring (as we say in idiomatic English). IOW it's there to fool people into choosing the wrong answer. I was fooled.

ewebxml
Posts: 77
Joined: Sun Jun 30, 2013 10:04 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1353 :

Post by ewebxml »

Isn't it true that the JDBC Documentation says that the method
rs.getString(1)
must start with 1 ?

If you attempt to access the column at 0 it will throw an Exception.
System.out.println(rs.getString(0));

Please confirm.

Thanks
()()()

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

Re: About Question enthuware.ocpjp.v8.2.1353 :

Post by admin »

Yes, that is true. But that is not the issue with this question. Before it can throw an exception, the code has to compile, right?
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests