Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL resultSet.getLong() results in ArrayIndexOutOfBoundsException when column type is bigint

i have a view in PostgreSQL 9.1 with a column of type bigint. This type should be mapped to Long in Java but actually is mapped to BigInteger. So

resultSet.getLong(columnPos)

results in an ArrayIndexOutOfBoundsException.

resultSet.getBigInteger(columnPos)

or

resultSet.get(columnPos)

both with a following toString and parse work fine. What would be the correct handling of this? Should i first get the BigInteger, call toString and parse the Long? Or Is there a better approach to tell the ResultSet or the ScrollableResults the correct Java column type?

Thank you.

like image 939
Julia Avatar asked Nov 13 '22 14:11

Julia


1 Answers

The bigint datatype is correctly mapped to BigInteger since that's what it says: it is a big integer which might not fit into a Long.

You could use resultSet.get(columnPos) and then check the class of the returned object.

We wrote a utility class for that, so we'd do something like this:

public Long getLongFromResultSet( ResultSet rs, int columnPos ) {
  Object value = rs.get( columnPos );

  if( value instanceof Number) { //this should handle any numeric Java type like BigInteger, Long etc.
    return ((Number)value).longValue(); //autoboxing here
  }  

  ... //handle non-Number cases
}
like image 118
Thomas Avatar answered Apr 26 '23 15:04

Thomas