About Question enthuware.ocpjp.v8.2.1353 :
Moderator: admin
-
- Posts: 27
- Joined: Fri Jul 22, 2016 9:14 am
- Contact:
About Question enthuware.ocpjp.v8.2.1353 :
In addition, shouldn't the Connection part also be included in try-with-resources?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1353 :
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.
-
- Posts: 27
- Joined: Fri Jul 22, 2016 9:14 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1353 :
I mean, Connection should also ultimately stay inside try(-with-resources) block, shouldn't it?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1353 :
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.
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.
-
- Posts: 27
- Joined: Fri Jul 22, 2016 9:14 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1353 :
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");
}
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1353 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Tue Jul 25, 2017 12:05 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1353 :
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?
-
- Posts: 29
- Joined: Sun Jul 16, 2017 4:24 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1353 :
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).
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).
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1353 :
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.
-
- Posts: 125
- Joined: Thu Jul 05, 2018 6:44 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1353 :
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.philippe wrote: ↑Sat Jul 28, 2018 3:34 pmHi 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).
-
- Posts: 78
- Joined: Sun Jun 30, 2013 10:04 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1353 :
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
()()()
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
()()()
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1353 :
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.
Who is online
Users browsing this forum: Bing [Bot] and 57 guests