Page 1 of 1
About Question enthuware.ocpjp.v7.2.1353 :
Posted: Sun Mar 30, 2014 3:49 pm
by tn1408
Hello,
#4 is right, it will not compile if Statement is declared outside.
My question is Connection c is created outside. How come that is allowed?
Thanks,
Tony,
Re: About Question enthuware.ocpjp.v7.2.1353 :
Posted: Sun Mar 30, 2014 6:37 pm
by admin
Because c is not being managed by try-with-resources.
Re: About Question enthuware.ocpjp.v7.2.1353 :
Posted: Sun Nov 29, 2015 8:10 pm
by vcheewei
Quick question, do you think rs.getString(0) will execute without exception since column index starts with 1 and not 0?
Re: About Question enthuware.ocpjp.v7.2.1353 :
Posted: Sun Nov 29, 2015 8:48 pm
by admin
It should throw an SQLException as per
http://docs.oracle.com/javase/7/docs/ap ... tring(int) if the columnIndex is invalid.
You should try it out.
Re: About Question enthuware.ocpjp.v7.2.1353 :
Posted: Mon Nov 30, 2015 3:57 am
by vcheewei
Thanks admin, I actually read the documentation beforehand. So would that mean answer 2 is correct as well?
"It will throw an exception every time it is run irrespective of what the query returns."
Re: About Question enthuware.ocpjp.v7.2.1353 :
Posted: Mon Nov 30, 2015 7:51 am
by admin
No, what will happen if the query returns 0 rows?
Re: About Question enthuware.ocpjp.v7.2.1353 :
Posted: Tue Aug 02, 2016 1:29 pm
by supafly
The statement at the end "(Assume that items not specified such as import statements and try/catch block are all valid.)" is misleading.
The try with resources statement is part of the try/catch block?
Cheers
Re: About Question enthuware.ocpjp.v7.2.1353 :
Posted: Tue Aug 02, 2016 9:00 pm
by admin
Not sure what you find misleading about it. It just means whatever is not specified i.e. whatever is not shown in the given code is valid. For example, the call to DriverManager.getConnection may throw an exception and so this line must either be wrapped in a try/catch block or a throws clause be used for the method in which this code is present.
It has nothing to do with what is shown in the given code.
-Paul.