在为select查询使用预准备语句时获取SQL异常

时间:2022-05-02 15:41:36
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.

列限制为255,路径为=“C:\ Documents and Settings \ xyz \ Desktop \ xyzs”,并且使用statement.So运行正常,它在预准备语句中抛出异常的原因是什么。

2 个解决方案

#1


3  

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


0  

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\"

检查你的单引号我认为其中一个缺少收盘报价。还可以尝试将单引号更改为双引号并将其转义为“* PGM”

#1


3  

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


0  

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\"

检查你的单引号我认为其中一个缺少收盘报价。还可以尝试将单引号更改为双引号并将其转义为“* PGM”