zoukankan      html  css  js  c++  java
  • 仓库物资管理

    总结:在这次的测验中看清楚题目的要求,没有分析好项目制作的调理,导致在做的时候比较盲目,所有在制作的后期比较慌乱。并且在此次实验中还犯了一个比较致命的错误,就是在进行数据库的操作过程中运用了和数据库相近的字符,导致添加和修改操作都无法进行,还有就是在数据的类型定义的时候不够认真,在界面中的字符与数据表中的的类型不符合,也导致无法对数据库进行操作,导致无法进行相关操作,所以浪费了大量的时间。

    这是在这次项目中所建的文件:这个项目中运用了javabean和Servlet

     首先要建立两个数据表,以存放商品信息和仓库物资信息:我建的是shop1和house

    然后就是运用javabean,我在com.jdbc.bean包里建立了两个类分别是Shop类和House类

    Shop类路径 /house/src/com/jdbc/bean/Shop.java

     1 package com.jdbc.bean;
     2 
     3 public class Shop {
     4 
     5     private int id;
     6     public Shop(String name, String product, String type, String guige) {
     7         // TODO 自动生成的构造函数存根
     8         this.name = name;
     9         this.guige = guige;
    10         this.type = type;
    11         this.product = product;
    12     }
    13     public Shop(int id, String name, String product, String type, String guige) {
    14         // TODO 自动生成的构造函数存根
    15         this.id = id;
    16         this.name = name;
    17         this.guige = guige;
    18         this.type = type;
    19         this.product = product;
    20         
    21     }
    22     public int getId() {
    23         return id;
    24     }
    25     public void setId(int id) {
    26         this.id = id;
    27     }
    28     public String getName() {
    29         return name;
    30     }
    31     public void setName(String name) {
    32         this.name = name;
    33     }
    34     public String getProduct() {
    35         return product;
    36     }
    37     public void setProduct(String product) {
    38         this.product = product;
    39     }
    40     public String getType() {
    41         return type;
    42     }
    43     public void setType(String type) {
    44         this.type = type;
    45     }
    46     public String getGuige() {
    47         return guige;
    48     }
    49     public void setGuige(String guige) {
    50         this.guige = guige;
    51     }
    52     private String name;
    53     private String product;
    54     private String type;
    55     private String guige;
    56 }

    House类:路径 /house/src/com/jdbc/bean/House.java

    代码:

     1 package com.jdbc.bean;
     2 
     3 import java.sql.Date;
     4 import java.sql.Time;
     5 import java.sql.Timestamp;
     6 
     7 public class House {
     8 
     9     private int id;
    10     public House(String name, String product, String type, String guige, int number, Timestamp timeStamp,
    11             String place, String person) {
    12         // TODO 自动生成的构造函数存根
    13         this.name = name;
    14         this.product = product;
    15         this.type = type;
    16         this.guige = guige;
    17         this.number = number;
    18         this.date = timeStamp;
    19         this.place = place;
    20         this.person = person;
    21     }
    22     public int getId() {
    23         return id;
    24     }
    25     public void setId(int id) {
    26         this.id = id;
    27     }
    28     public String getName() {
    29         return name;
    30     }
    31     public void setName(String name) {
    32         this.name = name;
    33     }
    34     public String getProduct() {
    35         return product;
    36     }
    37     public void setProduct(String product) {
    38         this.product = product;
    39     }
    40     public String getType() {
    41         return type;
    42     }
    43     public void setType(String type) {
    44         this.type = type;
    45     }
    46     public String getGuige() {
    47         return guige;
    48     }
    49     public void setGuige(String guige) {
    50         this.guige = guige;
    51     }
    52     public int getNumber() {
    53         return number;
    54     }
    55     public void setNumber(int number) {
    56         this.number = number;
    57     }
    58     public Timestamp getDate() {
    59         return date;
    60     }
    61     public void setDate(Timestamp date) {
    62         this.date = date;
    63     }
    64     public String getTime() {
    65         return time;
    66     }
    67     public void setTime(String time) {
    68         this.time = time;
    69     }
    70     public String getPlace() {
    71         return place;
    72     }
    73     public void setPlace(String place) {
    74         this.place = place;
    75     }
    76     public String getPerson() {
    77         return person;
    78     }
    79     public void setPerson(String person) {
    80         this.person = person;
    81     }
    82     private String name;
    83     private String product;
    84     private String type;
    85     private String guige;
    86     private int number;
    87     private Timestamp date;
    88     private String time;
    89     private String place;
    90     private String person;
    91     
    92     
    93     
    94 }

    然后下一部要建立数据库连接,这部分我放在com.jdbc.util包中实现

    在com.jdbc.util建立BaseConnection类,以实现数据库的连接与关闭:

    路径 /house/src/com/jdbc/util/BaseConnection.java

    代码如下:

     1 package com.jdbc.util;
     2 
     3 import java.sql.Connection;
     4 import java.sql.DriverManager;
     5 import java.sql.ResultSet;
     6 import java.sql.SQLException;
     7 import java.sql.Statement;
     8 
     9 public class BaseConnection {
    10     
    11      public static Connection getConnection(){//用这个方法获取mysql的连接
    12          Connection conn=null;
    13          String driver = "com.mysql.jdbc.Driver";
    14          String url = "jdbc:mysql://localhost:3306/sql?characterEncoding=utf8&useSSL=true";
    15          String user = "root";
    16          String password = "gy1212";
    17          try{
    18              Class.forName(driver);//加载驱动类
    19              conn=DriverManager.   
    20                      getConnection(url,user,password);//(url数据库的IP地址,user数据库用户名,password数据库密码)
    21          }catch(Exception e){
    22              e.printStackTrace();
    23          }
    24          return conn;
    25      }
    26      
    27      public static void close (Statement state, Connection conn) {
    28             if (state != null) {
    29                 try {
    30                     state.close();
    31                 } catch (SQLException e) {
    32                     e.printStackTrace();
    33                 }
    34             }
    35             
    36             if (conn != null) {
    37                 try {
    38                     conn.close();
    39                 } catch (SQLException e) {
    40                     e.printStackTrace();
    41                 }
    42             }
    43         }
    44         
    45         public static void close (ResultSet rs, Statement state, Connection conn) {
    46             if (rs != null) {
    47                 try {
    48                     rs.close();
    49                 } catch (SQLException e) {
    50                     e.printStackTrace();
    51                 }
    52             }
    53             
    54             if (state != null) {
    55                 try {
    56                     state.close();
    57                 } catch (SQLException e) {
    58                     e.printStackTrace();
    59                 }
    60             }
    61             
    62             if (conn != null) {
    63                 try {
    64                     conn.close();
    65                 } catch (SQLException e) {
    66                     e.printStackTrace();
    67                 }
    68             }
    69         }
    70 
    71 
    72     public static void main(String[] args) {
    73         System.out.println("连接成功");
    74     }
    75 }

    并且要定义两个类对bean进行操作,这里我把它分装在com.jdbc.dao包中,用ShopDao和HouseDao来实现两个类的基本操作。

    ShopDao:路径 /house/src/com/jdbc/dao/ShopDao.java

    这段代码实现了商品信息的添加,根据四中属性的任意一种进行模糊查询,根据名称删除,修改

    代码如下:

      1 package com.jdbc.dao;
      2 
      3 import java.sql.Connection;
      4 import java.sql.PreparedStatement;
      5 import java.sql.ResultSet;
      6 import java.sql.SQLException;
      7 import java.sql.Statement;
      8 import java.util.ArrayList;
      9 import java.util.List;
     10 
     11 import com.jdbc.bean.Shop;
     12 import com.jdbc.util.BaseConnection;
     13 
     14 public class ShopDao {
     15 
     16     
     17     public static boolean add(Shop shop)
     18     {
     19         boolean f = false;
     20         Connection conn = BaseConnection.getConnection();
     21         //PreparedStatement ps=null;
     22         String sql = "insert into shop1(name, product, type, guige) values('" + shop.getName() + "','" + shop.getProduct() + "','" + shop.getType() + "','" + shop.getGuige() + "')";
     23         //String sql = "insert into course1(name,teacher,classroom) values('" + cour.getName() + "','" + cour.getTeacher() + "','" + cour.getClassroom() + "')'";
     24         Statement state = null;
     25         int a = 0;
     26         try {
     27             state = conn.createStatement();
     28             a = state.executeUpdate(sql);
     29             
     30             
     31         } catch (SQLException e) {
     32             // TODO 自动生成的 catch 块
     33             e.printStackTrace();
     34         }
     35         
     36         if(a >0)
     37         {
     38             System.out.println("添加成功");
     39             f = true;
     40         }else {
     41             System.out.println("失败");
     42         }
     43         return f;
     44     }
     45     
     46     public static Shop getByName(String name)
     47     {
     48         
     49         String sql = "select * from shop1 where name ='" + name + "'";
     50         Connection conn = BaseConnection.getConnection();
     51         Shop shop = null;
     52         Statement state = null;
     53         ResultSet rs = null;
     54         
     55         try {
     56             state = conn.createStatement();
     57             rs = state.executeQuery(sql);
     58             while (rs.next()) {
     59                 int id = rs.getInt("id");
     60                 String product = rs.getString("product");
     61                 String type = rs.getString("type");
     62                 String guige = rs.getString("guige");
     63                 shop = new Shop(id, name, product, type , guige);
     64             }
     65         } catch (SQLException e) {
     66             // TODO 自动生成的 catch 块
     67             e.printStackTrace();
     68         }finally {
     69             BaseConnection.close(rs, state, conn);
     70         }
     71         return shop;
     72     }
     73     
     74     public static boolean delete(int id)
     75     {
     76         boolean f = false;
     77         String sql = "delete from shop1 where id='" + id + "'";
     78         Statement state = null;
     79         Connection conn = BaseConnection.getConnection();
     80         
     81         int a = 0;
     82         
     83         try {
     84             state = conn.createStatement();
     85             a = state.executeUpdate(sql);
     86             
     87         } catch (SQLException e) {
     88             // TODO 自动生成的 catch 块
     89             e.printStackTrace();
     90         }finally {
     91             BaseConnection.close(state, conn);
     92         }
     93         
     94         if(a > 0)
     95         {
     96             System.out.println("删除成功");
     97             f = true;
     98         }
     99         
    100         
    101         
    102         return f;
    103         
    104     }
    105     
    106     public static List<Shop> find(String name ,String product ,String type , String guige)
    107     {
    108         
    109         List<Shop> list = new ArrayList<Shop>();
    110         
    111         String sql = "select * from shop1 where ";
    112         if (name != "") {
    113             sql += "name like '%" + name + "%'";
    114         }
    115         if (product != "") {
    116             sql += "product like '%" + product + "%'";
    117         }
    118         if (type != "") {
    119             sql += "type like '%" + type + "%'";
    120         }
    121         if (guige != "") {
    122             sql += "guige like '%" + guige + "%'";
    123         }
    124         
    125         Connection conn = BaseConnection.getConnection();
    126         Statement state = null;
    127         ResultSet rs = null;
    128         
    129         try {
    130             state = conn.createStatement();
    131             rs = state.executeQuery(sql);
    132             Shop shop = null;
    133             while (rs.next()) {
    134                 
    135                 int id = rs.getInt("id");
    136                 String name2 = rs.getString("name");
    137                 String product2 = rs.getString("product");
    138                 String type2 = rs.getString("type");
    139                 String guige2 = rs.getString("guige");
    140                 shop = new Shop(id, name2, product2, type2 , guige2);
    141                 list.add(shop);
    142             }
    143         } catch (SQLException e) {
    144             // TODO 自动生成的 catch 块
    145             e.printStackTrace();
    146         }finally
    147         {
    148             BaseConnection.close(rs, state, conn);
    149         }
    150         return list;
    151     }
    152     
    153     public static boolean update(Shop shop )
    154     {
    155         boolean f = false;
    156         
    157         Connection conn= BaseConnection.getConnection();
    158         PreparedStatement ps=null;
    159         String sql="update shop1 set product=?,type=?,guige=? where name=?";
    160         try{
    161              ps=conn.prepareStatement(sql);
    162              
    163              ps.setString(1,shop.getProduct());             
    164              ps.setString(2,shop.getType());
    165              ps.setString(3,shop.getGuige());
    166              ps.setString(4, shop.getName());
    167              int a=ps.executeUpdate();
    168          if(a>0){
    169                  f = true;
    170                  System.out.println("修改成功");
    171              }else{
    172                  System.out.println("修改失败");
    173              }
    174          }catch(Exception e){
    175              e.printStackTrace();
    176          }finally{
    177              try{
    178                    if(ps!=null){
    179                        ps.close();
    180                    }if(conn!=null){
    181                        conn.close();
    182                    }
    183                }catch(Exception e2){
    184                    e2.printStackTrace();
    185                }
    186          }
    187 
    188 
    189     return f;
    190     }
    191     
    192 }

    HouseDao:路径 /house/src/com/jdbc/dao/HouseDao.java

    实现了输的添加和根据商品名称进行模糊查询

    代码如下:

      1 package com.jdbc.dao;
      2 
      3 import java.sql.Connection;
      4 import java.sql.PreparedStatement;
      5 import java.sql.ResultSet;
      6 import java.sql.SQLException;
      7 import java.sql.Statement;
      8 import java.sql.Timestamp;
      9 import java.util.ArrayList;
     10 import java.util.List;
     11 
     12 import com.jdbc.bean.House;
     13 import com.jdbc.bean.Shop;
     14 import com.jdbc.util.BaseConnection;
     15 
     16 public class HouseDao {
     17 
     18     public static boolean add(House house)
     19     {
     20         boolean f = false;
     21          Connection conn= BaseConnection.getConnection();
     22         
     23         String sql = "insert into house values(?,?,?,?,?,?,?,?)";
     24 
     25          PreparedStatement ps=null;
     26          try{
     27             
     28              ps= conn.prepareStatement(sql);//把写好的sql语句传递到数据库,让数据库知道我们要干什么
     29              
     30              ps.setString(1,house.getName());            
     31              ps.setString(2,house.getProduct());             
     32              ps.setString(3,house.getType());
     33              ps.setString(4,house.getGuige());    
     34              ps.setInt(5,house.getNumber());    
     35              ps.setTimestamp(6,house.getDate());    
     36              ps.setString(7,house.getPlace());    
     37              ps.setString(8,house.getPerson());    
     38              
     39              int a=ps.executeUpdate();//这个方法用于改变数据库数据,a代表改变数据库的条数
     40              if(a>0){
     41                  f = true;            
     42                  System.out.println("添加成功");                 
     43              }else{
     44                  System.out.println("添加失败");
     45                  
     46              }
     47          }catch(Exception e){
     48              e.printStackTrace();
     49          }try{
     50                if(ps!=null){
     51                    ps.close();
     52                }if(conn!=null){
     53                    conn.close();
     54                }
     55            }catch(Exception e2){
     56                  e2.printStackTrace();
     57              }
     58 
     59         
     60         return f;
     61     }
     62     
     63     public static List<House> find(String name )
     64     {
     65         
     66         List<House> list = new ArrayList<House>();
     67         
     68         String sql = "select * from house where  ";
     69         
     70             sql += "name like '%" + name + "%'";/*
     71 
     80         
     81         Connection conn = BaseConnection.getConnection();
     82         Statement state = null;
     83         ResultSet rs = null;
     84         
     85         try {
     86             state = conn.createStatement();
     87             rs = state.executeQuery(sql);
     88             House house = null;
     89             while (rs.next()) {
     90                 
     91                 
     92                 String name2 = rs.getString("name");
     93                 String product2 = rs.getString("product");
     94                 String type2 = rs.getString("type");
     95                 String guige2 = rs.getString("guige");
     96                 int number = rs.getInt("number");
     97                 Timestamp date = rs.getTimestamp("date");
     98                 String place = rs.getString("place");
     99                 String person = rs.getString("person");
    100                 
    101                 house = new House(name2, product2, type2 , guige2,number,date,place,person);
    102                 list.add(house);
    103             }
    104         } catch (SQLException e) {
    105             // TODO 自动生成的 catch 块
    106             e.printStackTrace();
    107         }finally
    108         {
    109             BaseConnection.close(rs, state, conn);
    110         }
    111         return list;
    112     }
    113 }

    之后应该处理的就是Servlet类了,这里我建立了两个Servlet类,分别对不同的bean进行操作

    HouseServlet:路径 /house/src/com/jdbc/servlet/HouseServlet.java

    代码如下

      1 package com.jdbc.servlet;
      2 
      3 import java.io.IOException;
      4 import java.io.UnsupportedEncodingException;
      5 import java.sql.Timestamp;
      6 import java.util.Date;
      7 import java.util.List;
      8 
      9 import javax.servlet.ServletException;
     10 import javax.servlet.annotation.WebServlet;
     11 import javax.servlet.http.HttpServlet;
     12 import javax.servlet.http.HttpServletRequest;
     13 import javax.servlet.http.HttpServletResponse;
     14 
     15 import com.jdbc.bean.House;
     16 import com.jdbc.dao.HouseDao;
     17 import com.jdbc.dao.ShopDao;
     18 
     19 /**
     20  * Servlet implementation class HouseServlet
     21  */
     22 @WebServlet("/HouseServlet")
     23 public class HouseServlet extends HttpServlet {
     24     private static final long serialVersionUID = 1L;
     25        
     26     /**
     27      * @see HttpServlet#HttpServlet()
     28      */
     29     public HouseServlet() {
     30         super();
     31         // TODO Auto-generated constructor stub
     32     }
     33 
     34     /**
     35      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     36      */
     37     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     38         // TODO Auto-generated method stub
     39         //response.getWriter().append("Served at: ").append(request.getContextPath());
     40         response.setContentType("text/html;charset=UTF-8");
     41         request.setCharacterEncoding("UTF-8");
     42         String method = request.getParameter("method");
     43         
     44         if(method.equals("add"))
     45         {
     46             add(request,response);
     47         }if (method.equals("find")) {
     48             find(request,response);
     49         }
     50         
     51     }
     52 
     53     private void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     54         // TODO 自动生成的方法存根
     55         response.setContentType("text/html;charset=UTF-8");
     56         request.setCharacterEncoding("UTF-8");
     57         
     58         String name = request.getParameter("name");
     59         List<House> list = HouseDao.find(name) ;
     60         request.setAttribute("list", list);
     61         request.getRequestDispatcher("houseResult.jsp").forward(request,response);
     62     }
     63 
     64     private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     65         // TODO 自动生成的方法存根
     66         response.setContentType("text/html;charset=UTF-8");
     67         request.setCharacterEncoding("UTF-8");
     68         
     69         String name = request.getParameter("name");
     70         String product = request.getParameter("product");
     71         String type = request.getParameter("type");
     72         String guige = request.getParameter("guige");
     73         int number = Integer.parseInt(request.getParameter("number"));
     74         Date date = new Date();
     75         Timestamp timeStamp = new Timestamp(date.getTime());
     76         String place = request.getParameter("place");
     77         String person = request.getParameter("person");
     78         
     79         House house = new House(name, product,type,guige,number,timeStamp,place,person);
     80         if (HouseDao.add(house)) {
     81             
     82                 request.setAttribute("message", "添加成功");
     83                 request.getRequestDispatcher("houseadd.jsp").forward(request,response);
     84         }else {
     85                 request.setAttribute("message", "添加失败");
     86                 request.getRequestDispatcher("houseadd.jsp").forward(request,response);
     87             }
     88         
     89 
     90         
     91     }
     92 
     93     /**
     94      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     95      */
     96     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     97         // TODO Auto-generated method stub
     98         doGet(request, response);
     99     }
    100 
    101 }

    ShopServlet:路径 /house/src/com/jdbc/servlet/ShopServlet.java

    代码如下:

      1 package com.jdbc.servlet;
      2 
      3 import java.io.IOException;
      4 import java.io.UnsupportedEncodingException;
      5 import java.util.List;
      6 
      7 import javax.servlet.ServletException;
      8 import javax.servlet.annotation.WebServlet;
      9 import javax.servlet.http.HttpServlet;
     10 import javax.servlet.http.HttpServletRequest;
     11 import javax.servlet.http.HttpServletResponse;
     12 
     13 import com.jdbc.bean.Shop;
     14 import com.jdbc.dao.ShopDao;
     15 
     16 /**
     17  * Servlet implementation class ShopServlet
     18  */
     19 @WebServlet("/ShopServlet")
     20 public class ShopServlet extends HttpServlet {
     21     private static final long serialVersionUID = 1L;
     22        
     23     /**
     24      * @see HttpServlet#HttpServlet()
     25      */
     26     public ShopServlet() {
     27         super();
     28         // TODO Auto-generated constructor stub
     29     }
     30 
     31     /**
     32      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     33      */
     34     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     35         // TODO Auto-generated method stub
     36         //response.getWriter().append("Served at: ").append(request.getContextPath());
     37         response.setContentType("text/html;charset=UTF-8");
     38         request.setCharacterEncoding("UTF-8");
     39         String method = request.getParameter("method");
     40         
     41         if(method.equals("add"))
     42         {
     43             add(request,response);
     44         }else if (method.equals("getByName")) {
     45             getByName(request,response);
     46         }else if (method.equals("delete")) {
     47             delete(request,response);
     48         }else if (method.equals("find")) {
     49             find(request,response);
     50         }else if (method.equals("update")) {
     51             update(request, response);
     52         }
     53     }
     54 
     55     private void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     56         // TODO 自动生成的方法存根
     57         response.setContentType("text/html;charset=UTF-8");
     58         request.setCharacterEncoding("UTF-8");
     59         
     60         String name = request.getParameter("name");
     61         String product = request.getParameter("product");
     62         String type = request.getParameter("type");
     63         String guige = request.getParameter("guige");
     64         List<Shop> list =  ShopDao.find(name,product,type,guige);
     65         request.setAttribute("list", list);
     66         //response.setHeader("refresh", "0;url=findResult.jsp");
     67         request.getRequestDispatcher("findResult.jsp").forward(request,response);
     68     }
     69     
     70     private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     71         // TODO 自动生成的方法存根
     72         String name = request.getParameter("name");
     73         String product = request.getParameter("product");
     74         String type = request.getParameter("type");
     75         String guige = request.getParameter("guige");
     76         Shop shop = new Shop(name, product, type, guige);
     77         if(ShopDao.update(shop))
     78         {
     79             request.setAttribute("message", "修改成功");
     80             request.getRequestDispatcher("main.jsp").forward(request, response);
     81         }else
     82         {
     83             request.setAttribute("message", "修改失败");
     84             request.getRequestDispatcher("update.jsp").forward(request, response);
     85         }
     86     }
     87 
     88     private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     89         // TODO 自动生成的方法存根
     90         request.setCharacterEncoding("UTF-8");
     91         response.setHeader("content-type","text/html;charset=UTF-8");
     92         
     93         int id = Integer.parseInt(request.getParameter("id"));
     94         
     95         if (ShopDao.delete(id)) {
     96             request.setAttribute("message", "删除成功");
     97             request.getRequestDispatcher("main.jsp").forward(request, response);
     98         }else {
     99             request.setAttribute("message", "删除失败");
    100             request.getRequestDispatcher("delete.jsp").forward(request, response);
    101         }
    102     }
    103 
    104     private void getByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    105         // TODO 自动生成的方法存根
    106         request.setCharacterEncoding("UTF-8");
    107         String name = request.getParameter("name");
    108         Shop shop =ShopDao.getByName(name);
    109         if(shop == null)
    110         {
    111             
    112             request.setAttribute("message", "查无此课程!");
    113             request.getRequestDispatcher("delete.jsp").forward(request,response);
    114         }else
    115         {
    116             System.out.println(shop.getId());
    117             request.setAttribute("shop", shop);
    118             request.getRequestDispatcher("del.jsp").forward(request,response);
    119         }
    120     }
    121 
    122     private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    123         // TODO 自动生成的方法存根
    124 request.setCharacterEncoding("UTF-8");
    125         
    126         String name = request.getParameter("name");
    127         String product = request.getParameter("product");
    128         String type = request.getParameter("type");
    129         String guige = request.getParameter("guige");
    130         
    131         Shop shop = new Shop(name, product, type, guige);
    132         
    133         if (ShopDao.add(shop)) {
    134             request.setAttribute("message", "添加成功");
    135             request.getRequestDispatcher("add.jsp").forward(request,response);
    136         }else {
    137             request.setAttribute("message", "添加失败");
    138             request.getRequestDispatcher("add.jsp").forward(request,response);
    139         }
    140     }
    141     
    142     
    143 
    144     /**
    145      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    146      */
    147     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    148         // TODO Auto-generated method stub
    149         doGet(request, response);
    150     }
    151 
    152 }

    然后就是进行页面的设计了:

    首先建立主界面:main.jsp:路径 /house/WebContent/add.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 <div align="center">
    11         
    12         <div class="a">
    13             <a href="add.jsp">商品信息添加</a>
    14         </div>
    15         <div class="a">
    16             <a href="update.jsp">商品信息修改</a>
    17         </div>
    18         <div class="a">
    19             <a href="delete.jsp">商品信息删除</a>
    20         </div>
    21         <div class="a">
    22             <a href="find.jsp">商品信息查询</a>
    23         </div>
    24         <div class="a">
    25             <a href="houseadd.jsp">入库信息添加</a>
    26         </div>
    27         <div class="a">
    28             <a href="housefind.jsp">入库信息查询</a>
    29         </div>
    30     </div>
    31 </body>
    32 </html>

    然后是商品信息的添加:add.jsp:路径 /house/WebContent/add.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>添加</title>
     8 </head>
     9 <body>
    10 <%
    11          Object message = request.getAttribute("message");
    12          if(message!=null && !"".equals(message)){
    13      
    14     %>
    15          <script type="text/javascript">
    16               alert("<%=request.getAttribute("message")%>");
    17          </script>
    18     <%} %>
    19     <div align="center">
    20         <h1 style="color: black;">商品信息录入</h1>
    21         <a href="main.jsp">返回主页</a>
    22         <form action="${pageContext.request.contextPath}/ShopServlet?method=add" method="post" onsubmit="return check()">
    23             <div class="a">
    24                 商品名称<input type="text" id="name" name="name"/>
    25             </div>
    26             <div class="a">
    27                 生产厂家<input type="text" id="product" name="product" />
    28             </div>
    29             <div class="a">
    30                 商品型号<input type="text" id="type" name="type" />
    31             </div>
    32             <div class="a">
    33                 商品规格<input type="text" id="guige" name="guige" />
    34             </div>
    35             <div class="a">
    36                 <button type="submit" class="b">保&nbsp;&nbsp;&nbsp;存</button>
    37             </div>
    38         </form>
    39     </div>
    40     <script type="text/javascript">
    41         function check() {
    42             var name = document.getElementById("name");;
    43             var product = document.getElementById("product");
    44             var type = document.getElementById("type");
    45             var guige = document.getElementById("guige");
    46             
    47             //非空 
    48             if(name.value == '') {
    49                 alert('商品名称为空');
    50                 name.focus();
    51                 return false;
    52             }
    53             if(product.value == '') {
    54                 alert('生产厂家为空');
    55                 product.focus();
    56                 return false;
    57             }
    58             if(type.value == '') {
    59                 alert('商品型号为空');
    60                 type.focus();
    61                 return false;
    62             }if(guige.value == '') {
    63                 alert('商品规格');
    64                 guige.focus();
    65                 return false;
    66             }
    67         }
    68             </script>
    69 </body>
    70 </html>

    商品删除界面:delete.jsp:路径 /house/WebContent/delete.jsp

    代码如下:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>删除</title>
     8 </head>
     9 <body>
    10 <% 
    11  Object message = request.getAttribute("message");
    12          if(message!=null && !"".equals(message)){
    13      
    14     %>
    15          <script type="text/javascript">
    16               alert("<%=request.getAttribute("message")%>");
    17          </script>
    18     <%} %>
    19     
    20     <div align="center">
    21         <h1 style="color: black;">商品信息删除</h1>
    22         <a href="main.jsp">返回主页</a>
    23         <form action="${pageContext.request.contextPath}/ShopServlet?method=getByName" method="post" onsubmit="return check()">
    24             <div class="a">
    25                 商品名称<input type="text" id="name" name="name"/>
    26             </div>
    27             <div class="a">
    28                 <button type="submit" class="b">查&nbsp;&nbsp;&nbsp;找</button>
    29             </div>
    30         </form>
    31     </div>
    32     <script type="text/javascript">
    33         function check() {
    34             var name = document.getElementById("name");;
    35             
    36             //非空
    37             if(name.value == '') {
    38                 alert('商品名称为空');
    39                 name.focus();
    40                 return false;
    41             }
    42         }
    43     </script>
    44 </body>
    45 </html>

    然后通过另外一个界面对删除信息进行确认:

    del.jsp:路径 /house/WebContent/del.jsp

    代码如下:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>确认删除</title>
     8 </head>
     9 <body>
    10 <style>
    11     .a{
    12         margin-top: 20px;
    13     }
    14     .b{
    15         font-size: 20px;
    16          160px;
    17         color: white;
    18         background-color: greenyellow;
    19     }
    20     .tb, td {
    21         border: 1px solid black;
    22         font-size: 22px;
    23     }
    24 </style>
    25 </head>
    26 <body>
    27 <div align="center">
    28         <h1 style="color: black;">商品信息删除</h1>
    29         <a href="main.jsp">返回主页</a>
    30         <table class="tb">
    31             <tr>
    32                 <td>商品名称</td>
    33                 <td>${shop.name}</td>
    34             </tr>
    35             <tr>
    36                 <td>商品厂家</td>
    37                 <td>${shop.product}</td>
    38             </tr>
    39             <tr>
    40                 <td>商品型号</td>
    41                 <td>${shop.type}</td>
    42             </tr>
    43             <tr>
    44                 <td>商品规格</td>
    45                 <td>${shop.guige}</td>
    46             </tr>
    47         </table>
    48         <div class="a">
    49             <a onclick="return check()" href="${pageContext.request.contextPath}/ShopServlet?method=delete&id=${shop.id}">删&nbsp;&nbsp;&nbsp;除</a>
    50         </div>
    51     </div>
    52     <script type="text/javascript">
    53         function check() {
    54             if (confirm("真的要删除吗?")){
    55                 return true;
    56             }else{
    57                 return false;
    58             }
    59         }
    60     </script>
    61 </body>
    62 </html>

    商品查找界面:find.jsp:路径 /house/WebContent/find.jsp

    代码如下:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>查询</title>
    </head>
    <body>
    <div align="center">
            <h1 style="color: black;">商品信息查询</h1>
            <a href="main.jsp">返回主页</a>
            <form action="${pageContext.request.contextPath}/ShopServlet?method=find" method="post" onsubmit="return check()">
                <div class="a">
                    商品名称<input type="text" id="name" name="name"/>
                </div>
                <div class="a">
                    商品厂家<input type="text" id="product" name="product" />
                </div>
                <div class="a">
                    商品型号<input type="text" id="type" name="type" />
                </div>
                <div class="a">
                    商品规格<input type="text" id="guige" name="guige" />
                </div>
                <div class="a">
                    <button type="submit" class="b">查&nbsp;&nbsp;&nbsp;询</button>
                </div>
            </form>
        </div>
        <script type="text/javascript">
            function check() {
                var name = document.getElementById("name");;
                var product = document.getElementById("product");
                var type = document.getElementById("type");
                var guige = document.getElementById("guige");
                
                //非空
                if(name.value == '' && product.value == '' && type.value == '' && guige.value == '') {
                    alert('请填写一个条件');
                    return false;
                }
            }
        </script>
    </body>
    </html>

    查找结果的输出:findResult:路径 /house/WebContent/findResult.jsp

    代码如下:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
         <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>查询结果</title>
    </head>
    <body>
    <div align="center">
            <h1 style="color: black;">课程信息列表</h1>
            <a href="main.jsp">返回主页</a>
            <table class="tb">
                <tr>
                    <td>id</td>
                    <td>商品名称</td>
                    <td>生产厂家</td>
                    <td>商品型号</td>
                    <td>商品规格</td>
                </tr>
                <!-- forEach遍历出adminBeans -->
                <c:forEach items="${list}" var="item" varStatus="status">
                    <tr>
                        <td>${item.id}</td>
                        <td><a>${item.name}</a></td>
                        <td>${item.product}</td>
                        <td>${item.type}</td>
                        <td>${item.guige}</td>
                    </tr>
                </c:forEach>
            </table>
        </div>
    </body>
    </html>

    商品信息的修改:路径 /house/WebContent/update.jsp

    代码如下:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 <%
    11          Object message = request.getAttribute("message");
    12          if(message!=null && !"".equals(message)){
    13      
    14     %>
    15          <script type="text/javascript">
    16               alert("<%=request.getAttribute("message")%>");
    17          </script>
    18     <%} %>
    19     <div align="center">
    20         <h1 style="color: black;">商品信息修改</h1>
    21         <a href="main.jsp">返回主页</a>
    22         <form action="${pageContext.request.contextPath}/ShopServlet?method=update" method="post" onsubmit="return check()">
    23             <div class="a">
    24                 商品名称<input type="text" id="name" name="name"/>
    25             </div>
    26             <div class="a">
    27                 生产厂家<input type="text" id="product" name="product" />
    28             </div>
    29             <div class="a">
    30                 商品型号<input type="text" id="type" name="type" />
    31             </div>
    32             <div class="a">
    33                 商品规格<input type="text" id="guige" name="guige" />
    34             </div>
    35             <div class="a">
    36                 <button type="submit" class="b">保&nbsp;&nbsp;&nbsp;存</button>
    37             </div>
    38         </form>
    39     </div>
    40     <script type="text/javascript">
    41         function check() {
    42             var name = document.getElementById("name");;
    43             var product = document.getElementById("product");
    44             var type = document.getElementById("type");
    45             var guige = document.getElementById("guige");
    46             
    47             //非空 
    48             if(name.value == '') {
    49                 alert('商品名称为空');
    50                 name.focus();
    51                 return false;
    52             }
    53             if(product.value == '') {
    54                 alert('生产厂家为空');
    55                 product.focus();
    56                 return false;
    57             }
    58             if(type.value == '') {
    59                 alert('商品型号为空');
    60                 type.focus();
    61                 return false;
    62             }if(guige.value == '') {
    63                 alert('商品规格');
    64                 guige.focus();
    65                 return false;
    66             }
    67         }
    68             </script>
    69 </body>
    70 </html>

    对于仓库信息的添加:houseadd.jsp:路径 /house/WebContent/houseadd.jsp

    代码如下:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>入库信息录入</title>
     8 </head>
     9 <body>
    10 <%
    11          Object message = request.getAttribute("message");
    12          if(message!=null && !"".equals(message)){
    13      
    14     %>
    15          <script type="text/javascript">
    16               alert("<%=request.getAttribute("message")%>");
    17          </script>
    18     <%} %>
    19     <div align="center">
    20         <h1 style="color: black;">商品信息录入</h1>
    21         <a href="main.jsp">返回主页</a>
    22         <form action="${pageContext.request.contextPath}/HouseServlet?method=add" method="post" onsubmit="return check()">
    23             <div class="a">
    24                 商品名称<input type="text" id="name" name="name"/>
    25             </div>
    26             <div class="a">
    27                 生产厂家<input type="text" id="product" name="product" />
    28             </div>
    29             <div class="a">
    30                 商品型号<input type="text" id="type" name="type" />
    31             </div>
    32             <div class="a">
    33                 商品规格<input type="text" id="guige" name="guige" />
    34             </div>
    35             <div class="a">
    36                 商品数量<input type="text" id="number" name="number" />
    37             </div>
    38             <div class="a">
    39                 入库单位<input type="text" id="place" name="place" />
    40             </div>
    41             <div class="a">
    42                 送货人姓名<input type="text" id="person" name="person" />
    43             </div>
    44         
    45             <div class="a">
    46                 <button type="submit" class="b">保&nbsp;&nbsp;&nbsp;存</button>
    47             </div>
    48         </form>
    49     </div>
    50     <script type="text/javascript">
    51         function check() {
    52             var name = document.getElementById("name");;
    53             var product = document.getElementById("product");
    54             var type = document.getElementById("type");
    55             var guige = document.getElementById("guige");
    56             var number = document.getElementById("number");
    57             
    58             var place = document.getElementById("place");
    59             var person = document.getElementById("person");
    60             
    61             
    62             //非空 
    63             if(name.value == '') {
    64                 alert('商品名称为空');
    65                 name.focus();
    66                 return false;
    67             }
    68             if(product.value == '') {
    69                 alert('生产厂家为空');
    70                 product.focus();
    71                 return false;
    72             }
    73             if(type.value == '') {
    74                 alert('商品型号为空');
    75                 type.focus();
    76                 return false;
    77             }if(guige.value == '') {
    78                 alert('商品规格');
    79                 guige.focus();
    80                 return false;
    81             }if(number.value == '') {
    82                 alert('商品数量为空');
    83                 number.focus();
    84                 return false;
    85             }if(place.value == '') {
    86                 alert('商品入库单位为空');
    87                 place.focus();
    88                 return false;
    89             }if(person.value == '') {
    90                 alert('送货人为空');
    91                 person.focus();
    92                 return false;
    93             }
    94         }
    95             </script>
    96 </body>
    97 </html>

    对于仓库信息的查询:housefind.jsp:路径 /house/WebContent/housefind.jsp

    代码如下:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 <%
    11          Object message = request.getAttribute("message");
    12          if(message!=null && !"".equals(message)){
    13      
    14     %>
    15          <script type="text/javascript">
    16               alert("<%=request.getAttribute("message")%>");
    17          </script>
    18     <%} %>
    19 <div align="center">
    20         <h1 style="color: black;">仓库信息查询</h1>
    21         <a href="main.jsp">返回主页</a>
    22         <form action="${pageContext.request.contextPath}/HouseServlet?method=find" method="post" onsubmit="return check()">
    23             <div class="a">
    24                 商品名称<input type="text" id="name" name="name"/>
    25             </div>
    26             <div class="a">
    27                 <button type="submit" class="b">保&nbsp;&nbsp;&nbsp;存</button>
    28             </div>
    29         </form>
    30     </div>
    31     <script type="text/javascript">
    32         function check() {
    33             var name = document.getElementById("name");;
    34             
    35             
    36             //非空
    37             if(name.value == "") {
    38                 alert('请填写条件');
    39                 return false;
    40             }
    41         }
    42     </script>
    43 </body>
    44 </html>

    将查询结果进行输出:houseRseult:路径 /house/WebContent/houseResult.jsp

    代码如下:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3      <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     8 <title>查询结果</title>
     9 </head>
    10 <body>
    11 <div align="center">
    12         <h1 style="color: black;">课程信息列表</h1>
    13         <a href="main.jsp">返回主页</a>
    14         <table class="tb">
    15             <tr>
    16                 
    17                 <td>商品名称</td>
    18                 <td>生产厂家</td>
    19                 <td>商品型号</td>
    20                 <td>商品规格</td>
    21                 <td>数量</td>
    22                 <td>时间</td>
    23                 <td>入库单位</td>
    24                 <td>送货人</td>
    25             </tr>
    26             <!-- forEach遍历出adminBeans -->
    27             <c:forEach items="${list}" var="item" varStatus="status">
    28                 <tr>
    29                 
    30                     <td><a>${item.name}</a></td>
    31                     <td>${item.product}</td>
    32                     <td>${item.type}</td>
    33                     <td>${item.guige}</td>
    34                     <td>${item.number}</td>
    35                     <td>${item.date }</td>
    36                     <td>${item.place }</td>
    37                     <td>${item.person }</td>
    38                 </tr>
    39             </c:forEach>
    40         </table>
    41     </div>
    42 </body>
    43 </html>
  • 相关阅读:
    ESP8266-12F引脚接法
    esp8266物联网开发六:让ESP32-CAM五彩斑斓
    esp8266物联网开发五:SSL保驾护航
    esp8266物联网开发四:MQTT再论部控
    esp8266物联网开发三:MQTT初窥貌容
    esp8266物联网开发二:Arduino名门正派
    esp8266物联网开发一:MicroPython初战江湖
    一些错误记录
    jimdb压测踩坑记
    Caffeine批量加载浅析
  • 原文地址:https://www.cnblogs.com/1gaoyu/p/10116935.html
Copyright © 2011-2022 走看看