Retrieving an object generically
One option would be to use the ResultSet.getObject()
. The method documentation says that the type of object returned will be the JDBC specification to the corresponding type of database.
So, just use the instanceof
operator to test the returned type or the isAssignableFrom()
" class. For example:
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2");
Object o = rs.getObject(1);
if (Integer.class.isAssignableFrom(o.getClass()))
System.out.println("true"); //verdadeiro
if (o instanceof Integer)
System.out.println("true"); //verdadeiro
Getting detailed information about the results
A second option is to retrieve the resulting ResultSetMetaData
the query. This is a more complex form, but it allows you to get accurate information about the columns.
See an example of how to get the columns and types:
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2");
ResultSetMetaData rsmd = rs.getMetaData();
//quantidade de colunas
int numberOfColumns = rsmd.getColumnCount();
int type = rsmd.getColumnType(1);
if (type == Types.VARCHAR || type == Types.CHAR) {
//executa alguma ação
}