如题,我的Dao层代码如下(感觉没毛病......):
public class MessageDao {
public ArrayList<Message> queryMessageList(String command,String description){
ArrayList<Message> messageList = new ArrayList<Message>();
Message message = null;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/micro_message","root","root");
StringBuilder sql = new StringBuilder("select ID,COMMAND,DESCRIPTION,CONTENT from message where 1=1 ");
ArrayList<String> paramList = new ArrayList<String>();
if(command!=null && !command.trim().equals("")){
System.out.println("command不为空");
sql.append(" and COMMAND=? ");
paramList.add(command);
}
if(description!=null && !description.trim().equals("")){
System.out.println("description不为空");
sql.append(" and DESCRIPTION LIKE '%' ? '%' ");
paramList.add(description);
}
PreparedStatement statement = conn.prepareStatement(sql.toString());
for(int i=0;i<paramList.size();i++){
System.out.println(paramList.get(i));
statement.setString(i+1, paramList.get(i));
}
ResultSet rs = statement.executeQuery();
while(rs.next()){
message = new Message();
message.setId(rs.getInt("ID"));
message.setCommand(rs.getString("COMMAND"));
message.setContent(rs.getString("CONTENT"));
message.setDescription(rs.getString("DESCRIPTION"));
messageList.add(message);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return messageList;
}
}
然后执行查询后,网页上没有显示查询的信息,经过在控制台打印输出,查询的对象集合为空,如下截图:
上面打印了两次,但我只执行了一次(点击了一次查询按钮,问题应该在这里.....)
各位大神,技术控能找到问题出在哪儿吗??求赐教!!!下面是其他层相关代码供参考。
service层代码
public class ListService {
public ArrayList<Message> queryMessageList(String command,String description){
MessageDao messageDao = new MessageDao();
return messageDao.queryMessageList(command, description);
}
}
servlet代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码,获取页面的值,向页面传值
request.setCharacterEncoding("utf-8");
String command = request.getParameter("command");
String description = request.getParameter("description");
request.setAttribute("command", command);
request.setAttribute("description", description);
//查询消息列表
ListService listService = new ListService();
request.setAttribute("messageList", listService.queryMessageList(command, description));
System.out.println(listService.queryMessageList(command, description));
//跳转
request.getRequestDispatcher("/WEB-INF/jsp/back/list.jsp").forward(request, response);
}
jsp:
<form action="<%= basePath %>ListServlet" id="mainForm" method="post">
<div class="right">
<div class="current">当前位置:<a href="javascript:void(0)" style="color:#6E6E6E;">内容管理</a> > 内容列表</div>
<div class="rightCont">
<p class="g_title fix">内容列表 <a class="btn03" href="#">新 增</a> <a class="btn03" href="#">删 除</a></p>
<table class="tab1">
<tbody>
<tr>
<td width="90" align="right">指令名称:</td>
<td>
<input type="text" name="command" class="allInput" value="${command}"/>
</td>
<td width="90" align="right">描述:</td>
<td>
<input type="text" name="description" class="allInput" value="${description}"/>
</td>
<td width="85" align="right"><input type="submit" class="tabSub" value="查 询" /></td>
</tr>
</tbody>
</table>
......