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

倉庫管理系統

  建立專案、配置環境等操作詳見我的另一篇部落格。  

  思路:先建立商品的資料表,完成商品資料庫的增刪,在建立表單的資料庫,編寫增刪改查,在出庫和入庫的時候分別呼叫相應的商品增刪函式。

  遇到的問題:在表單的增刪時,遇到了無法儲存的操作,最後改了表單名,完成了操作,在遍歷輸出資料庫內容時,運用了list容器的方法。遍歷輸出list中的內容。

<%List<Danju> l = (List<Danju>)session.getAttribute("list");%>
    <%for(int i=0;i<l.size();i++){
%> <tr> <td><%=l.get(i).getCaozuo()%></td> <td><%=l.get(i).getName()%></td> <td><%=l.get(i).getChangjia()%></td> <td><%=l.get(i).getXinghao()%></td> <td><%=l.get(i).getGuige()%></td>
<td><%=l.get(i).getNum()%></td> <td><%=l.get(i).getDate()%></td> <td><%=l.get(i).getTime()%></td> <td><%=l.get(i).getDanwei()%></td> <td><%=l.get(i).getPeople()%></td> <%}%>

程式的實現步驟:

開始的資料庫內容:商品資料庫

表單資料庫:

程式介面:

新增商品

刪除商品:

 

 

 新增入庫表單

 

 

出庫操作:

顯示全部表單

 

查詢表單資訊:

 

 相關原始碼:

Danju.java:

package com.Warehouse;

public class Danju {
    private String caozuo;
    private String name;
    private String changjia;
    private String xinghao;
    private String guige;
    private String num;
    private String date;
    private String time;
    private String danwei;
    private String people;
    public Danju(String cao,String na,String chang,String xing,String gui,String num,String date,String time,String dan,String people) {
        this.caozuo=cao;
        this.name=na;
        this.changjia=chang;
        this.xinghao=xing;
        this.guige=gui;
        this.num=num;
        this.date=date;
        this.time=time;
        this.danwei=dan;
        this.people=people;
    }
    
    public String getCaozuo() {
        return caozuo;
    }

    public void setCaozuo(String caozuo) {
        this.caozuo = caozuo;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getChangjia() {
        return changjia;
    }
    public void setChangjia(String changjia) {
        this.changjia = changjia;
    }
    public String getXinghao() {
        return xinghao;
    }
    public void setXinghao(String xinghao) {
        this.xinghao = xinghao;
    }
    public String getGuige() {
        return guige;
    }
    public void setGuige(String guige) {
        this.guige = guige;
    }
    public String getNum() {
        return num;
    }
    public void setNum(String num) {
        this.num = num;
    }
    public String getTime() {
        return time;
    }
    public void setTime(String time) {
        this.time = time;
    }
    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;
    }
    
}

Warehouse.java:

package com.Warehouse;

public class Warehouse {
    private String name;
    private String Manufacturer;
    private String xinghao;
    private String guige;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getManufacturer() {
        return Manufacturer;
    }
    public void setManufacturer(String manufacturer) {
        Manufacturer = manufacturer;
    }
    public String getXinghao() {
        return xinghao;
    }
    public void setXinghao(String xinghao) {
        this.xinghao = xinghao;
    }
    public String getGuige() {
        return guige;
    }
    public void setGuige(String guige) {
        this.guige = guige;
    }
    public Warehouse(String na,String Manu,String xin,String gui) {
        this.name=na;
        this.Manufacturer=Manu;
        this.xinghao=xin;
        this.guige=gui;
        
    }

}

DBUtil.java:

package com.DBUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil {
    
    public static String db_url = "jdbc:mysql://localhost:3306/warehouse?useSSL=false&serverTimezone=GMT";
    public static String db_user = "root";
    public static String db_pass = "1234567";
    
    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();
            }
        }
    }

}

Serlvet:

package com.serlvet;

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 javax.servlet.http.HttpSession;

import com.Warehouse.Danju;
import com.Warehouse.Warehouse;
import com.daobao.Dao;

/**
 * Servlet implementation class Serlvet
 */
