Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resultSet.getInt() return value on a string containing two ints

I need to parse the value of a database column that generally contains integers, based on the result Set generated from a JDBC call. However, one particular row of the column has two integers in it (ie, "48, 103"). What will be the return value of resultSet.getInt() on that column?

like image 456
DaveWeber Avatar asked Jul 14 '11 15:07

DaveWeber


3 Answers

It will throw an exception.

I think you are taking the wrong approach here. The getXXX() is supposed to match the data type of the table. Is the data type on the table listed as VARCHAR? If that case you should use getString() to get the data and then parse it with the String.spilt(",") if the , exists (you can use String.indexOf() to verify is the comma is there or not).

like image 150
CoolBeans Avatar answered Sep 27 '22 20:09

CoolBeans


You'll almost certainly get a SQLException (or possibly a NumberFormatException). The actual interface just says that the result set will return "the value of the designated column... as an int". The exact details will be implementation-specific, but I doubt you'll get anything sensible from a value of "48, 103".

(Personally I think it's an error if the driver lets you call getInt on that column in any case, even for "sensible" values. A string is not an int, even if it's a string representation of an int, and the conversion should be done manually by the developer.)

like image 33
Andrzej Doyle Avatar answered Sep 27 '22 21:09

Andrzej Doyle


I'd expect it to throw an exception. If it does give you a value, it won't be what you want. I'd get the values as strings and parse them, splitting on commas and trimming spaces.

like image 40
Nathan Hughes Avatar answered Sep 27 '22 19:09

Nathan Hughes