I'm trying to see if there is a better way to do this when I just need one column with one row. Do not mind the lack of use of a prepared statement, I will put it in.
public <T> T getValueFromDb(String col, String table, String cond, Class<T> returnValueClass) {
T val = null;
try {
ResultSet rs = getSet("SELECT " + col + " FROM " + table + " WHERE " + cond);
if(rs.next()) {
if(returnValueClass == Integer.class) {
val = (T) (Integer) rs.getInt(col);
} else if(returnValueClass == Double.class) {
val = (T) (Double) rs.getDouble(col);
} else if(returnValueClass == String.class) {
val = (T) rs.getString(col);
}
}
closeSetQuietly(rs);
} catch(SQLException sql) {
Log.logException(null, sql);
}
return val;
}
Should I just perhaps do just:
returnValueClass.cast(rs.getInt(x));
null
is returned from this method? Does not a value meeting the passed in condition exist? Or did an exception occurred while trying to read from the DB? Or did the passed in class is not one of the few supported ones? – abuzittin gillifirca Jul 17 '13 at 9:04