開發環境:JavaSE1.7、JavaEE7.0、JSTL1.2.2、Web2.3、MySQL5.5.28
系統分析與功能設計:
本系統實現商品信息的管理,應包括以下幾個功能:
商品信息列表:列出所有商品信息,並提供對指定商品信息的修改和刪除接口
添加商品信息:向數據庫中添加一條商品信息
編輯商品信息:修改數據庫中已有的商品信息
刪除商品信息:刪除指定商品的信息
異常處理:跳轉到錯誤頁面並顯示異常信息
MVC框架模式設計:
(1)模型:Proccess.java完成商品信息的在數據庫中的增刪改查,將要處理的商品信息封裝到Goods.java中。
(2)控制器:Controller.java完成區別客戶端不同業務請求,根據不同的參數進行不同的操作。
(3)視圖:list.jsp顯示商品信息列表,同時提供增加、編輯和刪除鏈接。
edit.jsp添加或編輯信息
error.jsp顯示異常信息
除此以外添加監聽器,當應用關閉時關閉與數據庫的鏈連接。
項目結構:
效果截圖:
注意事項:
(1)在Web項目中導入MySQL架包要把jar復制到WebRoot/WEB-INF/lib下。
(2)Web2.3中使用JSTL時需要在jsp上加這兩句:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false" %>
(3)PreparedStatement能夠對sql語句進行預編譯,預編譯后能夠提高數據庫sql語句執行效率。用於處理動態SQL語句,在執行前會有一個預編譯過程,這個過程是有時間開銷的,雖然相對數據庫的操作,該時間開銷可以忽略不計,但是PreparedStatement的預編譯結果會被緩存,下次執行相同的預編譯語句時,就不需要編譯,只要將參數直接傳入編譯過的語句執行代碼
中就會得到執行,所以,對於批量處理可以大大提高效率。
Proccess.java

1 package mvc.model; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 import java.sql.Statement; 9 import java.util.ArrayList; 10 import java.util.List; 11 12 public class Proccess { 13 private static Connection conn; 14 15 private void getConn() throws ClassNotFoundException, SQLException{ 16 Class.forName("com.mysql.jdbc.Driver"); 17 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/goods?characterEncoding=UTF-8", 18 "root", "123456"); // url user passwd 19 } 20 21 private Goods fill(ResultSet rs) throws SQLException{ 22 Goods gd = new Goods(); 23 gd.setHh(rs.getString("hh")); 24 gd.setName(rs.getString("name")); 25 gd.setNum(rs.getString("number")); 26 return gd; 27 } 28 29 public List<Goods> list() throws ClassNotFoundException, SQLException{ 30 List<Goods> gds = new ArrayList<Goods>(); 31 if(conn == null){ 32 getConn(); 33 } 34 Statement statement = conn.createStatement(); 35 ResultSet rs = statement.executeQuery("select * from goods"); 36 while(rs.next()){ 37 gds.add(fill(rs)); 38 } 39 rs.close(); 40 statement.close(); 41 return gds; 42 } 43 44 public Goods findByHH(String hh) throws ClassNotFoundException, SQLException{ 45 Goods gd = null; 46 if(conn == null){ 47 getConn(); 48 } 49 PreparedStatement pstatement = conn.prepareStatement("select * from goods where hh= ?"); 50 pstatement.setString(1, hh); 51 ResultSet rs = pstatement.executeQuery(); 52 if(rs.next()){ 53 gd = fill(rs); 54 } 55 return gd; 56 } 57 58 public void save(Goods gd, String oldHh) throws ClassNotFoundException, SQLException{ 59 if(conn == null){ 60 getConn(); 61 } 62 String sql = "update goods set hh=?,name=?,number=? where hh=?"; 63 if(oldHh == null || "".equals(oldHh)){ 64 sql = "insert into goods set hh=?,name=?,number=?"; 65 } 66 PreparedStatement pstatement = conn.prepareStatement(sql); 67 pstatement.setString(1, gd.getHh()); 68 pstatement.setString(2, gd.getName()); 69 pstatement.setString(3, gd.getNum()); 70 if(oldHh != null && !("".equals(oldHh))){ 71 pstatement.setString(4, oldHh); 72 } 73 pstatement.executeUpdate(); 74 } 75 76 public void delete(String Hh) throws ClassNotFoundException, SQLException{ 77 if(conn == null){ 78 getConn(); 79 } 80 String sql = "delete from goods where hh=?"; 81 PreparedStatement pstatement = conn.prepareStatement(sql); 82 pstatement.setString(1, Hh); 83 pstatement.executeUpdate(); 84 } 85 86 public static void conClose(){ 87 if(conn != null){ 88 try { 89 conn.close(); 90 } catch (SQLException e) { 91 e.printStackTrace(); 92 } 93 } 94 } 95 }
Controller.java

1 package mvc.controller; 2 3 import java.io.IOException; 4 import java.util.List; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import mvc.model.Goods; 12 import mvc.model.Proccess; 13 14 public class Controller extends HttpServlet { 15 private static final long serialVersionUID = 1L; 16 17 public Controller() { 18 super(); 19 } 20 21 public void destroy() { 22 super.destroy(); 23 } 24 25 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 26 doPost(request, response); 27 } 28 29 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 30 request.setCharacterEncoding("utf-8"); 31 String action = request.getParameter("action"); 32 Proccess pc = new Proccess(); 33 try { 34 if ("list".equals(action)) { 35 List<Goods> goods = pc.list(); 36 String test = "Test"; 37 request.setAttribute("goods", goods); 38 request.setAttribute("test", test); 39 request.getRequestDispatcher("list.jsp").forward(request, response); 40 } else if ("add".equals(action)) { 41 request.getRequestDispatcher("edit.jsp").forward(request, response); 42 } else if ("edit".equals(action)) { 43 String hh = request.getParameter("hh"); 44 Goods gds = pc.findByHH(hh); 45 request.setAttribute("gds", gds); 46 request.getRequestDispatcher("edit.jsp").forward(request, response); 47 48 } else if ("save".equals(action)) { 49 String oldHh = request.getParameter("oldHh"); 50 String hh = request.getParameter("hh"); 51 String name = request.getParameter("name"); 52 String num = request.getParameter("num"); 53 54 Goods gds = new Goods(); 55 gds.setHh(hh); 56 gds.setName(name); 57 gds.setNum(num); 58 pc.save(gds, oldHh); 59 response.sendRedirect("ctrl?action=list"); 60 61 } else if ("delete".equals(action)) { 62 String hh = request.getParameter("hh"); 63 pc.delete(hh); 64 response.sendRedirect("ctrl?action=list"); 65 } 66 } catch (Exception e) { 67 request.setAttribute("errMsg", e.getMessage()); 68 request.getRequestDispatcher("error.jsp").forward(request, response); 69 e.printStackTrace(); 70 } 71 } 72 73 public void init() throws ServletException { 74 } 75 }