heavydb icon indicating copy to clipboard operation
heavydb copied to clipboard

JDBC PreparedStatement won't work with parameter as last element

Open julianhowarth opened this issue 9 years ago • 4 comments

Using release 3.1.1 and the associated jdbc driver lib.

CREATE TABLE t (x INT NOT NULL);

In Java code, assuming properly set up connection:

PreparedStatement ps = conn.prepareStatement("SELECT x FROM t WHERE x = ?");
ps.setInt(1, 1)

fails with:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
	at com.mapd.jdbc.MapDPreparedStatement.setInt(MapDPreparedStatement.java:175)

This is due to the MapDPreparedStatement at https://github.com/mapd/mapd-core/blob/master/java/mapdjdbc/src/main/java/com/mapd/jdbc/MapDPreparedStatement.java#L84 initially splitting the SQL String using .split. The javadoc for that method states:

Trailing empty strings are therefore not included in the resulting array.

Therefore the subsequent line of code calculates the number of expected parameters as zero.

A simple workaround is to include an additional space at the end of the statement i.e. "SELECT x FROM t WHERE x = ? " but that shouldn't really be necessary.

julianhowarth avatar Jul 13 '17 07:07 julianhowarth

@julianhowarth Thanks for that.

Are you thinking you might prepare PR to fix this?

dwayneberry avatar Jul 13 '17 18:07 dwayneberry

I can do - do you have a set of contributor guidelines/requirements?

julianhowarth avatar Jul 13 '17 18:07 julianhowarth

@julianhowarth

Great, thanks

Yes please see https://github.com/mapd/mapd-core#contributing

dwayneberry avatar Jul 13 '17 18:07 dwayneberry

What's the current status on this one? I see it has been three months since last update, may I take it after I'm done with #29? This one looks quite easy.

Smyatkin-Maxim avatar Oct 13 '17 16:10 Smyatkin-Maxim