5

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?

5
  • What is the definition of the column in the database - some kind of varchar? Commented Jul 14, 2011 at 16:02
  • Eng.Fouad - I'm currently solving a few other problems that are stopping me from being to test it immediately - trying to get ahead of the curve by asking here first. Commented Jul 14, 2011 at 16:05
  • How can 2 rows returned by one select statement differ in its number of columns? Commented Jul 14, 2011 at 16:05
  • What DBMS are you using? Commented Jul 14, 2011 at 16:07
  • The select statement returns one row in particular. However, most rows in the column only contain one number in a string - the row that I need to work with has two numbers separated by a comma. The DBMS that I am using is MySQL Commented Jul 14, 2011 at 16:22

4 Answers 4

6

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

Sign up to request clarification or add additional context in comments.

3 Comments

This sounds right. Thanks for the feedback - I'm updating some existing code so maybe the getInt worked before when they knew they were only dealing with one value in the column.
@DaveWeber - you are welcome. Do you have access to see the schema for the table? Validate what the data type is listed there as.
I should have access (it's a syscolumns table), will try to do so later. Thanks for your help.
5

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

2 Comments

I'd think NumberFormat is more likely, as SQLExceptions only occur on database access error. It's grabbing the value fine, it's just an issue with parsing the number. But I do think you're correct about the getInt part not working on text columns.
This makes a lot of sense. Thanks for the feedback.
1

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.

Comments

0

I believe it's a NumberFormatException.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.