ResultSet.getInt () returns a value for a string containing two ints

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

+4
source share
4 answers

It throws an exception.

I think you're wrong here. getXXX() is assumed to match the data type of the table. Is the data type in the table like VARCHAR? In this case, you should use getString () to get the data, and then parse it with String.spilt (",") if it exists (you can use String.indexOf () to check for a comma or not).

+5
source

You will almost certainly get a SQLException (or perhaps a NumberFormatException ). The actual interface simply says that the result set will return "the value of the specified column ... as int ". The exact details will be implementation specific, but I doubt you will get anything reasonable from the value "48, 103" .

(Personally, I consider the error if the driver allows you to call getInt in this column in any case, even for "reasonable" values. The string is not int, even if it is a string representation of int, and the conversion must be done manually by the developer.)

+4
source

I expect him to throw an exception. If it gives you meaning, it will not be what you want. I would get the values ​​as strings and parse them, separating commas and trimming spaces.

+1
source

I believe this is a NumberFormatException .

0
source

All Articles