@WebServlet("/Serlvet")
public class Serlvet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    Dao service=new Dao();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Serlvet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    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 ("show".equals(method)) {
            show(req, resp);
        } else if ("search".equals(method)) {
            search(req, resp);
        }else if ("inwarehouse".equals(method)) {
            Inwarehouse(req, resp);
        }else if ("outwarehouse".equals(method)) {
            outwarehouse(req, resp);
        }
    }
    private void outwarehouse(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        String str="出庫";
        String name = req.getParameter("name");
        String changjia = req.getParameter("changjia");
        String xinghao = req.getParameter("xinghao");
        String guige = req.getParameter("guige");
        String num = req.getParameter("num");
        String date = req.getParameter("date");
        String time = req.getParameter("time");
        String danwei = req.getParameter("danwei");
        String people = req.getParameter("people");
        Danju course=new Danju(str,name,changjia,xinghao,guige,num,date,time,danwei,people);
        if(service.add2(course)&&service.Dlete(name)) {
            req.setAttribute("message", "出庫成功");
            req.getRequestDispatcher("OutWarehouse.jsp").forward(req,resp);
        } else {
            req.setAttribute("message", "出庫失敗");
            req.getRequestDispatcher("OutWarehouse.jsp").forward(req,resp);
        }
        
    }

    private void Inwarehouse(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String str="入庫";
        String name = req.getParameter("name");
        String changjia = req.getParameter("changjia");
        String xinghao = req.getParameter("xinghao");
        String guige = req.getParameter("guige");
        String num = req.getParameter("num");
        String date = req.getParameter("date");
        String time = req.getParameter("time");
        String danwei = req.getParameter("danwei");
        String people = req.getParameter("people");
        Danju course=new Danju(str,name,changjia,xinghao,guige,num,date,time,danwei,people);
        Warehouse course2 = new Warehouse(name, changjia, xinghao, guige);
        if(service.add2(course)&&service.add(course2)) {
            req.setAttribute("message", "入庫成功");
            req.getRequestDispatcher("InWarehouse.jsp").forward(req,resp);
        } else {
            req.setAttribute("message", "入庫失敗");
            req.getRequestDispatcher("InWarehouse.jsp").forward(req,resp);
        }
    }

    private void add(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        //req.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");
        String manu = req.getParameter("manu");
        String xing = req.getParameter("xing");
        String gui = req.getParameter("gui");
        Warehouse course = new Warehouse(name, manu, xing, gui);
        //新增後訊息顯示
        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);
        }
        
    }
    private void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        if(service.Dlete(id)) {
            req.setAttribute("message", "刪除成功");
            req.getRequestDispatcher("Delte.jsp").forward(req,resp);
        } else {
            req.setAttribute("message", "刪除失敗");
            req.getRequestDispatcher("Delte.jsp").forward(req,resp);
        }
    }
    private void show(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException  {
        req.setCharacterEncoding("utf-8");
        List<Danju> courses = service.list();
        HttpSession session = req.getSession();
        session.setAttribute("list", courses);
        req.getRequestDispatcher("Show.jsp").forward(req,resp);
    }
    private void search(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException  {
        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");
        String date = req.getParameter("date");
        HttpSession session = req.getSession();
        List<Danju> courses = service.search(name,date);
        session.setAttribute("list", courses);
        req.getRequestDispatcher("Show.jsp").forward(req,resp);
    }
    

}

 

 Dao.java:

package com.daobao;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.Warehouse.Danju;
import com.Warehouse.Warehouse;
import com.DBUtil.DBUtil;
public class Dao {
    public boolean add(Warehouse warehouse) {
        String sql ="insert into commodity(名稱, 生產廠家, 型號, 規格)  value('"+warehouse.getName()+"','"+warehouse.getManufacturer()+"','"+warehouse.getXinghao()+"','"+warehouse.getGuige()+"')";
        Connection conn = DBUtil.getConn();
        Statement state=null;
        boolean f=false;
        int a = 0;
        try {
            state =conn.createStatement();
            a = state.executeUpdate(sql);
        }catch(Exception e) {
            
        }finally {
            DBUtil.close(state, conn);
        }
        if(a>0)f=true;
        return f;
    }
    public boolean add2(Danju dan) {
        String sql ="insert into inwarehouse(操作, 名稱, 生產廠家, 型號, 規格, 數量, 日期, 時間, 入(出)庫單位名稱, 送貨人姓名)  value('"+dan.getCaozuo()+"','"+dan.getName()+"','"+dan.getChangjia()+"','"+dan.getXinghao()+"','"+dan.getGuige()+"','"+dan.getNum()+"','"+dan.getDate()+"','"+dan.getTime()+"','"+dan.getDanwei()+"','"+dan.getPeople()+"')";
        Connection conn = DBUtil.getConn();
        Statement state=null;
        boolean f=false;
        int a = 0;
        try {
            state =conn.createStatement();
            a = state.executeUpdate(sql);
        }catch(Exception e) {
            
        }finally {
            DBUtil.close(state, conn);
        }
        if(a>0)f=true;
        return f;
    }
    public boolean Dlete(String id) {
        boolean f = false;
        String sql = "delete from commodity where 名稱='" + 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;
    }
    public List<Danju> list() {
        String sql = "select * from inwarehouse";
        List<Danju> list = new ArrayList<>();
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;

        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            Danju bean = null;
            while (rs.next()) {
                String caozuo = rs.getString("操作");
                String name = rs.getString("名稱");
                String changjia = rs.getString("生產廠家");
                String xinghao = rs.getString("型號");
                String guige = rs.getString("規格");
                String num = rs.getString("數量");
                String date = rs.getString("日期");
                String time = rs.getString("時間");
                String danwei = rs.getString("入(出)庫單位名稱");
                String people = rs.getString("送貨人姓名");
                bean = new Danju(caozuo, name, changjia, xinghao,guige,num,date,time,danwei,people);
                list.add(bean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        
        return list;
    }
    public List<Danju> search(String name, String date) {
        String sql = "select * from inwarehouse where ";
        if (name != "") {
            sql += "名稱= '"+name+"'";
        }
        if (date != "") {
            sql += "日期= '"+date+"'";
        }
        List<Danju> list = new ArrayList<>();
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            Danju bean = null;
            while (rs.next()) {
                String caozuo = rs.getString("操作");
                String name2 = rs.getString("名稱");
                String changjia = rs.getString("生產廠家");
                String xinghao = rs.getString("型號");
                String guige = rs.getString("規格");
                String num = rs.getString("數量");
                String date2 = rs.getString("日期");
                String time2 = rs.getString("時間");
                String danwei = rs.getString("入(出)庫單位名稱");
                String people = rs.getString("送貨人姓名");
                bean = new Danju(caozuo, name2, changjia, xinghao,guige,num,date2,time2,danwei,people);
                list.add(bean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        return list;
    }
}

 

 Warehouse.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>
    h1{text-align:center;text-bgcolor:red; }
    a {    text-decoation:none;}
</style>
</head>
<body>
<h1>倉庫管理系統</h1>
<div id=Layer1 style="position:absolute; ;z-index:1;left: 0px;top:50px;">
<table border="1"  >
<tr bgcolor="#DCDCDC"><td></td></tr>
<tr><td align="center" bgcolor=Aqua>倉庫管理</td></tr>
<tr><td align="center"> <a href="Add.jsp">新增商品</a></td></tr>
<tr><td align="center"> <a href="Delte.jsp">刪除商品</a></td></tr>
<tr><td align="center" bgcolor=Aqua>表單管理</td></tr>
<tr><td align="center"> <a href="InWarehouse.jsp">入庫</a></td></tr>
<tr><td align="center"> <a href="OutWarehouse.jsp">出庫</a></td></tr>
<tr><td align="center"> <a href="Serlvet?method=show">顯示出入庫資訊</a></td></tr>
<tr><td align="center"> <a href="Search.jsp">查詢(入)出庫資訊</a></td></tr>
</table></div>
</body>
</html>

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>
    div{ text-align:center;}
</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">
<a href="Warehouse.jsp">返回首頁</a>
</div>
<form action="Serlvet?method=add" method="post" onsubmit="return check()">
<div >

<table align="center" border="1">
<tr>
    <td align="right">商品名稱</td>
    <td><input type="text" id="name" name="name"/></td>
</tr>
<tr>
    <td align="right">生產廠家</td>
    <td><input type="text" id="manu" name="manu"/></td>
</tr>
<tr>
    <td align="right">型號</td>
    <td><input type="text" id="xing" name="xing"/></td>
</tr>
<tr>
    <td align="right">規格</td>
    <td><input type="text" id="gui" name="gui"/></td>
</tr>
</table>
</div>
<div align="center">
<button type="submit" style="width:160px;color:Write;">&nbsp;&nbsp;&nbsp;</button>
</div>
</form>

<script type="text/javascript">
    function check(){
        var name = document.getElementById("name");
        var manu = document.getElementById("manu");
        var xing = document.getElementById("xing");
        var gui = document.getElementById("gui");
        if(name.value == '') {
            alert('課程名稱為空');
            name.focus();
            return false;
        }
        if(manu.value == '') {
            alert('課程名稱為空');
            name.focus();
            return false;
        }
        if(xing.value == '') {
            alert('課程名稱為空');
            name.focus();
            return false;
        }
        if(gui.value == '') {
            alert('課程名稱為空');
            name.focus();
            return false;
        }
    }
</script>
</body>
</html>

 Delte.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>
</head>
<body>
    <%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
<div align="center">
<div>
<a href="Warehouse.jsp">返回首頁</a>
</div>
<form action="Serlvet?method=del" method="post" onsubmit="return check()">
<table align="center" border="">
<tr>
    <td align="right">要刪除的商品名稱</td>
    <td><input type="text" id="id" name="id"></td>
</tr>
</table>
<button type="submit" class="b">&nbsp;&nbsp;&nbsp;</button>
</form>
<script type="text/javascript">
    function check(){
        var name = document.getElementById("id");
        
        if(name.value == '') {
            alert('課程名稱為空');
            name.focus();
            return false;
        }
    }
</script>
</div>
</body>
</html>

InWerahouse.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>
</head>
<body>
    <%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
<div>
<div>
<a href="Warehouse.jsp">返回首頁</a>
</div>
<form action="Serlvet?method=inwarehouse" method="post" onsubmit="return check()">
<table>
<tr><th align="center">填寫入庫單據</th></tr>
<tr>
    <td align="right">商品名稱</td>
    <td><input type="text" id="name" name="name"></td>
</tr>
<tr>
    <td align="right">生產廠家</td>
    <td><input type="text" id="changjia" name="changjia"></td>
</tr>
<tr>
    <td align="right">型號</td>
    <td><input type="text" id="xinghao" name="xinghao"></td>
</tr>
<tr>
    <td align="right">規格</td>
    <td><input type="text" id="guige" name="guige"></td>
</tr>
<tr>
    <td align="right">數量</td>
    <td><input type="text" id="num" name="num"></td>
</tr>
<tr>
    <td align="right">日期</td>
    <td><input type="text" id="date" name="date"></td>
</tr>
<tr>
    <td align="right">時間</td>
    <td><input type="text" id="time" name="time"></td>
</tr>
<tr>
    <td align="right">入(出)庫單位名稱</td>
    <td><input type="text" id="danwei" name="danwei"></td>
</tr>
<tr>
    <td align="right">送貨(提貨)人名稱</td>
    <td><input type="text" id="people" name="people"></td>
</tr>
</table>
<button type="submit">&nbsp;&nbsp;&nbsp;</button>
</form>
</div>
<script>
    function check(){
        var name = document.getElementById("name");
        var changjia = document.getElementById("changjia");
        var xinghao = document.getElementById("xinghao");
        var guige = document.getElementById("guige");
        var num = document.getElementById("num");
        var date = document.getElementById("date");
        var time = document.getElementById("time");
        var danwei = document.getElementById("danwei");
        var people = document.getElementById("people");
        
        if(name.value == '') {
            alert('商品名稱為空');
            name.focus();
            return false;
        }
        if(changjia.value == '') {
            alert('生產廠家為空');
            name.focus();
            return false;
        }
        if(xinghao.value == '') {
            alert('型號為空');
            name.focus();
            return false;
        }
        if(guige.value == '') {
            alert('規格為空');
            name.focus();
            return false;
        }
        if(num.value == '') {
            alert('數量為空');
            name.focus();
            return false;
        }
        if(date.value == '') {
            alert('日期為空');
            name.focus();
            return false;
        }
        if(time.value == '') {
            alert('時間為空');
            name.focus();
            return false;
        }
        
        if(danwei.value == '') {
            alert('入(出)庫單位名稱為空');
            name.focus();
            return false;
        }
        if(people.value == '') {
            alert('送(提)貨人名稱為空');
            name.focus();
            return false;
        }
    }
</script>
</body>
</html>

OutWerahouse.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>
</head>
<body>
    <%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
<div>
<div>
<a href="Warehouse.jsp">返回首頁</a>
</div>
<form action="Serlvet?method=outwarehouse" method="post" onsubmit="return check()">
<table>
<tr><th align="center">填寫出庫單據</th></tr>
<tr>
    <td align="right">商品名稱</td>
    <td><input type="text" id="name" name="name"></td>
</tr>
<tr>
    <td align="right">生產廠家</td>
    <td><input type="text" id="changjia" name="changjia"></td>
</tr>
<tr>
    <td align="right">型號</td>
    <td><input type="text" id="xinghao" name="xinghao"></td>
</tr>
<tr>
    <td align="right">規格</td>
    <td><input type="text" id="guige" name="guige"></td>
</tr>
<tr>
    <td align="right">數量</td>
    <td><input type="text" id="num" name="num"></td>
</tr>
<tr>
    <td align="right">日期</td>
    <td><input type="text" id="date" name="date"></td>
</tr>
<tr>
    <td align="right">時間</td>
    <td><input type="text" id="time" name="time"></td>
</tr>
<tr>
    <td align="right">入(出)庫單位名稱</td>
    <td><input type="text" id="danwei" name="danwei"></td>
</tr>
<