庫存物資管理系統javascript
1.首先先創建一個名爲course的數據庫,在創建一個course名的表,而後添加幾個字段html
項目目錄結構java
2.源代碼mysql
先創建一個util的包,建立一個DBUtil的類,這是肯定的,DBUtil.javaweb
package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 數據庫鏈接工具 * @author zlj * */ public class DBUtil { public static String db_url = "jdbc:mysql://localhost:3306/course?useSSL=false"; public static String db_user = "root"; public static String db_pass = "199126"; public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver");//加載驅動 conn = DriverManager.getConnection(db_url, db_user, db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; } /** * 關閉鏈接 * @param state * @param conn */ public static void close (Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
而後添加建立一個course的類,定義變量。course.javasql
package entity; public class Course { private int id; private String name; private String changjia; private String type; private String guige; private String shuliang; private String riqi; private String shijian; private String danwei; private String people; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getChangjia() { return changjia; } public void setChangjia(String teacher) { this.changjia = teacher; } public String gettype() { return type; } public void settype(String classroom) { this.type = classroom; } public String getGuige() { return guige; } public void setGuige(String guige) { this.guige = guige; } public String getShuliang() { return shuliang; } public void setShuliang(String shuliang) { this.shuliang = shuliang; } public String getRiqi() { return riqi; } public void setRiqi(String riqi) { this.riqi = riqi; } public String getShijian() { return shijian; } public void setShijian(String shijian) { this.shijian = shijian; } public String getDanwei() { return danwei; } public void setDanwei(String danwei) { this.danwei = danwei; } public String getpeople() { return people; } public void setpeople(String people) { this.people = people; } public Course() {} public Course(int id, String name, String changjia, String type, String guige, String shuliang, String riqi, String shijian, String danwei, String people) { this.id = id; this.name = name; this.changjia = changjia; this.type = type; this.guige = guige; this.shuliang = shuliang; this.riqi = riqi; this.shijian = shijian; this.danwei = danwei; this.people = people; } public Course(String name, String changjia, String type, String guige, String shuliang, String riqi, String shijian, String danwei, String people) { this.name = name; this.changjia = changjia; this.type = type; this.guige = guige; this.shuliang = shuliang; this.riqi = riqi; this.shijian = shijian; this.danwei = danwei; this.people = people; } }
而後創建一個Dao層,來封裝數據,實現增刪改查,courseDao.java數據庫
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } .tb, td { border: 1px solid black; font-size: 22px; } </style> </head> <body> <div align="center"> <h1 style="color: red;">庫存物資信息刪除</h1> <a href="index.jsp">返回主頁</a> <table class="tb"> <tr> <td>商品名稱</td> <td>${course.name}</td> </tr> <tr> <td>廠家</td> <td>${course.changjia}</td> </tr> <tr> <td>型號</td> <td>${course.type}</td> </tr> <tr> <td>規格</td> <td>${course.guige}</td> </tr> <tr> <td>數量</td> <td>${course.shuliang}</td> </tr> <tr> <td>日期</td> <td>${course.riqi}</td> </tr> <tr> <td>時間</td> <td>${course.shijian}</td> </tr> <tr> <td>單位</td> <td>${course.danwei}</td> </tr> <tr> <td>收送貨人姓名</td> <td>${course.people}</td> </tr> </table> <div class="a"> <a onclick="return check()" href="CourseServlet?method=del&id=${course.id}">刪 除</a> </div> </div> <script type="text/javascript"> function check() { if (confirm("真的要刪除嗎?")){ return true; }else{ return false; } } </script> </body> </html>
在建立一個se'rvice的包,courseservice.javajsp
package service; import java.util.List; import dao.CourseDao; import entity.Course; /** * CourseService * 服務層 * @author Hu * */ public class CourseService { CourseDao cDao = new CourseDao(); /** * 添加 * @param course * @return */ public boolean add(Course course) { boolean f = false; if(!cDao.name(course.getName())) { cDao.add(course); f = true; } return f; } /** * 刪除 */ public void del(int id) { cDao.delete(id); } /** * 修改 * @return */ public void update(Course course) { cDao.update(course); } /** * 經過ID獲得一個Course * @return */ public Course getCourseById(int id) { return cDao.getCourseById(id); } /** * 經過Name獲得一個Course * @return */ public Course getCourseByName(String name) { return cDao.getCourseByName(name); } /** * 查找 * @return */ public List<Course> search(String name, String riqi) { return cDao.search(name,riqi); } /** * 所有數據 * @return */ public List<Course> list() { return cDao.list(); } }
最後建立一個servlet的包 courseservlet.java工具
package servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import entity.Course; import service.CourseService; @WebServlet("/CourseServlet") public class CourseServlet extends HttpServlet { private static final long serialVersionUID = 1L; CourseService service = new CourseService(); /** * 方法選擇 */ protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("add".equals(method)) { add(req, resp); } else if ("del".equals(method)) { del(req, resp); } else if ("update".equals(method)) { update(req, resp); } else if ("search".equals(method)) { search(req, resp); } else if ("getcoursebyid".equals(method)) { getCourseById(req, resp); } else if ("getcoursebyname".equals(method)) { getCourseByName(req, resp); } else if ("list".equals(method)) { list(req, resp); } } /** * 添加 * @param req * @param resp * @throws IOException * @throws ServletException */ private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); String changjia = req.getParameter("changjia"); String type = req.getParameter("type"); String guige = req.getParameter("guige"); String shuliang = req.getParameter("shuliang"); String riqi =req.getParameter("riqi"); String shijian = req.getParameter("shijian"); String danwei = req.getParameter("danwei"); String people = req.getParameter("people"); Course course = new Course(name,changjia,type,guige,shuliang,riqi,shijian,danwei,people); //添加後消息顯示 if(service.add(course)) { req.setAttribute("message", "添加成功"); req.getRequestDispatcher("add.jsp").forward(req,resp); } else { req.setAttribute("message", "貨物名稱重複,請從新錄入"); req.getRequestDispatcher("add.jsp").forward(req,resp); } } /** * 所有 * @param req * @param resp * @throws ServletException */ private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); List<Course> courses = service.list(); req.setAttribute("courses", courses); req.getRequestDispatcher("list.jsp").forward(req,resp); } /** * 經過ID獲得Course * @param req * @param resp * @throws ServletException */ private void getCourseById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); Course course = service.getCourseById(id); req.setAttribute("course", course); req.getRequestDispatcher("detail2.jsp").forward(req,resp); } /** * 經過名字查找 * 跳轉至刪除 * @param req * @param resp * @throws IOException * @throws ServletException */ private void getCourseByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); Course course = service.getCourseByName(name); if(course == null) { req.setAttribute("message", "查無此貨物!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } else { req.setAttribute("course", course); req.getRequestDispatcher("detail.jsp").forward(req,resp); } } /** * 刪除 * @param req * @param resp * @throws IOException * @throws ServletException */ private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); service.del(id); req.setAttribute("message", "刪除成功!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } /** * 修改 * @param req * @param resp * @throws IOException * @throws ServletException */ private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); String name = req.getParameter("name"); String changjia = req.getParameter("changjia"); String type = req.getParameter("type"); String guige = req.getParameter("guige"); String shuliang = req.getParameter("shuliang"); String riqi =req.getParameter("riqi"); String shijian = req.getParameter("shijian"); String danwei = req.getParameter("danwei"); String people = req.getParameter("people"); Course course = new Course(id, name,changjia,type,guige,shuliang,riqi,shijian,danwei,people); service.update(course); req.setAttribute("message", "修改爲功"); req.getRequestDispatcher("CourseServlet?method=list").forward(req,resp); } /** * 查找 * @param req * @param resp * @throws ServletException */ private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); String riqi = req.getParameter("riqi"); List<Course> courses = service.search(name, riqi); req.setAttribute("courses", courses); req.getRequestDispatcher("searchlist.jsp").forward(req,resp); } }
而後對於jsp的的源代碼post
添加 add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } </style> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: red;">庫存物資信息錄入</h1> <a href="index.jsp">返回主頁</a> <form action="CourseServlet?method=add" method="post" onsubmit="return check()"> <div class="a"> 貨物名稱<input type="text" id="name" name="name"/> </div> <div class="a"> 生產廠家<input type="text" id="changjia" name="changjia" /> </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"> 數量 <input type="text" id="shuliang" name="shuliang"/> </div> <div class="a"> 日期 <input type="text" id="riqi" name="riqi"/> </div> <div class="a"> 時間 <input type="text" id="shijian" name="shijian"/> </div> <div class="a"> 單位 <input type="text" id="danwei" name="danwei"/> </div> <div class="a"> 人姓名<input type="text" id="people" name="people"/> </div> <div class="a"> <button type="submit" class="b">錄 入</button> </div> </form> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name");; var changjia = document.getElementById("changjia"); var type = document.getElementById("type"); var guige = document.getElementById("guige");; var shuliang = document.getElementById("shuliang"); var riqi = document.getElementById("riqi"); var shijian = document.getElementById("shijian");; var danwei = document.getElementById("danwei"); var people = document.getElementById("people"); //非空 if(name.value == '') { alert('貨物名稱爲空'); name.focus(); return false; } if(changjia.value == '') { alert('廠家爲空'); changjia.focus(); return false; } if(type.value == '') { alert('型號爲空'); type.focus(); return false; } if(guige.value == '') { alert('規格爲空'); guige.focus(); return false; } if(shuliang.value == '') { alert('數量爲空'); shuliang.focus(); return false; } if(riqi.value == '') { alert('日期爲空'); riqi.focus(); return false; } if(shijian.value == '') { alert('時間爲空'); shijian.focus(); return false; } if(danwei.value == '') { alert('單位爲空'); danwei.focus(); return false; } if(people.value == '') { alert('收送貨人姓名爲空'); people.focus(); return false; } } </script> </body> </html>
刪除 del.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } </style> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: red;">庫存物資信息刪除</h1> <a href="index.jsp">返回主頁</a> <form action="CourseServlet?method=getcoursebyname" method="post" onsubmit="return check()"> <div class="a"> 貨物名稱<input type="text" id="name" name="name"/> </div> <div class="a"> <button type="submit" class="b">查 找</button> </div> </form> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name");; //非空 if(name.value == '') { alert('商品名稱爲空'); name.focus(); return false; } } </script> </body> </html>
detail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } .tb, td { border: 1px solid black; font-size: 22px; } </style> </head> <body> <div align="center"> <h1 style="color: red;">庫存物資信息刪除</h1> <a href="index.jsp">返回主頁</a> <table class="tb"> <tr> <td>商品名稱</td> <td>${course.name}</td> </tr> <tr> <td>廠家</td> <td>${course.changjia}</td> </tr> <tr> <td>型號</td> <td>${course.type}</td> </tr> <tr> <td>規格</td> <td>${course.guige}</td> </tr> <tr> <td>數量</td> <td>${course.shuliang}</td> </tr> <tr> <td>日期</td> <td>${course.riqi}</td> </tr> <tr> <td>時間</td> <td>${course.shijian}</td> </tr> <tr> <td>單位</td> <td>${course.danwei}</td> </tr> <tr> <td>收送貨人姓名</td> <td>${course.people}</td> </tr> </table> <div class="a"> <a onclick="return check()" href="CourseServlet?method=del&id=${course.id}">刪 除</a> </div> </div> <script type="text/javascript"> function check() { if (confirm("真的要刪除嗎?")){ return true; }else{ return false; } } </script> </body> </html>
修改 detail2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } </style> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: red;">庫存物資信息修改</h1> <a href="index.jsp">返回主頁</a> <form action="CourseServlet?method=update" method="post" onsubmit="return check()"> <div class="a"> 貨物名稱<input type="text" id="name" name="name" value="${course.name}"/> </div> <div class="a"> 廠家<input type="text" id="changjia" name="changjia" value="${course.changjia}"/> </div> <div class="a"> 型號<input type="text" id="type" name="type" value="${course.type}"/> </div> <div class="a"> 規格<input type="text" id="guige" name="guige" value="${course.guige}"/> </div> <div class="a"> 數量<input type="text" id="shuliang" name="shuliang" value="${course.shuliang}"/> </div> <div class="a"> 日期<input type="text" id="riqi" name="riqi" value="${course.riqi}"/> </div> <div class="a"> 時間<input type="text" id="shijian" name="shijian" value="${course.shijian}"/> </div> <div class="a"> 單位<input type="text" id="danwei" name="danwei" value="${course.danwei}"/> </div> <div class="a"> 人姓名<input type="text" id="people" name="people" value="${course.people}"/> </div> <input type="hidden" id="id" name="id" value="${course.id}"/> <div class="a"> <button type="submit" class="b">修 改</button> </div> </form> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name");; var changjia = document.getElementById("changjia"); var type = document.getElementById("type"); var guige = document.getElementById("guige");; var shuliang = document.getElementById("shuliang"); var riqi = document.getElementById("riqi"); var shijian = document.getElementById("shijian");; var danwei = document.getElementById("danwei"); var people = document.getElementById("people"); //非空 if(name.value == '') { alert('商品名稱爲空'); name.focus(); return false; } if(changjia.value == '') { alert('廠家爲空'); changjia.focus(); return false; } if(type.value == '') { alert('型號爲空'); type.focus(); return false; } if(guige.value == '') { alert('規格爲空'); guige.focus(); return false; } if(shuliang.value == '') { alert('數量爲空'); shuliang.focus(); return false; } if(riqi.value == '') { alert('日期爲空'); riqi.focus(); return false; } if(shijian.value == '') { alert('時間爲空'); shijian.focus(); return false; } if(danwei.value == '') { alert('單位爲空'); danwei.focus(); return false; } if(people.value == '') { alert('人姓名爲空'); people.focus(); return false; } } </script> </body> </html>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>首頁</title> <style> .a{ font-size: 26px; margin-top: 20px; } </style> </head> <body> <div align="center"> <h1 style="color: red;">庫存物資管理系統</h1> <div class="a"> <a href="add.jsp">庫存物資信息錄入</a> </div> <div class="a"> <a href="CourseServlet?method=list">庫存物資信息修改</a> </div> <div class="a"> <a href="del.jsp">庫存物資信息刪除</a> </div> <div class="a"> <a href="search.jsp">庫存物資信息查詢</a> </div> </div> </body> </html>
list.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> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } .tb, td { border: 1px solid black; font-size: 22px; } </style> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: red;">庫存物資列表</h1> <a href="index.jsp">返回主頁</a> <table class="tb"> <tr> <td>id</td> <td>商品名稱</td> <td>廠家</td> <td>型號</td> <td>規格</td> <td>數量</td> <td>日期</td> <td>時間</td> <td>單位</td> <td>人姓名</td> <td align="center" colspan="2">操做</td> </tr> <c:forEach items="${courses}" var="item"> <tr> <td>${item.id}</td> <td>${item.name}</td> <td>${item.changjia}</td> <td>${item.type}</td> <td>${item.guige}</td> <td>${item.shuliang}</td> <td>${item.riqi}</td> <td>${item.shijian}</td> <td>${item.danwei}</td> <td>${item.people}</td> <td><a href="CourseServlet?method=getcoursebyid&id=${item.id}">修改</a></td> </tr> </c:forEach> </table> </div> </body> </html>
查詢
search.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } </style> </head> <body> <div align="center"> <h1 style="color: red;">庫存物資信息查詢</h1> <a href="index.jsp">返回主頁</a> <form action="CourseServlet?method=search" method="post" onsubmit="return check()"> <div class="a"> 商品名稱<input type="text" id="name" name="name"/> </div> <div class="a"> 型號<input type="text" id="type" name="riqi" /> </div> <div class="a"> <button type="submit" class="b">查 詢</button> </div> </form> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name");; var riqi = document.getElementById("type"); //非空 if(name.value == '' && riqi.value == '') { alert('請填寫一個條件'); return false; } } </script> </body> </html>
查詢列表searchlist.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> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } .tb, td { border: 1px solid black; font-size: 22px; } </style> </head> <body> <div align="center"> <h1 style="color: red;">庫存物資信息列表</h1> <a href="index.jsp">返回主頁</a> <table class="tb"> <tr> <td>id</td> <td>商品名稱</td> <td> 廠家</td> <td> 型號 </td> <td> 規格 </td> <td> 數量 </td> <td> 日期 </td> <td> 時間 </td> <td> 單位 </td> <td>人姓名</td> </tr> <!-- forEach遍歷出adminBeans --> <c:forEach items="${courses}" var="item" varStatus="status"> <tr> <td>${item.id}</td> <td><a>${item.name}</a></td> <td>${item.changjia}</td> <td>${item.type}</td> <td><a>${item.guige}</a></td> <td>${item.shuliang}</td> <td>${item.riqi}</td> <td><a>${item.shijian}</a></td> <td>${item.danwei}</td> <td>${item.people}</td> </tr> </c:forEach> </table> </div> </body> </html>
添加
添加前
添加後
刪除
刪除後
查詢
總結:
對於這個庫存物資管理系統,整體來講,比上週有一些的進步,此次是獨自作出來的,雖然是仿造上週學長給的模板來作的,可是好歹作出來了,又一些瑕疵,好比對於出入庫的時間人物,我以爲應該用兩個表,分別入庫和出庫,可是不怎麼會作,因此只製做了一個表。經過今天的作這個庫存物資管理系統,也懂了一些關於java web的知識,學會了如何Java鏈接數據庫,但對於增刪改查的具體操做,仍是不怎麼了解,還有些茫然,可是我會在之後的課餘時間,來學習它,爭取把它搞明白,最後獨自來作一個系統。