DaveWeber
DaveWeber

Reputation: 141

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?

Upvotes: 5

Views: 7393

Answers (4)

Jake Roussel
Jake Roussel

Reputation: 641

I believe it's a NumberFormatException.

Upvotes: 0

CoolBeans
CoolBeans

Reputation: 20800

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).

Upvotes: 6

Andrzej Doyle
Andrzej Doyle

Reputation: 103797

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.)

Upvotes: 5

Nathan Hughes
Nathan Hughes

Reputation: 96394

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.

Upvotes: 1

Related Questions