java - PreparedStatement 忽略查询中的参数 : java. sql.SQLException : Parameter index out of range (1 > number of parameters, 为 0)

标签 java mysql jdbc

我使用java和jdbc驱动程序(java.sql),我得到了这段代码:

String clinetIP = "220.181.108.89";
String sql = "SELECT * FROM `as` WHERE `as`.ip_range LIKE '%?%'";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, clinetIP);

我得到:

java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

执行statement.setString(1, clinetIP)时。

这是为什么呢? statements.setxxx 不会在语句字符串中查找问号吗?我在声明中使用了一个问号,但它似乎被忽略了!

最佳答案

试试这个

String sql = "SELECT * FROM `as` WHERE `as`.ip_range LIKE ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, "%" + clientIP + "%");

关于java - PreparedStatement 忽略查询中的参数 : java. sql.SQLException : Parameter index out of range (1 > number of parameters, 为 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18652703/

相关文章:

php - 经常使用的 PDO 行数

sql - SQL语句-Java

java.sql.SQLException : Smart-large-object error informix

java - 从图库中选择图像后 Android 应用程序崩溃 -cursor.getColumnIndex 返回 -1

php - MySQL、PHP : Hide specific Value from table

MySQL INSERT 未按预期工作

tomcat - 如何在没有 ClassNotFound 异常的 Apache Tomcat 中配置 Impala/Hive2 JDBC 驱动程序

java - Eclipse:如何在 Tomcat 上部署引用其他动态 Web 项目的动态 Web 项目

java - 在 Node js 中通过 AES/GCM/NoPadding 算法使用 key 和 iv 加密有效负载并在 java 中解密

java - 谷歌应用引擎 : couldn't start server - jetty