java 使用准备好的语句进行选择查询时获取 SQL 异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12399879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:44:51  来源:igfitidea点击:

Getting SQL Exception while using prepared statement for select query

javadb2sqlexception

提问by NKM

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 一起运行良好。那么,它在准备好的语句中抛出异常的原因是什么。

回答by Alok

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

回答by Xitcod13

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