为什么我的查不出来 求解

来源:2-6 列表查询

qq_Elaborate_34097817

2016-12-09 16:42

public class ListServlet extends HttpServlet{


@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

try {

req.setCharacterEncoding("utf-8");

String command = req.getParameter("command");

System.out.println(command);

String description = req.getParameter("description");

System.out.println(description);

Class.forName("com.mysql.jdbc.Driver");

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/micro_message","root","123456");

StringBuilder sql = new StringBuilder("select ID,COMMAND,DESCRIPTION,CONTENT from message where 1=1");

List<String> paramList = new ArrayList<String>();

if(command != null && !"".equals(command.trim())){

sql.append(" and COMMAND='"+command+"' ");

paramList.add(command);

}

if(description != null && !"".equals(description.trim())){

sql.append(" and DESCRIPTION like '%' '"+description+"' '%' ");

paramList.add(description);

}

PreparedStatement ps = con.prepareStatement(sql.toString());

for(int i = 0 ; i < paramList.size(); i ++ ){

ps.setString(i + 1, paramList.get(i));

}

ResultSet rs = ps.executeQuery();

List<Message> messageList = new ArrayList<Message>();

while(rs.next()){

Message message = new Message();

messageList.add(message);

message.setId(rs.getString("ID"));

message.setCommand(rs.getString("COMMAND"));

message.setDescription(rs.getString("DESCRIPTION"));

message.setContent(rs.getString("CONTENT"));

}

req.setAttribute("messageList", messageList);

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

req.getRequestDispatcher("/WEB-INF/jsp/back/list.jsp").forward(req, resp);

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

this.doGet(req, resp);

}

}


写回答 关注

1回答

  • Ainsley
    2016-12-09 19:30:41

    sql.append你里面的引号有问题

    qq_Ela...

    谢谢你 我解决了

    2016-12-12 10:52:31

    共 1 条回复 >

通过自动回复机器人学Mybatis---基础版

微信公众号自动回复功能学习Mybatis,基础教程加案例实战方式学习

107412 学习 · 786 问题

查看课程

相似问题