倉庫物資管理
總結:在這次的測驗中看清楚題目的要求,沒有分析好專案製作的調理,導致在做的時候比較盲目,所有在製作的後期比較慌亂。並且在此次實驗中還犯了一個比較致命的錯誤,就是在進行資料庫的操作過程中運用了和資料庫相近的字元,導致新增和修改操作都無法進行,還有就是在資料的型別定義的時候不夠認真,在介面中的字元與資料表中的的型別不符合,也導致無法對資料庫進行操作,導致無法進行相關操作,所以浪費了大量的時間。
這是在這次專案中所建的檔案:這個專案中運用了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">保 存</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">查 找</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 width: 160px; 17 color: white; 18 background-color: greenyellow; 19 } 20 .tb, td { 21