模糊查詢查不到內(nèi)容
關(guān)鍵console并沒有報(bào)錯(cuò)??!
一下是我的代碼
MessageDao:
package com.imooc.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.imooc.bean.Message;
/**
?* 和message表相關(guān)的數(shù)據(jù)庫操作
?*/
public class MessageDao {
?? ?/**
?? ? * 根據(jù)查詢條件查詢消息列表
?? ? */
?? ?public List<Message> queryMessageList(String command,String description){
?? ??? ?List<Message> messageList = new ArrayList<Message>();
?? ??? ?try {
?? ??? ??? ?Class.forName("com.mysql.jdbc.Driver");
?? ??? ??? ?Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/micro_message?useUnicode=true&characterEncoding=utf8","root","skd2013");
?? ??? ??? ?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'%'?'%'");
?? ??? ??? ??? ?paramList.add(description);
?? ??? ??? ?}
?? ??? ??? ?PreparedStatement st = conn.prepareStatement(sql.toString());
?? ??? ??? ?for (int i=0; i<paramList.size(); i++) {
?? ??? ??? ??? ?st.setString(i + 1, paramList.get(i));
?? ??? ??? ?}
?? ??? ??? ?ResultSet rs = st.executeQuery();
?? ??? ??? ?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"));
?? ??? ??? ?}
?? ??? ?} catch (ClassNotFoundException e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?} catch (SQLException e) {
?? ??? ??? ?// TODO 自動(dòng)生成的 catch 塊
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?return messageList;
?? ?}
}
list.jsp:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
?? ?String path = request.getContextPath();
?? ?String basePath = request.getScheme() + "://"
?? ??? ??? ?+ request.getServerName() + ":" + request.getServerPort()
?? ??? ??? ?+ path + "/";
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE" />
<title>內(nèi)容列表頁面</title>
<link href="<%= basePath %>resource/css/all.css" rel="stylesheet"
?? ?type="text/css" />
</head>
<body style="background: #e1e9eb;">
?? ?<form action="<%= basePath %>List.action" id="mainForm" method="post">
?? ??? ?<div class="right">
?? ??? ??? ?<div class="current">
?? ??? ??? ??? ?當(dāng)前位置:<a href="javascript:void(0)" style="color:#6E6E6E;">內(nèi)容管理</a>
?? ??? ??? ??? ?> 內(nèi)容列表
?? ??? ??? ?</div>
?? ??? ??? ?<div class="rightCont">
?? ??? ??? ??? ?<p class="g_title fix">
?? ??? ??? ??? ??? ?內(nèi)容列表 <a class="btn03" href="#">新 增</a> <a
?? ??? ??? ??? ??? ??? ?class="btn03" href="#">刪 除</a>
?? ??? ??? ??? ?</p>
?? ??? ??? ??? ?<table class="tab1">
?? ??? ??? ??? ??? ?<tbody>
?? ??? ??? ??? ??? ??? ?<tr>
?? ??? ??? ??? ??? ??? ??? ?<td width="90" align="right">指令名稱:</td>
?? ??? ??? ??? ??? ??? ??? ?<td><input name="command" type="text" class="allInput" value="${command}" /></td>
?? ??? ??? ??? ??? ??? ??? ?<td width="90" align="right">描述:</td>
?? ??? ??? ??? ??? ??? ??? ?<td><input name="description" type="text" class="allInput" value="${description}" /></td>
?? ??? ??? ??? ??? ??? ??? ?<td width="85" align="right"><input type="submit" class="tabSub" value="查 詢" /></td>
?? ??? ??? ??? ??? ??? ?</tr>
?? ??? ??? ??? ??? ?</tbody>
?? ??? ??? ??? ?</table>
?? ??? ??? ??? ?<div class="zixun fix">
?? ??? ??? ??? ??? ?<table class="tab2" width="100%">
?? ??? ??? ??? ??? ??? ?<tbody>
?? ??? ??? ??? ??? ??? ??? ?<tr>
?? ??? ??? ??? ??? ??? ??? ??? ?<th><input type="checkbox" id="all" onclick=" "/></th>
?? ??? ??? ??? ??? ??? ??? ??? ?<th>序號(hào)</th>
?? ??? ??? ??? ??? ??? ??? ??? ?<th>指令名稱</th>
?? ??? ??? ??? ??? ??? ??? ??? ?<th>描述</th>
?? ??? ??? ??? ??? ??? ??? ??? ?<th>操作</th>
?? ??? ??? ??? ??? ??? ??? ?</tr>
?? ??? ??? ??? ??? ??? ??? ?<c:forEach items="${messageList}" var="message"
?? ??? ??? ??? ??? ??? ??? ??? ?varStatus="status">
?? ??? ??? ??? ??? ??? ??? ??? ?<tr
?? ??? ??? ??? ??? ??? ??? ??? ??? ?<c:if test="${status.index % 2 != 0}">style='background-color:#ECF6EE;'</c:if>>
?? ??? ??? ??? ??? ??? ??? ??? ??? ?<td><input type="checkbox" /></td>
?? ??? ??? ??? ??? ??? ??? ??? ??? ?<td>${status.index + 1}</td>
?? ??? ??? ??? ??? ??? ??? ??? ??? ?<td>${message.command}</td>
?? ??? ??? ??? ??? ??? ??? ??? ??? ?<td>${message.description}</td>
?? ??? ??? ??? ??? ??? ??? ??? ??? ?<td><a href="#">修改</a> <a href="#">刪除</a>
?? ??? ??? ??? ??? ??? ??? ??? ??? ?</td>
?? ??? ??? ??? ??? ??? ??? ??? ?</tr>
?? ??? ??? ??? ??? ??? ??? ?</c:forEach>
?? ??? ??? ??? ??? ??? ?</tbody>
?? ??? ??? ??? ??? ?</table>
?? ??? ??? ??? ??? ?<div class='page fix'>
?? ??? ??? ??? ??? ??? ?共 <b>4</b> 條 <a href='###' class='first'>首頁</a> <a href='###'
?? ??? ??? ??? ??? ??? ??? ?class='pre'>上一頁</a> 當(dāng)前第<span>1/1</span>頁 <a href='###'
?? ??? ??? ??? ??? ??? ??? ?class='next'>下一頁</a> <a href='###' class='last'>末頁</a> 跳至 <input
?? ??? ??? ??? ??? ??? ??? ?type='text' value='1' class='allInput w28' /> 頁 <a
?? ??? ??? ??? ??? ??? ??? ?href='###' class='go'>GO</a>
?? ??? ??? ??? ??? ?</div>
?? ??? ??? ??? ?</div>
?? ??? ??? ?</div>
?? ??? ?</div>
?? ?</form>
</body>
</html>
2019-02-26
問號(hào)和兩邊的百分號(hào)之間要加上空格? '%' ? '%'