imooc视频学习笔记 ----> URL:http://www.imooc.com/learn/154
ListServlet.java
package com.imooc.servlet; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.imooc.service.ListService; /** * 列表页面初始化控制 */ @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); // 查询消息列表并传给页面 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 { // TODO Auto-generated method stub this.doGet(req, resp); } }
MessageDao.java
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; public class MessageDao { // 数据库驱动 private static final String JDBC_DRIVER = "org.gjt.mm.mysql.Driver"; // 数据库地址 private static final String DB_URL = "jdbc:mysql://localhost:3306/miro_message"; // 用户名与密码 private static final String USER = "root"; private static final String PASS = "pass"; private static Connection conn = null; // 加载数据库驱动 static { try { Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } // 获取消息列表 public List<Message> queryMessageList(String command, String description) { List<Message> messageList = new ArrayList<>(); // SQL拼接 StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.append("select ID, COMMAND, DESCRIPTION, CONTENT from MESSAGE where 1=1"); List<String> paramList = new ArrayList<>(); // 判断指令是否为空 if (command != null && !"".equals(command.trim())) { sqlBuilder.append(" and COMMAND=?"); paramList.add(command); } // 判断描述是否为空 if (description != null && !"".equals(description.trim())) { sqlBuilder.append(" and DESCRIPTION like ‘%‘ ? ‘%‘"); paramList.add(description); } String sql = sqlBuilder.toString(); PreparedStatement prep = null; ResultSet result = null; try { prep = conn.prepareStatement(sql); // 设置SQL参数 for (int i = 0; i != paramList.size(); ++i) { prep.setString(i+1, paramList.get(i)); } // 执行查找操作 result = prep.executeQuery(); while (result.next()) { // 把查找结果放进List里 Message message = new Message(); messageList.add(message); message.setId(result.getString("ID")); message.setCommand(result.getString("COMMAND")); message.setDescription(result.getString("DESCRIPTION")); message.setContent(result.getString("CONTENT")); } } catch (SQLException e) { e.printStackTrace(); } // 如果出现异常就返回一个空的List return messageList; } }
ListService.java
package com.imooc.service; import java.util.List; import com.imooc.bean.Message; import com.imooc.dao.MessageDao; /** * * 列表相关的业务功能 * */ public class ListService { /** * 获取消息列表 */ public List<Message> queryMessageList(String command, String description) { MessageDao dao = new MessageDao(); return dao.queryMessageList(command, description); } }
时间: 2024-12-28 01:39:16