JDBC如何在具有LIKE子句的查询中传递参数

时间:2015-07-31 15:52:09

标签: java jdbc prepared-statement

问题:传递参数不适用于具有LIKE子句的查询

我正在尝试在参数化查询中传递参数。

工作正常:

String Query = "Select * from ABC where name=?";
Connection con = sql.getSqlDOConnector().getConnection();
PreparedStatement selectStmt = con.prepareStatement(selectQuery);
selectStmt.setString(1, "ABC");
ResultSet rs5 = selectStmt.executeQuery();

有问题:

 String Query = "Select * from ABC where (LOWER(NAME) LIKE \'% ? %\')";
   Connection con = sql.getSqlDOConnector().getConnection();
   PreparedStatement selectStmt = con.prepareStatement(selectQuery);
   selectStmt.setString(1, "ABC");
   ResultSet rs5 = selectStmt.executeQuery();

我正在

java.sql.SQLException: Invalid column index at oracle.jdbc.driver.OraclePreparedStatement.setStringInternal(OraclePreparedStatement.java:5303) at oracle.jdbc.driver.OraclePreparedStatement.setString(OraclePreparedStatement.java:5291) at oracle.jdbc.driver.OraclePreparedStatementWrapper.setString(OraclePreparedStatementWrapper.java:278)

在使用LIKE子句的参数化查询时遇到错误的原因是什么?任何指针都会有所帮助。

0 个答案:

没有答案