实验要求: 1.、有一个存放商品的仓库,每天都有商品出库和入库。 2、每种商品都有名称、生产厂家、型号、规格等。 3、出入库时必须填写出入库单据,单据包括商品名称、生产厂家、型号、规格、数量、日期、时间、入库单位(或出库单位)名称、送货(或提货)人姓名。

建两张数据表,一张用来商品的增删改查,一张记录商品的出库与入库信息

所用及代码:

public class GoodsBean
{
    private String name;
    private String type;
    private String PM;
    private String specification;
    private int number;
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public String getType()
    {
        return type;
    }
    public void setType(String type)
    {
        this.type = type;
    }
    public String getPM()
    {
        return PM;
    }
    public void setPM(String pM)
    {
        PM = pM;
    }
    public String getSpecification()
    {
        return specification;
    }
    public void setSpecification(String specification)
    {
        this.specification = specification;
    }
    public int getNumber()
    {
        return number;
    }
    public void setNumber(int number)
    {
        this.number = number;
    }
    
}
public class GoodsBean
{
    private String name;
    private String type;
    private String PM;
    private String specification;
    private int number;
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public String getType()
    {
        return type;
    }
    public void setType(String type)
    {
        this.type = type;
    }
    public String getPM()
    {
        return PM;
    }
    public void setPM(String pM)
    {
        PM = pM;
    }
    public String getSpecification()
    {
        return specification;
    }
    public void setSpecification(String specification)
    {
        this.specification = specification;
    }
    public int getNumber()
    {
        return number;
    }
    public void setNumber(int number)
    {
        this.number = number;
    }
    
}

 

import java.sql.*;


import com.ccr.jsp.bean.GoodsBean;
import com.ccr.jsp.util.DB;

public class GoodsDao
{
    public boolean insert(GoodsBean g)
    {
        DB db=new DB();
        Connection con = db.getCon();
        try
        {
        Statement stm = con.createStatement();
        stm.execute("insert into goods(name,PM,type,specification,number) values ('"+g.getName()+"','"+g.getPM()+"','"+g.getType()+"','"+g.getSpecification()+"',"+g.getNumber()+")");
        }
        catch (Exception e) 
        {
        e.printStackTrace();
        return false;
        }
        return true;
    }
    public boolean update(int number,int i)
    {
        int num=0;
        DB db=new DB();
        Connection con = db.getCon();
        try
        {
        Statement stm = con.createStatement();
        ResultSet rs =stm.executeQuery("select number from goods where id="+i);
        System.out.println("update"+i+"num "+number);
        rs.next();
        num=rs.getInt("number");
        System.out.print(num+"+"+number);
        num=num+number;
        System.out.println("="+num);
        stm.execute("update goods set number="+num+" where id="+i);
        }
        catch (Exception e) 
        {
        e.printStackTrace();
        return false;
        }
        return true;
    }
    public int chick(GoodsBean g)
    {
        int i=-1;//i>0查询有结果
        DB db=new DB();
        Connection con = db.getCon();
        try
        {
        Statement stm = con.createStatement();
        ResultSet rs =stm.executeQuery("select id from goods where name='"+g.getName()+"' and PM='"+g.getPM()+"' and type='"+g.getType()+"' and specification='"+g.getSpecification()+"'");
        if(rs.next())
        {
            i=rs.getInt("id");
        }
        } 
        catch (Exception e) 
        {
        e.printStackTrace();
        }
        return i;
    }
}
import java.sql.*;


import com.ccr.jsp.bean.GoodsBean;
import com.ccr.jsp.util.DB;

public class GoodsDao
{
    public boolean insert(GoodsBean g)
    {
        DB db=new DB();
        Connection con = db.getCon();
        try
        {
        Statement stm = con.createStatement();
        stm.execute("insert into goods(name,PM,type,specification,number) values ('"+g.getName()+"','"+g.getPM()+"','"+g.getType()+"','"+g.getSpecification()+"',"+g.getNumber()+")");
        }
        catch (Exception e) 
        {
        e.printStackTrace();
        return false;
        }
        return true;
    }
    public boolean update(int number,int i)
    {
        int num=0;
        DB db=new DB();
        Connection con = db.getCon();
        try
        {
        Statement stm = con.createStatement();
        ResultSet rs =stm.executeQuery("select number from goods where id="+i);
        System.out.println("update"+i+"num "+number);
        rs.next();
        num=rs.getInt("number");
        System.out.print(num+"+"+number);
        num=num+number;
        System.out.println("="+num);
        stm.execute("update goods set number="+num+" where id="+i);
        }
        catch (Exception e) 
        {
        e.printStackTrace();
        return false;
        }
        return true;
    }
    public int chick(GoodsBean g)
    {
        int i=-1;//i>0查询有结果
        DB db=new DB();
        Connection con = db.getCon();
        try
        {
        Statement stm = con.createStatement();
        ResultSet rs =stm.executeQuery("select id from goods where name='"+g.getName()+"' and PM='"+g.getPM()+"' and type='"+g.getType()+"' and specification='"+g.getSpecification()+"'");
        if(rs.next())
        {
            i=rs.getInt("id");
        }
        } 
        catch (Exception e) 
        {
        e.printStackTrace();
        }
        return i;
    }
}

 

