Getting SQL Exception when using a prepared statement to select a query

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 everyone, I get the following exception

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

column limit 255 and path = "C: \ Documents and Settings \ hoog \ Desktop \ xyzs". And it works great with statement.So, which is the reason that it throws an exception in a prepared statement

+4
source share
2 answers
 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 + "%"); 
+3
source

Check your single quotes, I think one of them does not skip closing the quote. Also try changing your single quotes to double quotes and avoid them, such as \ "* PGM \"

0
source

Source: https://habr.com/ru/post/1433938/


All Articles