模棱0
2016-10-30 15:55
SERVLET代码
try {
req.setCharacterEncoding("utf-8");
String command = req.getParameter("command");
String description = req.getParameter("description");
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/micro_message?characterEncoding=utf-8", "root",
"root");
StringBuilder sql = new StringBuilder("select ID,COMMAND,DESCRIPTION,CONTENT from MESSAGE where 1=1");
List<String> paramList = new ArrayList<>();
if (command != null && !"".equals(command.trim())) {
sql.append(" and COMMAND=?");
paramList.add(command);
}
if (description != null && !"".equals(description.trim())) {
sql.append(" and DESCRIPTION like '%' ? '%'");
paramList.add(description);
}
PreparedStatement statement = connection.prepareStatement(sql.toString());
for (int i = 0; i < paramList.size(); i++) {
statement.setString(i + 1, paramList.get(i));
}
ResultSet resultSet = statement.executeQuery();
List<Message> messageList = new ArrayList<>();
while (resultSet.next()) {
Message message = new Message();
messageList.add(message);
message.setId(resultSet.getString("ID"));
message.setCommand(resultSet.getString("COMMAND"));
message.setDescription(resultSet.getString("DESCRIPTION"));
message.setContent(resultSet.getString("CONTENT"));
}
req.setAttribute("messageList", messageList);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
jsp页面
<body style="background: #e1e9eb;">
<form action="<%=basePath %>list.action" 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 name="command" type="text" class="allInput" value=""/>
</td>
<td width="90" align="right">描述:</td>
<td>
<input name="description" type="text" class="allInput" value=""/>
</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>序号</th>
<th>指令名称</th>
<th>描述</th>
<th>操作</th>
</tr>
<c:forEach items="${messageList }" var="message" varStatus="status">
<tr<c:if test="${status.index % 2 == 1 }">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>
我解决了,我的数据库问题,连接时拼上编码
晕,试了楼主的代码可以查到呀,你那边有报错吗?
是不是哪个地方没有配置好?
通过自动回复机器人学Mybatis---基础版
107412 学习 · 786 问题
相似问题