import java.io.IOException;
import java.util.ArrayList;
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 com.ccr.jsp.bean.GoodsBean;
import com.ccr.jsp.bean.ListBean;
import com.ccr.jsp.dao.GoodsDao;
import com.ccr.jsp.dao.ListDao;

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

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("content-type", "text/html;charset=UTF-8");
        String action=request.getParameter("action");
        if(action.equals("select"))
        {
            select(request,response);
        }
        if(action.equals("in"))
        {
            in(request,response);
        }
        if(action.equals("out"))
        {
            out(request,response);
        }
    }
    protected void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        String name=request.getParameter("name");
        String date=request.getParameter("date");
        ListDao d=new ListDao();
        List<ListBean>l=d.select(name,date);
        request.getSession().setAttribute("l", l);
        request.getRequestDispatcher("select.jsp").forward(request,response);
    }
    protected void in(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        int id=-2;
        ListBean b=new ListBean();
        GoodsBean g=new GoodsBean();
        GoodsDao gd=new GoodsDao();
        ListDao ld=new ListDao();
        b.setName(request.getParameter("name"));
        b.setPM(request.getParameter("PM"));
        b.setType(request.getParameter("type"));
        b.setSpecification(request.getParameter("specification"));
        b.setNumber(Integer.parseInt(request.getParameter("number")));
        b.setUnits(request.getParameter("units"));
        b.setPName(request.getParameter("PName"));
        b.setIo("in");
        b.setDate();
        ld.insert(b);
        
        
        g.setName(request.getParameter("name"));
        g.setPM(request.getParameter("PM"));
        g.setType(request.getParameter("type"));
        g.setSpecification(request.getParameter("specification"));
        g.setNumber(Integer.parseInt(request.getParameter("number")));
        id=gd.chick(g);
        System.out.println(id);
        if(id>0)
        {
            gd.update(b.getNumber(), id);
        }
        else
        {
            gd.insert(g);
        }
        request.getRequestDispatcher("in.jsp").forward(request,response);
    }
    protected void out(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        int id=-2;
        ListBean b=new ListBean();
        GoodsBean g=new GoodsBean();
        GoodsDao gd=new GoodsDao();
        ListDao ld=new ListDao();
        b.setName(request.getParameter("name"));
        b.setPM(request.getParameter("PM"));
        b.setType(request.getParameter("type"));
        System.out.println(b.getType());
        b.setSpecification(request.getParameter("specification"));
        int number=Integer.parseInt(request.getParameter("number"));
        number=-1*number;
        System.out.println("SN"+number);
        b.setNumber(number);
        b.setUnits(request.getParameter("units"));
        b.setPName(request.getParameter("PName"));
        b.setIo("out");
        b.setDate();
        ld.insert(b);
        
        
        g.setName(request.getParameter("name"));
        g.setPM(request.getParameter("PM"));
        g.setType(request.getParameter("type"));
        g.setSpecification(request.getParameter("specification"));
        g.setNumber(number);
        id=gd.chick(g);
        if(id>0)
        {
            gd.update(b.getNumber(), id);
        }
        else
        {
            System.out.println("错误");
        }
        request.getRequestDispatcher("out.jsp").forward(request,response);
    }

}

 

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

