PreparedStatement忽略查询中的参数:java.sql.SQLException:参数索引超出范围(1>参数个数,为0) [英] PreparedStatement ignores parameters in the query: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0)

查看:1090
本文介绍了PreparedStatement忽略查询中的参数:java.sql.SQLException:参数索引超出范围(1>参数个数,为0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use java and jdbc driver (java.sql), and I got this piece of code:

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.setxxx在语句字符串中查找问号?我在声明中使用了一个问号,但它似乎被忽略了!

Why is that? doesn't statement.setxxx looks for question marks in the statement string? I have used one question mark in the statement, but it seems to be ignored!

推荐答案

试试这个

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

这篇关于PreparedStatement忽略查询中的参数:java.sql.SQLException:参数索引超出范围(1>参数个数,为0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