jsp 中 mysql 模糊查询_jsp mysql 模糊查询中文字

在JavaEE应用中使用JSP进行MySQL模糊查询时遇到问题,当SQL语句`SELECT * FROM BOOK WHERE userId=? AND name LIKE BINARY '%?%'`通过预编译设置参数时,出现`Parameter index out of range (2 > number of parameters, which is 1)`异常。尝试在命令行将问号替换为中文可以正常工作,但在程序中不生效。问题可能出在参数绑定或字符编码上,寻求解决方案。" 123909844,13121654,力扣76题解:最小覆盖子串与Integer比较陷阱,"['Java', '数据结构', '字符串算法']
摘要由CSDN通过智能技术生成

前面省略.....Stringsql="select*fromBOOKwhereuserId=?andnameLIKEBINARY'%?%'";//这里try{conn=DBConnection.getConnection();ps=conn.prepareStatement(sql);ps.setInt(1,userId);...

前面省略.....

String sql = "select * from BOOK where userId= ? and name LIKE BINARY '%?%'"; //这里

try {

conn = DBConnection.getConnection();

ps = conn.prepareStatement(sql);

ps.setInt(1, userId);//这里

ps.setString(2, name);//这里

rs = ps.executeQuery();

while (rs.next()) {

Book book = new Book();

book.setId(rs.getInt("id"));

book.setName(rs.getString("name"));

book.setSex(rs.getString("sex"));

book.setPhone(rs.getString("phone"));

book.setAddress(rs.getString("address"));

book.setMobilePhone(rs.getString("mobilePhone"));

book.setCompany(rs.getString("company"));

book.setComPhone(rs.getString("comPhone"));

book.setComAddress(rs.getString("comAddress"));

後面省略...

查不出数据,提示:

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

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)

at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3326)

at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3310)

at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4146)

at y2javaee.xmal1.operation.BookBo.SearchBook(BookBo.java:168)

at y2javaee.xmal1.servlet.SearchServlet.doPost(SearchServlet.java:54)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)

at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)

at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)

at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)

at java.lang.Thread.run(Unknown Source)

sql语句应该是正确的,在cmd里把问号变成中文就能查询出,但是代入程序就不行,哪位大神能明白哪里出问题 在线等

展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值