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
The wasNull() method of the ResultSet interface determines whether the last column read had a Null value. i.e. whenever you read the contents of a column of the ResultSet using the getter methods (getInt(), getString etc...) you can determine whether it (column) contains null values, using the wasNull() method.
Hence, when a Java programmer needs to determine if ResultSet is empty or not, it just calls the next() method and if next() returns false it means ResultSet is empty.
This method returns a boolean value specifying whether the ResultSet object contains more rows. If there are no rows next to its current position this method returns false, else it returns true. Using this method in the while loop you can iterate the contents of the result set.
A ResultSet object maintains a cursor that points to the current row in the result set. The term "result set" refers to the row and column data contained in a ResultSet object. Navigational methods − Used to move the cursor around.
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()
.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With