库存物资管理
创建两个表,一个用来存仓库中的物品信息,另一个用来存 出入库单据的信息,两个表分别同时操作,进行信息存储。
package service; public class Cun { private int id; private String sname; private String made; private String xinghao; private String guige; private int number; private String day; private String shijian ; private String dwname; private String rname; private String caozuo; public int getid() { return id; } public void setid(int id) { this.id = id; } public String getsname() { return sname; } public void setsname(String sname) { this.sname = sname; } public String getmade() { return made; } public void setmade(String made) { this.made = made; } 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 int getnumber() { return number; } public void setnumber(int number) { this.number = number; } public String getday() { return day; } public void setday(String day) { this.day = day; } public String getshijian() { return shijian; } public void setshijian(String shijian) { this.shijian = shijian; } public String getdwname() { return dwname; } public void setdwname(String dwname) { this.dwname = dwname; } public String getrname() { return rname; } public void setrname(String rname) { this.rname = rname; } public String getcaozuo() { return caozuo; } public void setcaozuo(String caozuo) { this.caozuo=caozuo; } }
package dao; import util.DBUtil; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import service.Cun; public class CunDao { public List<Cun> getcun(){ List<Cun> list =new ArrayList<Cun>(); Cun cun = null; Connection conn=DBUtil.getConnection();//连接数据库 String sql="select * from kucun"; Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while(rs.next()) { cun = new Cun(); cun.setid(rs.getInt("id")); cun.setsname(rs.getString("sname")); cun.setmade(rs.getString("made")); cun.setxinghao(rs.getString("xinghao")); cun.setguige(rs.getString ("guige")); cun.setnumber(rs.getInt("number")); cun.setday(rs.getString("day")); cun.setshijian(rs.getString("shijian")); cun.setdwname(rs.getString("dwname")); cun.setrname(rs.getString("rname")); cun.setcaozuo(rs.getString("caozuo")); list.add(cun); } } catch(SQLException e) { e.printStackTrace(); } return list; } public boolean addcun(Cun cun) { String sql = "insert into kucun(sname, made, xinghao,guige,number,day,shijian,dwname,rname,caozuo) values('" + cun.getsname() + "','" + cun.getmade() + "','" + cun.getxinghao() + "','"+cun.getguige()+"','"+cun.getnumber()+"','"+cun.getday()+"','"+cun.getshijian()+"','"+cun.getdwname()+"','"+cun.getrname()+"','"+cun.getcaozuo()+"')"; //创建数据库链接 Connection conn = DBUtil.getConnection(); Statement state = null; try { state = conn.createStatement(); //state.executeUpdate(sql); int a= state.executeUpdate(sql); state.close(); return a>0?true:false; } catch (Exception e) { e.printStackTrace(); } return false; } public List<Cun> search (String sname,String day){ List<Cun> list =new ArrayList<Cun>(); Cun cun= null; Connection conn=DBUtil.getConnection();//连接数据库 String sql=null; if(sname!=null) { sql="select * from kucun where sname like'%"+sname+"%'";} if(day!=null) { sql="select * from kucun where day like'%"+day+"%'"; } Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while(rs.next()) { cun = new Cun(); cun.setid(rs.getInt("id")); cun.setsname(rs.getString("sname")); cun.setmade(rs.getString("made")); cun.setxinghao(rs.getString("xinghao")); cun.setguige(rs.getString ("guige")); cun.setnumber(rs.getInt("number")); cun.setday(rs.getString("day")); cun.setshijian(rs.getString("shijian")); cun.setdwname(rs.getString("dwname")); cun.setrname(rs.getString("rname")); cun.setcaozuo(rs.getString("caozuo")); list.add(cun); } } catch(SQLException e) { e.printStackTrace(); } return list; } public List<Cun> getku(){
List<Cun> list =new ArrayList<Cun>();
Cun cun = null;
Connection conn=DBUtil.getConnection();//连接数据库
String sql="select * from cangku";
Statement state = null;
ResultSet rs = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while(rs.next()) {
cun = new Cun();
cun.setid(rs.getInt("id"));
cun.setsname(rs.getString("sname"));
cun.setmade(rs.getString("made"));
cun.setxinghao(rs.getString("xinghao"));
cun.setguige(rs.getString ("guige"));
cun.setnumber(rs.getInt("number"));
list.add(cun);
}
}
catch(SQLException e) {
e.printStackTrace();
}
return list;
} }
package servlet; import java.io.IOException; 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 dao.CunDao; import service.Cun; /** * Servlet implementation class AddServlet */ @WebServlet("/AddServlet") public class AddServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public AddServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); doGet(request, response); String sname = request.getParameter("sname"); String made = request.getParameter("made"); String xinghao = request.getParameter("xinghao"); String guige = request.getParameter("guige"); Integer number = Integer.valueOf(request.getParameter("number")); String day = request.getParameter("day"); String shijian = request.getParameter("shijian"); String dwname = request.getParameter("dwname"); String rname = request.getParameter("rname"); String caozuo =request.getParameter("caozuo"); Cun cun = new Cun(); cun.setsname(sname); cun.setmade(made); cun.setxinghao(xinghao); cun.setguige(guige); cun.setnumber(number); cun.setday(day); cun.setshijian(shijian); cun.setdwname(dwname); cun.setrname(rname); cun.setcaozuo(caozuo); CunDao cun1 = new CunDao(); cun1.addcun(cun); request.getRequestDispatcher("menu.jsp").forward(request, response); } }
package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBUtil { // 数据库连接地址 private static String URL = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC"; // 数据库的用户名 private static String UserName = "root"; // 数据库的密码 private static String Password = "123456"; public static Connection getConnection() { Connection Conn=null; try { Class.forName("com.mysql.cj.jdbc.Driver"); // 加载驱动 System.out.println("加载驱动成功!!!"); } catch (ClassNotFoundException e) { // TODO: handle exception e.printStackTrace(); } try { //通过DriverManager类的getConenction方法指定三个参数,连接数据库 Conn = DriverManager.getConnection(URL, UserName, Password); System.out.println("连接数据库成功!!!"); //返回连接对象 //return Conn; } catch (SQLException e) { // TODO: handle exception e.printStackTrace(); } return Conn; } public static void main(String[] args)throws SQLException { //测试数据库是否连通 Connection conn = getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; String sql ="select * from kucun"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); System.out.println(getConnection()); while(rs.next()){ System.out.println("空"); } } }
<%@ 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>
<table align="center">
<tr>
<td>
<h2>选择操作</h2>
</td>
</tr>
</table>
<table align="center" border="1" width="500">
<tr>
<td><a href="add.jsp">出入库信息添加</a></td>
<td><a href="search.jsp">出入库信息查询</a></td>
<td><a href="add.jsp">出入库信息删除</a></td>
</table>
</body>
</html>
<%@page import="dao.CunDao"%> <%@ page import="service.Cun" %> <%@ page import="java.util.List" %> <%@ 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> <table align="center" border="1" width="800"> <% CunDao cun1 =new CunDao(); List<Cun> list=cun1.getcun(); if (list == null || list.size() < 1) { %> <tr bgcolor="white"><td colspan="5" ><h4 align="center">没有数据</h4></td></tr> <tr> <th>商品名称</th> <th>生产厂家</th> <th>型号</th> <th>规格</th> <th>数量</th> <th>日期</th> <th>时间</th> <th>单位名称</th> <th>操作人</th> <th>操作</th> </tr> <% } else{%> <tr> <th>商品名称</th> <th>生产厂家</th> <th>型号</th> <th>规格</th> <th>数量</th> <th>日期</th> <th>时间</th> <th>单位名称</th> <th>操作人</th> <th>操作</th> </tr> <% for (Cun cun: list) { %> <tr> <td><%=cun.getsname()%></td> <td><%=cun.getmade()%></td> <td><%=cun.getxinghao()%></td> <td><%=cun.getguige()%></td> <td><%=cun.getnumber()%></td> <td><%=cun.getday()%></td> <td><%=cun.getshijian()%></td> <td><%=cun.getdwname()%></td> <td><%=cun.getrname()%></td> <td><%=cun.getcaozuo()%></td> </tr> <% } } %> </table> </body> </html>
<%@ 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> <form action ="AddServlet" method ="post" onsubmit="check(this)"> <table align="center"> <tr> <td> <h2>添加单据</h2> </td> </tr> </table> <table align="center" border="1" width="500"> <tr> <td >商品名称</td> <td><input type ="text" name ="sname"></td> </tr> <tr> <td >生产厂家</td> <td><input type ="text" name ="made"></td> </tr> <tr> <td >型号</td> <td><input type ="text" name ="xinghao"></td> </tr> <tr> <td >规格</td> <td><input type ="text" name ="guige"></td> </tr> <tr> <td >数量</td> <td><input type ="text" name ="number"></td> </tr> <tr> <td >日期</td> <td><input type ="text" name ="day"></td> </tr> <tr> <td >时间</td> <td><input type ="text" name ="shijian"></td> </tr> <tr> <td >单位</td> <td><input type ="text" name ="dwname"></td> </tr> <tr> <td >操作人</td> <td><input type ="text" name ="rname"></td> </tr> <tr> <td >操作</td> <td><input type ="text" name ="caozuo"></td> </tr> <tr> <td align="center" colspan="2"> <input type="submit" value="添加" /> <input type="reset" value="重置" /> </td> </tr> <tr> <td align="center" colspan="2"><a href="menu.jsp">返回主页</a></td> </tr> </table> <script type="text/javascript"> function check(form) { with (form) { if (name.value !== "") { alert("添加成功"); } } } </script> </form> </body> </html>
<%@ 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>
<form action ="search2.jsp" method ="get" >
<table align="center">
<tr>
<td>
<h2>查询单据</h2>
</td>
</tr>
</table>
<table align="center" border="1" width="300">
<tr>
<td >商品名称</td>
<td><input type ="text" name ="sname"></td>
</tr>
<tr>
<td >日期</td>
<td><input type ="text" name ="day"></td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="查询" />
</td>
</table>
</form>
</body>
</html>
<%@page import="dao.CunDao"%> <%@page import="util.DBUtil"%> <%@ page import="service.Cun" %> <%@ page import="java.util.List" %> <%@ 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> <table align="center" border="1" width="800"> <% CunDao cun1 =new CunDao(); List<Cun> list1=cun1.search(request.getParameter("sname"),request.getParameter("day")); if (list1 == null || list1.size() < 1) { %> <tr bgcolor="white"><td colspan="5" ><h4 align="center">没有数据</h4></td></tr> <% } else{ %> <tr> <th>商品名称</th> <th>生产厂家</th> <th>型号</th> <th>规格</th> <th>数量</th> <th>日期</th> <th>时间</th> <th>单位名称</th> <th>操作人</th> <th>操作</th> </tr> <% for (Cun cun: list1) { %> <tr> <td><%=cun.getsname()%></td> <td><%=cun.getmade()%></td> <td><%=cun.getxinghao()%></td> <td><%=cun.getguige()%></td> <td><%=cun.getnumber()%></td> <td><%=cun.getday()%></td> <td><%=cun.getshijian()%></td> <td><%=cun.getdwname()%></td> <td><%=cun.getrname()%></td> <td><%=cun.getcaozuo()%></td> </tr> <% } } %> </table> </body> </html>
<%@page import="dao.CunDao"%> <%@ page import="service.Cun" %> <%@ page import="java.util.List" %> <%@ 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> <table align="center" border="1" width="500"> <% CunDao cun1 =new CunDao(); List<Cun> list=cun1.getku(); if (list == null || list.size() < 1) { %> <tr bgcolor="white"><td colspan="5" ><h4 align="center">没有数据</h4></td></tr> <tr> <th>商品名称</th> <th>生产厂家</th> <th>型号</th> <th>规格</th> <th>数量</th> </tr> <% } else{%> <tr> <th>商品名称</th> <th>生产厂家</th> <th>型号</th> <th>规格</th> <th>数量</th> </tr> <% for (Cun cun: list) { %> <tr> <td><%=cun.getsname()%></td> <td><%=cun.getmade()%></td> <td><%=cun.getxinghao()%></td> <td><%=cun.getguige()%></td> <td><%=cun.getnumber()%></td> </tr> <% } } %> </table> </body> </html>