As said in the commments, it depends on how the JDBC driver implements the ResultSet
interface. For example, the MySQL jdbc driver implements the two getInt()
methods similar to
public int getInt(int columnIndex) throws SQLException {
...
}
public int getInt(String columnName) throws SQLException {
int idx = findColumn(columnName);
return getInt(idx);
}
Naturally, getInt(int)
is faster than getInt(String)
since the String
variant requires an additional mapping from the column name to the column index.
Does it matter in practice? Most likely not. If unsure, create test data and benchmark it. Other factors like the fetch size will impact your overall performance much more.
I would always prefer the getInt(String)
variant, since it is more robust against modifying the SELECT
statement (adding/deleting columns from the projection).