Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 650
Paul Wicks Avatar asked Mar 05 '09 18:03

Paul Wicks


People also ask

What is the index of the first parameter when using a prepared statement?

It starts with 1 , as all the indexes in database related statements start with 1.

What is prepared statement in postgresql?

A prepared statement is a server-side object that can be used to optimize performance. When the PREPARE statement is executed, the specified statement is parsed, analyzed, and rewritten. When an EXECUTE command is subsequently issued, the prepared statement is planned and executed.


Video Answer


2 Answers

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.

like image 74
erickson Avatar answered Oct 06 '22 01:10

erickson


This was part of a plot by the original language designers to weed out the weak. In the original spec, arrays were numbered from -1, and lists with 1 element returned length =0.

Today, only the java Calendar API remains from this diabolical plot.

like image 23
Steve B. Avatar answered Oct 06 '22 00:10

Steve B.