public class DB
{
    private Connection con;
    private Statement stm;
    private ResultSet rs;
    private String classname="com.mysql.jdbc.Driver";
    private String url="jdbc:mysql://127.0.0.1:3306/warehouse?useSSL=false&characterEncoding=utf-8";
    public Connection getCon(){          
        try{
            Class.forName(classname);
            System.out.println("驱动加载成功");
        }
        catch(ClassNotFoundException e){
            e.printStackTrace();
        }
        try{
            con=DriverManager.getConnection(url,"root","root");
            System.out.println("数据库连接成功");
        }
        catch(Exception e){
            e.printStackTrace(System.err);
            con=null;
        }
        return con;
    }
    public static void close(Statement stm, Connection conn) {
        if(stm!=null) {
            try {
                stm.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void close(ResultSet rs, Statement stm, Connection con) {
        if(rs!=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(stm!=null) {
            try {
                stm.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(con!=null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>in</title>
</head>
<body>
<h1>入库</h1>
<form method="post" action="GoodsServlet?action=in">
<div>
    <div>
        <div style="display:inline"><label for="name">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label></div>
        <div style="display:inline"><input type="text" name="name" id="name"></div>
    </div><br/>
    <div>
        <div style="display:inline"><label for="name">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label></div>
        <div style="display:inline"><input type="text" name="type" id="type"></div>
    </div><br/>
    <div>
        <div style="display:inline"><label for="name">出产厂家</label></div>
        <div style="display:inline"><input type="text" name="PM" id="PM"></div>
    </div><br/>
    <div>
        <div style="display:inline"><label for="name">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label></div>
        <div style="display:inline"><input type="text" name="specification" id="specification"></div>
    </div><br/>
    <div>
        <div style="display:inline"><label for="name">入库单位</label></div>
        <div style="display:inline"><input type="text" name="units" id="units"></div>
    </div><br/>
    <div>
        <div style="display:inline"><label for="name">送货人名</label></div>
        <div style="display:inline"><input type="text" name="PName" id="PName"></div>
    </div><br/>
    <div>
        <div style="display:inline"><label for="name">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label></div>
        <div style="display:inline"><input type="text" name="number" id="number"></div>
    </div><br/>
    <div>
        <div><input type="submit" value="提交"></div>
    </div>
</div>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>left</title>
</head>
<body>
<div>
<div><a style="color:blue" target="right" href="select.jsp">查询</a></div><br/>
<div><a style="color:blue" target="right" href="out.jsp">出库</a></div><br/>
<div><a style="color:blue" target="right" href="in.jsp">入库</a></div><br/>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>main</title>
<style>
        html,body{width: 100%;height: 100%;padding: 0;margin: 0}
         #left{width:20%;height: 100%;padding: 0;margin: 0;border: none;}
         #right{width: 80%;height: 100%;padding: 0;margin: 0;border: none;}
</style>
</head>
<body>
<iframe name="left" id="left" align="left" height="500px" width="200" scrolling="no" src="left.jsp"></iframe>
<iframe name="right" id="right" align="right" height="500px" width="800" scrolling="no" src="select.jsp"></iframe>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>main</title>
<style>
        html,body{width: 100%;height: 100%;padding: 0;margin: 0}
         #left{width:20%;height: 100%;padding: 0;margin: 0;border: none;}
         #right{width: 80%;height: 100%;padding: 0;margin: 0;border: none;}
</style>
</head>
<body>
<iframe name="left" id="left" align="left" height="500px" width="200" scrolling="no" src="left.jsp"></iframe>
<iframe name="right" id="right" align="right" height="500px" width="800" scrolling="no" src="select.jsp"></iframe>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.ccr.jsp.bean.ListBean" %>
<%@ page import="java.util.List" %>
<%@ page import="javax.servlet.http.HttpServletRequest" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>select</title>
</head>
<body>
<h1>信息查询</h1>
<form method="post" action="GoodsServlet?action=select">
    <div>
        <div>
            <div><label>产品名称</label></div>
            <div><input type="text" name="name" id="name"></div>
        </div>
        <div>
            <div><label>日期</label></div>
            <div><input type="text" name="date" id="date"></div>
        </div>
        <div>
            <input type="submit" value="查询">
        </div>
    </div>
</form>
<c:set var="list" value="${sessionScope.l}"></c:set>
<c:if test="${l!=null }">
<c:forEach var="i" begin="0" end="${l.size()-1}">
&nbsp;名称&nbsp;${l[i].getName()}
&nbsp;型号&nbsp;${l[i].getType()}
&nbsp;出产厂家&nbsp;${l[i].getPM()}
&nbsp;规格&nbsp;${l[i].getSpecification()}
&nbsp;数量&nbsp;${l[i].getNumber()}
&nbsp;单位&nbsp;${l[i].getUnits()}
&nbsp;姓名&nbsp;${l[i].getPName()}
&nbsp;操作&nbsp;${l[i].getIo()}
&nbsp;日期&nbsp;${l[i].getDate()}
<br/>
</c:forEach>
</c:if>
</body>
</html>

 

 posted on 2018-12-13 22:52  Aurinko  阅读(506)  评论(0编辑  收藏  举报