1. 程式人生 > >倉庫資訊管理系統

倉庫資訊管理系統

資料庫裡需要建兩個表,一個存貨物資料實現增刪改查,另一個記錄出庫入庫。主體還是jsp加servlet。

package dao;

import entity.Course;
import util.DBUtil;

/**
 * 課程Dao
 * Dao層操作資料
 * @author Hu
 *
 */
public class CourseDao {

    /**
     * 新增
     * @param course
     * @return
     */
    public boolean add(Course course) {
        String sql 
= "insert into danju(name,changjia,xinghao,guige,shuliang,riqi,shijian,danwei,renming) values('" + course.getName() + "','" + course.getChangjia() + "','" + course.getXinghao() + "','" + course.getGuige() + "','" + course.getShuliang() + "','" + course.getRiqi() + "','" + course.getShijian() + "','"
+ course.getDanwei() + "','" + course.getRenming() + "')"; Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); state.executeUpdate(sql); } catch
(Exception e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 刪除 * * @param id * @return */ public boolean delete (int id) { boolean f = false; String sql = "delete from danju where id='" + id + "'"; Connection conn = DBUtil.getConn(); Statement state = null; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 修改 * @param name * @param pass */ public boolean update(Course course) { String sql = "update danju set name='" + course.getName() + "', changjia='" + course.getChangjia() + "', xinghao='" + course.getXinghao() + "', guige='" + course.getGuige() + "', shuliang='" + course.getShuliang() + "', riqi='" + course.getRiqi() + "', shijian='" + course.getShijian() + "', danwei='" + course.getDanwei() + "', renming='" + course.getRenming() + "' where id='" + course.getId() + "'"; Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 驗證課程名稱是否唯一 * true --- 不唯一 * @param name * @return */ public boolean name(String name) { boolean flag = false; String sql = "select name from danju where name = '" + name + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { flag = true; } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return flag; } /** * 通過ID得到類 * @param id * @return */ public Course getCourseById(int id) { String sql = "select * from danju where id ='" + id + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; Course course = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String name = rs.getString("name"); String changjia = rs.getString("changjia"); String xinghao = rs.getString("xinghao"); String guige = rs.getString("guige"); String shuliang = rs.getString("shuliang"); String riqi = rs.getString("riqi"); String shijian = rs.getString("shijian"); String danwei = rs.getString("danwei"); String renming = rs.getString("renming"); course = new Course(id,name,changjia,xinghao,guige,shuliang,riqi,shijian,danwei,renming); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return course; } /** * 通過name得到Course * @param name * @return */ public Course getCourseByName(String name) { String sql = "select * from danju where name ='" + name + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; Course course = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { int id = rs.getInt("id"); String changjia = rs.getString("changjia"); String xinghao = rs.getString("xinghao"); String guige = rs.getString("guige"); String shuliang = rs.getString("shuliang"); String riqi = rs.getString("riqi"); String shijian = rs.getString("shijian"); String danwei = rs.getString("shijian"); String renming = rs.getString("renming"); course = new Course(id,name,changjia,xinghao,guige,shuliang,riqi,shijian,danwei,renming); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return course; } /** * 查詢 * @param name * @param teacher * @param classroom * @return */ public List<Course> search(String name, String riqi) { String sql = "select * from danju where 1=1 "; if (name != "") { sql += "and name like '%" + name + "%'"; } if (riqi != "") { sql += "and teacher like '%" + riqi + "%'"; } List<Course> list = new ArrayList<>(); Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); Course bean = null; while (rs.next()) { int id = rs.getInt("id"); String name2 = rs.getString("name"); String changjia2 = rs.getString("changjia"); String xinghao2 = rs.getString("xinghao"); String guige2 = rs.getString("guige"); String shuliang2 = rs.getString("shuliang"); String riqi2 = rs.getString("riqi"); String shijian2 = rs.getString("shijian"); String danwei2 = rs.getString("danwei"); String renming2 = rs.getString("renming"); bean = new Course(id, name2, changjia2, xinghao2,guige2,shuliang2,riqi2,shijian2,danwei2,renming2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } /** * 全部資料 * @param name * @param teacher * @param classroom * @return */ public List<Course> list() { String sql = "select * from danju"; List<Course> list = new ArrayList<>(); Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); Course bean = null; while (rs.next()) { int id = rs.getInt("id"); String name2 = rs.getString("name"); String changjia2 = rs.getString("changjia"); String xinghao2 = rs.getString("xinghao"); String guige2 = rs.getString("guige"); String shuliang2 = rs.getString("shuliang"); String riqi2 = rs.getString("riqi"); String shijian2 = rs.getString("shijian"); String danwei2 = rs.getString("danwei"); String renming2 = rs.getString("renming"); bean = new Course(id, name2, changjia2, xinghao2,guige2,shuliang2,riqi2,shijian2,danwei2,renming2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } } package entity; public class Course { private int id; private String name; private String changjia; private String xinghao; private String guige; private String shuliang; private String riqi; private String shijian; private String danwei; private String renming; 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 getXinghao() { return xinghao; } public void setXinghao(String classroom) { this.xinghao = 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 getRenming() { return renming; } public void setRenming(String renming) { this.renming = renming; } public Course() {} public Course(int id, String name, String changjia, String xinghao, String guige, String shuliang, String riqi, String shijian, String danwei, String renming) { this.id = id; this.name = name; this.changjia = changjia; this.xinghao = xinghao; this.guige = guige; this.shuliang = shuliang; this.riqi = riqi; this.shijian = shijian; this.danwei = danwei; this.renming = renming; } public Course(String name, String changjia, String xinghao, String guige, String shuliang, String riqi, String shijian, String danwei, String renming) { this.name = name; this.changjia = changjia; this.xinghao = xinghao; this.guige = guige; this.shuliang = shuliang; this.riqi = riqi; this.shijian = shijian; this.danwei = danwei; this.renming = renming; } } 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(); } } 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 xinghao = req.getParameter("xinghao"); 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 renming = req.getParameter("renming"); Course course = new Course(name,changjia,xinghao,guige,shuliang,riqi,shijian,danwei,renming); //新增後訊息顯示 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 xinghao = req.getParameter("xinghao"); 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 renming = req.getParameter("renming"); Course course = new Course(id, name,changjia,xinghao,guige,shuliang,riqi,shijian,danwei,renming); 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); } } package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 資料庫連線工具 * @author Hu * */ public class DBUtil { public static String db_url = "jdbc:mysql://localhost:3306/kucun?useSSL=false"; public static String db_user = "root"; public static String db_pass = "root"; 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(); } } } } <%@ 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>Insert title here</title> </head> <body> <h2>請選擇查詢方式</h2> <a href=shangpin.jsp>按照商品名稱查詢</a> <a href=riqi.jsp>按照出入庫日期查詢</a> </body> </html> <%@ 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>Insert title here</title> </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="main.jsp">返回主頁</a> <form action="servlet?method=chu" method="post"> <div class="a"> 商品<input type="text" name="a"/> </div> <div class="a"> 廠家<input type="text" name="b" /> </div> <div class="a"> 型號<input type="text" name="c" /> </div> <div class="a"> 規格<input type="text" name="d" /> </div> <div class="a"> 數量<input type="text" name="e" /> </div> <div class="a"> 日期<input type="text" name="f" /> </div> <div class="a"> 時間<input type="text" name="g" /> </div> <div class="a"> 單位<input type="text" name="h" /> </div> <div class="a"> 姓名<input type="text" name="i" /> </div> <div class="a"> <button type="submit" class="b">保&nbsp;&nbsp;&nbsp;存</button> </div> </form> </div> </body> </html> <%@ 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>Insert title here</title> </head> <body> <h2>請選擇出庫,入庫或查詢記錄</h2> <a href=chu.jsp>出庫</a> <a href=ru.jsp>入庫</a> <a href=cha.jsp>查詢</a> <a href=gai.jsp>修改</a> </body> </html> <%@ 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>Insert title here</title> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <h2>請輸入出入庫日期進行查詢</h2> <form action="servlet?method=cha2" method="post" > <div class="a"> 日期:<input type="text" name="a"/> </div> <div class="a"> <button type="submit" class="b">查&nbsp;&nbsp;&nbsp;詢</button> </div> </form> </body> </html> <%@ 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>Insert title here</title> </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="main.jsp">返回主頁</a> <form action="servlet?method=chu" method="post" onsubmit="return check()"> <div class="a"> 商品<input type="text" name="a"/> </div> <div class="a"> 廠家<input type="text" name="b" /> </div> <div class="a"> 型號<input type="text" name="c" /> </div> <div class="a"> 規格<input type="text" name="d" /> </div> <div class="a"> 數量<input type="text" name="e" /> </div> <div class="a"> 日期<input type="text" name="f" /> </div> <div class="a"> 時間<input type="text" name="g" /> </div> <div class="a"> 單位<input type="text" name="h" /> </div> <div class="a"> 姓名<input type="text" name="i" /> </div> <div class="a"> <button type="submit" class="b">保&nbsp;&nbsp;&nbsp;存</button> </div> </form> </div> </body> </html> <%@ 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>Insert title here</title> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <h2>請輸入商品名稱進行查詢</h2> <form action="servlet?method=cha1" method="post"> <div class="a"> 商品名稱:<input type="text" name="a"/> </div> <div class="a"> <button type="submit" class="b">查&nbsp;&nbsp;&nbsp;詢</button> </div> </form> </body> </html><%@ 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>Insert title here</title> </head> <body> <h3>出入庫資訊</h3> <form> 商品:${x.a}<br> 廠家:${x.b}<br> 型號:${x.c}<br> 規格:${x.d}<br> 數量:${x.e}<br> 日期:${x.f}<br> 時間:${x.g}<br> 單位:${x.h}<br> 姓名:${x.i}<br> 出庫與入庫:${x.type}<br> </form> </body> </html>