Page 1 of 1

About Question enthuware.ocpjp.v8.2.1353 :

Posted: Sun Jul 31, 2016 11:38 am
by badbishop
In addition, shouldn't the Connection part also be included in try-with-resources?

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

Posted: Sun Jul 31, 2016 8:13 pm
by admin
Not necessarily. There could be more code below the given try block that reuses the same Connection.

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

Posted: Mon Aug 01, 2016 12:11 pm
by badbishop
I mean, Connection should also ultimately stay inside try(-with-resources) block, shouldn't it?

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

Posted: Mon Aug 01, 2016 8:16 pm
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.

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

Posted: Wed Aug 03, 2016 5:47 am
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.

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

Posted: Wed Aug 03, 2016 11:31 am
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.

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

Posted: Tue Jul 25, 2017 12:08 pm
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?

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

Posted: Sat Jul 28, 2018 3:34 pm
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).

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

Posted: Sat Jul 28, 2018 11:29 pm
by admin
That's alright because it is an incorrect option anyway.

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

Posted: Fri Aug 03, 2018 7:30 am
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.

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

Posted: Thu Sep 26, 2019 12:52 am
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
()()()

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

Posted: Thu Sep 26, 2019 1:50 am
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?