Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDBC get/setObject vs. get/setSpecificType

Tags:

java

types

jdbc

JDBC ResultSet offers getObject, getInt, getString etc. methods, and PreparedStatement has analogous setters. Apart from type compile-time type safety, do the type specific getters/setters have any (dis)advantages, or is it OK to use getObject/setObject everywhere?

like image 823
Joonas Pulakka Avatar asked Mar 30 '10 06:03

Joonas Pulakka


1 Answers

There are no real technical (dis)advantages. They may only be functionally disadvantageous if you're doing typechecking/casting yourself afterwards.

I myself use ResultSet#getObject() only when the returned value is a primitive which is DB-defaulted to NULL and the declared value is a wrapper for the primitive. E.g. Integer age:

user.setAge(resultSet.getObject("age") != null ? resultSet.getInt("age") : null);

And I use PreparedStatement#setObject() practically all the time, in an utility method:

public static void setValues(PreparedStatement preparedStatement, Object... values) throws SQLException {
    for (int i = 0; i < values.length; i++) {
        preparedStatement.setObject(i + 1, values[i]);
    }
}
like image 134
BalusC Avatar answered Oct 12 '22 23:10

BalusC