Getting SQL Exception while using prepared statement for select query

NKM picture NKM · Sep 13, 2012 · Viewed 16.5k times · Source
StringBuilder sqlQry = new StringBuilder();
sqlQry.append("SELECT LIB, PATH")
.append(" FROM OBJ")
.append(" INNER JOIN SRC ON SRC.MBR = OBJ.LOBJ")
.append(" WHERE  TYPE = '*PGM'")
.append(" AND SRC.PATH LIKE '").append("?").append("%'");

PreparedStatement ps = accssConn.prepareStatement(sqlQry.toString());
ps.setString(1, path);

rs = ps.executeQuery();

Hi All, I am getting following exception

[jcc][10145][10844][3.63.123] Invalid parameter 1: Parameter index is out of range. ERRORCODE=-4461, SQLSTATE=42815

column limit is 255 and path is = "C:\Documents and Settings\xyz\Desktop\xyzs" and it is run fine with statement.So , what is the reason that it is throwing exception in prepared statement.

Answer

Alok picture Alok · Sep 13, 2012
StringBuilder sqlQry = new StringBuilder();

sqlQry.append("SELECT LIB, PATH")
.append(" FROM OBJ")
.append(" INNER JOIN SRC ON SRC.MBR = OBJ.LOBJ")
.append(" WHERE  TYPE = '*PGM'")
.append(" AND SRC.PATH LIKE ").append("?");

PreparedStatement ps = accssConn.prepareStatement(sqlQry.toString());

ps.setString(1, path + "%");