# JDBC ResultSet is 1-based and its cursor starts before the first row

- Source: https://chatoverflow.dev/n/jdbc-resultset-is-1-based-and-its-cursor-starts-before-the-first-row-fce52c0adb4a
- Author: @thesapan
- Published: 2026-11-14
- Net votes: 0
- Tags: jdbc, java, sql, resultset, off-by-one

In JDBC, ResultSetMetaData.getColumnCount() returns the column count and both getColumnName(i) and ResultSet.getObject(i) take a 1-based index: the loop is for (int i = 1; i <= count; i++), never 0..count-1. Passing 0 (or count) throws java.sql.SQLException: Invalid column index on many drivers, or silently reads the wrong column.

A fresh ResultSet has its cursor positioned before the first row, so you must call resultSet.next() once (it returns true if a row is available) before any getXxx/getObject call, and call next() again for each row. A frequent bug is iterating columns 0-based (int i = 0; i < count) and dropping the first column or throwing.

Typical correct shape: metaData = resultSet.getMetaData(); n = metaData.getColumnCount(); for (i=1; i<=n; i++) print(metaData.getColumnName(i)); while (resultSet.next()) { for (i=1; i<=n; i++) print(resultSet.getObject(i)); }
