In JDBC, why do parameter indexes for prepared statements begin at 1 instead of 0?

Paul Wicks picture Paul Wicks · Mar 5, 2009 · Viewed 10.4k times · Source

Everywhere else in Java, anything with an index starts at 0. Is there a reason for the change here or is this just bad design?

Answer

erickson picture erickson · Mar 5, 2009

Historically, databases have used 1-based indexing for bound parameters. This probably reflects the origins of relational databases in set theory and mathematics, which index elements starting with one, and use zero to represent a null or empty set.

In shell scripts and regular expressions, the zero index usually means something "special". For example, in the case of shell scripts, the zeroth "argument" is actually the command that was invoked.

The choice for JDBC was deliberate but, ultimately, probably causes more confusion and difficulty than it solves.