项目结构:
Servlet:
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import service.ListService;
import bean.Message;
/*
* 列表页面初始化控制
*/
@SuppressWarnings("serial")
public class ListServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置编码
req.setCharacterEncoding("UTF-8");
//接受页面的值
String command = req.getParameter("command");
String description = req.getParameter("description");
//向页面传值
req.setAttribute("command", command);
req.setAttribute("description", description);
//业务需要调用service
ListService listService = new ListService();
//查询消息列表并传给页面
req.setAttribute("messageList", listService.queryMessageList(command, description));
//向页面跳转
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);
}
}
bean:
package bean;
/**
* 与消息表对应的实体类 (alt+shift+j:自动添加类的文档注释)
*/
public class Message {
/*
* 主键
*/
private String id;
/*
* 指令名称
*/
private String command;
/*
* 描述
*/
private String description;
/*
* 内容
*/
private String content;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCommand() {
return command;
}
public void setCommand(String command) {
this.command = command;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
jdbc的dao:
package 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 bean.Message;
/**
*和message表相关的数据库操作
*/
public class MessageDao {
/*
* 根据查询条件查询消息列表
*/
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://127.0.0.1:3306/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=?");
paramList.add(command);
}
if(description != null && !"".equals(description.trim())){
sql.append(" and DESCRIPTION like '%' ? '%'");
paramList.add(description);
}
PreparedStatement statement = conn.prepareStatement(sql.toString());
for (int i=0; i<paramList.size(); i++) {
statement.setString(i+1, paramList.get(i));
}
ResultSet rs = statement.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) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return messageList;
}
}
service层(logic层,与servlet对应):
package service;
import java.util.List;
import dao.MessageDao;
import bean.Message;
/**
*列表相关的业务功能
*/
public class ListService {
public List<Message> queryMessageList(String command, String description){
MessageDao messageDao = new MessageDao();
return messageDao.queryMessageList(command, description);
}
}