tags:

views:

47

answers:

4

Say that I have a JDBC ResultSet, and I call the getLong() or getshort() method.

For which of the following SQL types {SMALLINT, INT, BIGINT} should I get long, and for which types should I get an error?

In other words, if I have an INT and I want a SMALLINT (A short), would I get it, or would I get an error? Similarly, if I have an INT and want a BIGINT (a long), would I get it, or would I get an error?

The Javadocs (listed below) say nothing.

public long getLong(int columnIndex) throws SQLException

Retrieves the value of the designated column in the current row

of this ResultSet object as a long in the Java programming language.

Parameters:
    columnIndex - the first column is 1, the second is 2, ... 
Returns:
    the column value; if the value is SQL NULL, the value returned is 0 
Throws:
    SQLException - if a database access error occurs
+1  A: 

It'll cast it to a long and it should be fine.

You'll get an error if you're trying to get a long from a string containing "Bob", or some other field that can't be easily converted to a long.

Will Hartung
That's what I'm wondering about and whether this is implementation dependent. I feel like the documentation is ambiguous about this. In the case that it is a null, I know I will get a 0. But it seems like I won't even get a runtime warning in this case.
Uri
It most likely is very implementation dependent. That just means you'll need to test it against several DBs. But, the cold truth is that many apps, even using "portable" SQL are DB specific, so it's not usually a big problem. Obviously, "it depends".
Will Hartung
+1  A: 

The spec doesn't say anything about this behavior. This will totally depend on the implementation of the drivers.

With the MySQL Connector, you can almost get anything looking like a number as long as it's in valid numeric format and it's in the range of long. Null/False are also returned as 0.

ZZ Coder
+2  A: 

From the Retrieving Values from Result Sets section of the Java tutorials:

JDBC allows a lot of latitude as far as which getXXX methods you can use to retrieve the different SQL types. For example, the method getInt can be used to retrieve any of the numeric or character types. The data it retrieves will be converted to an int; that is, if the SQL type is VARCHAR , JDBC will attempt to parse an integer out of the VARCHAR. The method getInt is recommended for retrieving only SQL INTEGER types, however, and it cannot be used for the SQL types BINARY, VARBINARY, LONGVARBINARY, DATE , TIME, or TIMESTAMP.

I'm interpreting that to mean that the data will be coerced. It should work just fine if it's an upcast, but I'd expect potential loss of precision (naturally) if, for example, you're reading a LONG value using getInt(). I'd expect an Exception to be thrown if you try to read text using getInt().

Bill the Lizard
Wonderful... I'm writing a wrapper for JDBC's ResultSet and was hoping there was an unambiguous interpretation of the int types that is not implementation dependent. Thank you for finding this!
Uri
You can call DataBaseMetaData.supportsConvert(int fromType, int toType) to see if a given implementation supports a given conversion.
Yishai
A: 

This is implementation dependent. The spec says that the ResultSet implemenation may support such a conversion, and you can check by calling DataBaseMetaData.supportsConvert(int fromType, int toType) (Section 15.2.3.1 of the 4.0 implementer spec).

Best is to not rely on the behavior but rather check the ResultSetMetaData for the correct type.

Yishai