Getting SQL Exception while using prepared statement for select query

点点圈 提交于 2019-12-12 09:46:13

问题


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.


回答1:


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 + "%");



回答2:


Check your single quotes I think one of them is missing closing quote. Also try changing your single quotes to double quotes and escaping them like so \"*PGM\"



来源:https://stackoverflow.com/questions/12399879/getting-sql-exception-while-using-prepared-statement-for-select-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!