模糊查询使用占位符出现问题
public class ListServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub try { req.setCharacterEncoding("UTF-8"); //首先不管是不是第一次查看列表页面,先得到查询条件 String command=(String) req.getParameter("command"); String description=(String) req.getParameter("description"); req.setAttribute("command", command); req.setAttribute("description", description); Connection connection=ConnectionDB.getConnection(); StringBuilder sql=new StringBuilder("select ID,COMMAND,DESCRIPTION,CONTENT from message where 1=1"); //判断得到的参数是不是空,还有是不是空字符串,把条件存放在集合中,因为使用了占位符,下边要进行赋值 List<String> list=new ArrayList<String>(); if(command!=null && !command.trim().equals("")){ sql.append(" and COMMAND = ?"); // sql.append(" and COMMAND = "+command); list.add(command); } if(description!=null && !description.trim().equals("")){ sql.append(" and DESCRIPTION like '%'?'%'"); // sql.append(" and DESCRIPTION like '%"+description+"%'"); list.add(description); } // System.out.println(sql); PreparedStatement statement=connection.prepareStatement(sql.toString()); for(int i=0;i<list.size();i++){ statement.setNString(i+1,list.get(i)); } ResultSet rs=statement.executeQuery(); List<Message> messages=new ArrayList<Message>(); while(rs.next()){ Message message=new Message(); message.setId(rs.getInt("ID")); message.setCommand(rs.getString("COMMAND")); message.setDescription(rs.getString("DESCRIPTION")); message.setContent(rs.getString("CONTENT")); messages.add(message); } //跳转界面 req.setAttribute("messageList", messages); req.getRequestDispatcher("/WEB-INF/jsp/list.jsp").forward(req, resp); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
不明白为什么这样使用占位符会报错!!!!!
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3729)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3713)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4553)
at com.mysql.jdbc.PreparedStatement.setNString(PreparedStatement.java:5262)
at com.company.servlet.ListServlet.doGet(ListServlet.java:55)
at com.company.servlet.ListServlet.doPost(ListServlet.java:80)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1083)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:640)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
我也换了其他的方式,但是就是一直报这个错误,我也是服了,我感觉我没有什么问题的呀!!!!