开发环境:JavaSE1.7、JavaEE7.0、JSTL1.2.2、Web2.3、MySQL5.5.28

 

系统分析与功能设计

本系统实现商品信息的管理,应包括以下几个功能:

商品信息列表:列出所有商品信息,并提供对指定商品信息的修改和删除接口

添加商品信息:向数据库中添加一条商品信息

编辑商品信息:修改数据库中已有的商品信息

删除商品信息:删除指定商品的信息

异常处理:跳转到错误页面并显示异常信息

JavaWeb中MVC的使用--以管理系统举例

MVC框架模式设计:

(1)模型:Proccess.java完成商品信息的在数据库中的增删改查,将要处理的商品信息封装到Goods.java中。

(2)控制器:Controller.java完成区别客户端不同业务请求,根据不同的参数进行不同的操作。

(3)视图:list.jsp显示商品信息列表,同时提供增加、编辑和删除链接。

edit.jsp添加或编辑信息

error.jsp显示异常信息

除此以外添加监听器,当应用关闭时关闭与数据库的链连接。

 

 项目结构:

JavaWeb中MVC的使用--以管理系统举例

 

 

效果截图:

JavaWeb中MVC的使用--以管理系统举例

注意事项:

(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 }
Proccess

相关文章:

  • 2021-07-14
  • 2021-12-18
  • 2021-06-11
  • 2022-12-23
  • 2021-11-21
  • 2022-01-08
  • 2021-09-16
  • 2021-12-05
猜你喜欢
  • 2021-12-17
  • 2021-12-30
  • 2021-08-24
  • 2021-11-27
  • 2021-04-06
  • 2021-12-12
相关资源
相似解决方案