库存物资管理系统要求有一个存放商品的仓库 ,每天都有商品的出库和入库。每个商品都有名称、生产厂家、型号、规格。我的数据库里商品的表如图。
包括id、产品名称、型号、规格。数据库里内容为,如图。
分别给他们的名称、型号、规格赋值。单据里的表属性为,如图。
表内的内容为,如图。
首先要实现商品的增删改查。首先要连接到数据库里面的表cangku。连接数据库的代码放在util包的DButil类里。代码如下。
package util; 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/kucunwuzi"; public static String db_user = "root"; public static String db_pass = "zxh521+."; 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; } public static void main(String[] args) { try { //第三步:获取连接类实例con,用con创建Statement对象类实例 sql_statement Connection con = getConn(); Statement sql_statement = con.createStatement(); String query = "select * from cangku"; ResultSet result = sql_statement.executeQuery(query); //显示数据中network表中的内容: System.out.println("数据库kucunwuzi表cangku中的数据如下:"); System.out.println("------------------------"); System.out.println("name" + " " + "xinghao"+" "+"guige"+" "); System.out.println("------------------------"); //对获得的查询结果进行处理,对Result类的对象进行操作 while (result.next()) { String name = result.getString("name"); String xinghao = result.getString("xinghao"); String guige = result.getString("guige"); //取得数据库中的数据 System.out.println(""+name + " " + xinghao+" "+guige+" "); } } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } } /** * 关闭连接 * @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(); } } } }
在连接数据库时首先要加载驱动。我在这里设置连上数据库之后查看数据库中的信息,这样可以更清晰的看出来数据库中的内容,并且可以知道成功连上了数据库。
在要实现数据库增删改查的时候,有一个实体类,我把它放在entity包里,这里是各个属性和set,get方法。代码如下。
package entity; public class course { private int id; private String name; private String xinghao; private String guige; 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 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 course(){} public course(int id,String name,String xinghao,String guige){ this.id=id; this.name=name; this.xinghao=xinghao; this.guige=guige; } public course(String name,String xinghao,String guige){ this.name=name; this.xinghao=xinghao; this.guige=guige; } }
在这个里面建了三个构造方法,一个是无参,一个是有参但是没有id,一个是有参有id。
要建一个类与数据库进制操作,我建立的是dao包,里面的类是coursedao,代码如下。
package dao; 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 entity.course; import util.DButil; public class coursedao { public boolean add(course course) {//建立course类的对象 String sql="insert into cangku(name,xinghao,guige)values('"+course.getName()+"','"+course.getXinghao()+"','"+course.getGuige()+"')"; //添加语句,调用course类里面的方法,完成添加 Connection conn=DButil.getConn();//建立桥 Statement state=null;//建立车 Boolean f=false; int a=0; try { state = conn.createStatement();//桥给车一个可以运输货物的功能 a=state.executeUpdate(sql);//把运输货物的多少赋值给a } catch (Exception e) { e.printStackTrace(); } finally { DButil.close(state, conn);//在运输结束后,毁掉桥和车 } if (a > 0) { f = true; } return f; } public boolean name(String name) { Boolean flag=false; String sql="select from cangku 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; } public course getCourseById(int id) { String sql = "select * from cangku 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 xinghao = rs.getString("xinghao"); String guige = rs.getString("guige"); course = new course(id,name,xinghao,guige); } } catch (Exception e) { e.printStackTrace(); } finally { DButil.close(rs, state, conn); } return course; } public course getCourseByName(String name) { String sql = "select * from cangku 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 xinghao = rs.getString("xinghao"); String guige = rs.getString("guige"); course = new course(name,xinghao,guige); } } catch (Exception e) { e.printStackTrace(); } finally { DButil.close(rs, state, conn); } return course; } public boolean delete (int id) { boolean f = false; String sql = "delete from cangku 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; } public boolean update(course course) { String sql = "update cangku set name='" + course.getName() + "', xinghao='" + course.getXinghao()+"',guige='"+course.getGuige() + "' 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; } public List<course> search(String name, String xinghao,String guige) { String sql = "select * from cangku where "; if (name != "") { sql += "name like '%" + name + "%'"; } if (xinghao != "") { sql += "xinghao like '%" + xinghao + "%'"; } if (guige != "") { sql += "guige like '%" + guige + "%'"; } 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 xinghao2 = rs.getString("xinghao"); String guige2 = rs.getString("guige"); bean = new course(id, name2, xinghao2,guige2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DButil.close(rs, state, conn); } return list; } public List<course> list() { String sql = "select * from cangku"; 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 xinghao2 = rs.getString("xinghao"); String guige2 = rs.getString("guige"); bean = new course(id,name2,xinghao2,guige2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DButil.close(rs, state, conn); } return list; } }
在与数据库进行操作时,比如添加,要先写添加的sql语句,然后把数据库想象成一个岛,然后搭建一个桥,与岛相连,在造一个车。之后使用try,catch语句,在这个语句中给车一个可以载货物的功能,让它载上刚才的sql语句,通过桥到达岛上;其余的方法,比如删除,修改,通过名字查找,通过id查找都与这个类似。
有了与数据库进行联系的层,就需要有一个服务层,这一个实现dao层与servlet层之间的联系。代码如下。
package service; import java.util.List; import dao.coursedao; import entity.course; public class courseservice { coursedao cdao=new coursedao();//因为dao层就是与数据库的联系,所以其他层要要与数据可联系就要通过dao层 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); } public void update(course course) { cdao.update(course); } public course getCourseById(int id) { return cdao.getCourseById(id); } public course getCourseByName(String name) { return cdao.getCourseByName(name); } public List<course> search(String name, String xinghao,String guige) { return cdao.search(name, xinghao,guige); } public List<course> list() { return cdao.list(); } }
这一层是service层。接下来就需要有一层可以实现与后台.jsp文件之间的联系。因为一个网页版的数据库的增删改查离不开前台和后台的合作。代码如下。
package servlet; import java.io.IOException; import java.io.UnsupportedEncodingException; 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 dao.coursedao; import entity.course; import entity.courselist; import service.courseservice; @WebServlet("/courseservlet") public class courseservlet extends HttpServlet { private static final long serialVersionUID = 1L; courseservice service = new courseservice(); public courseservlet() { super(); } 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); } } 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); } 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); } } 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); } private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); String xinghao = req.getParameter("xinghao"); String guige = req.getParameter("guige"); List<course> courses = service.search(name, xinghao,guige); req.setAttribute("courses", courses); req.getRequestDispatcher("searchlist.jsp").forward(req,resp); } private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); String name = req.getParameter("name"); String xinghao = req.getParameter("xinghao"); String guige = req.getParameter("guige"); course course = new course(id, name, xinghao,guige); service.update(course); req.setAttribute("message", "修改成功"); req.getRequestDispatcher("courseservlet?method=list").forward(req,resp); } private void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); service.del(id); req.setAttribute("message", "删除成功!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); String xinghao = req.getParameter("xinghao"); String guige = req.getParameter("guige"); course course = new course(name,xinghao,guige); //添加后消息显示 if(service.add(course)) { req.setAttribute("message", "添加成功");//传递参数给jsp req.getRequestDispatcher("add.jsp").forward(req,resp);//与前台add.jsp文件取得联系 } else { req.setAttribute("message", "物品名称重复,请重新录入"); req.getRequestDispatcher("add.jsp").forward(req,resp); } } }
这一层首先要设置请求的字符编码格式,然后获得请求表单中的信息。根据.jsp文件中写的来运行一下相关方法。比如添加方法。在这个方法中同样要先设置请求的字符编码格式,然后获得请求表单中的信息,然后把在.jsp中表格前的内容在分别赋给一个字符串类型的变量,然后运行service中的add方法,如果运行成功就会再次回到add.jsp文件的网页部分,并且显示添加成功。其他方法与此类似。
然后就是后台的.jsp文件,主页的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 class="a"> <a href="add2.jsp">物品入库信息</a> </div> <div class="a"> <a href="del2.jsp">物品出库信息</a> </div> <div class="a"> <a href="search2.jsp">物品单据信息查询</a> </div> </div> </body> </html>
在这个里面与其他的.jsp文件建立了联系,这样就可以实现网页之间的跳转。
之后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; 160px; color: white; background-color: greenyellow; } </style> </head> <body> <% Object message = request.getAttribute("message");//接收从sevelet里面传来的参数 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="xinghao" name="xinghao"/> </div> <div class="a"> 物品规格<input type="text" id="guige" name="guige"/> </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 xinghao = document.getElementById("xinghao"); var guige = document.getElementById("guige"); //非空 if(name.value == '') { alert('物品名称为空'); name.focus();//// return false; } if(xinghao.value == '') { alert('物品型号为空'); classroom.focus(); return false; } if(guige.value == '') { alert('物品规格为空'); classroom.focus(); return false; } } </script> </body> </html>
在添加的时候要进行/method的判断,来判断要运行courseservlet中的哪个方法。之后要设置文本框的格式。最后要进行判断,输入的内容不能为空。
删除的代码如下。
<%@ 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; 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>
首先根据名字进行查找。
<%@ 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; 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.xinghao}</td> </tr> <tr> <td>物品规格</td> <td>${course.guige}</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>
然后查找出内容,在点了删除键之后要确定是否真的删除。
<%@ 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; 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 align="center" colspan="2">操作</td> </tr> <c:forEach items="${courses}" var="item"> <tr> <td>${item.id}</td> <td>${item.name}</td> <td>${item.xinghao}</td> <td>${item.guige}</td> <td><a href="courseservlet?method=getcoursebyid&id=${item.id}">修改</a></td> </tr> </c:forEach> </table> </div> </body>
这个是进行修改的页面,首先把全部的信息列出来,可以选择性的对哪一个信息进行修改。点击修改之后。到达另一个修改界面。代码如下。
<%@ 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; 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="xinghao" name="xinghao" value="${course.xinghao}"/> </div> <div class="a"> 物品规格<input type="text" id="guige" name="guige" value="${course.guige}"/> </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 xinghao = document.getElementById("xinghao"); var guige = document.getElementById("guige"); //非空 if(name.value == '') { alert('物品名称为空'); name.focus(); return false; } if(xinghao.value == '') { alert('物品型号为空'); classroom.focus(); return false; } if(guige.value == '') { alert('物品规格为空'); classroom.focus(); return false; } } </script> </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> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; 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="xinghao" name="xinghao" /> </div> <div class="a"> 物品规格<input type="text" id="guige" name="guige" /> </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 xinghao = document.getElementById("xinghao"); var guige = document.getElementById("guige"); //非空 if(name.value == '' && xinghao.value == ''&&guige.value=='') { alert('请填写一个条件'); return false; } } </script> </body> </html>
在查询时输入一个条件,就会出现整个信息。出现整个信息的代码如下。
<%@ 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; 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> </tr> <!-- forEach遍历出adminBeans --> <c:forEach items="${courses}" var="item" varStatus="status"> <tr> <td>${item.id}</td> <td><a>${item.name}</a></td> <td>${item.xinghao}</td> <td>${item.guige}</td> </tr> </c:forEach> </table> </div> </body> </html>
在这个题目中要求使用单据,用单据记录商品出库入库的信息,所以又建立了一个表danju,首先也是连接整个表。内容如下。
package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; //连接数据库 public class DButil2 { public static String db_url = "jdbc:mysql://localhost:3306/kucunwuzi"; public static String db_user = "root"; public static String db_pass = "zxh521+."; 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; } public static void main(String[] args) { try { //第三步:获取连接类实例con,用con创建Statement对象类实例 sql_statement Connection con = getConn(); Statement sql_statement = con.createStatement(); String query = "select * from danju"; ResultSet result = sql_statement.executeQuery(query); //显示数据中network表中的内容: System.out.println("数据库kucunwuzi表danju中的数据如下:"); System.out.println("------------------------"); System.out.println("name" + " " + "xinghao"+" "+"guige"+" "+"num"+" "+"date"+" "+"time"+" "+"danwei"+" "+"pname"+" "); System.out.println("------------------------"); //对获得的查询结果进行处理,对Result类的对象进行操作 while (result.next()) { String name = result.getString("name"); String xinghao = result.getString("xinghao"); String guige = result.getString("guige"); String num = result.getString("num"); String date = result.getString("date"); String time = result.getString("time"); String danwei = result.getString("danwei"); String pname = result.getString("pname"); //取得数据库中的数据 System.out.println(""+name + " " + xinghao+" "+guige+" "+num+" "+date+" "+time+" "+danwei+" "+pname+" "); } } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } } /** * 关闭连接 * @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(); } } } }
然后是实体类,代码如下。
package entity; import java.util.List; public class courselist { private int id; private String name; private String xinghao; private String guige; private int num; private String date; private String danwei; private String pname; 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 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 getNum() { return num; } public void setNum(int num) { this.num = num; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getDanwei() { return danwei; } public void setDanwei(String danwei) { this.danwei = danwei; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public courselist() {} public courselist(int id,String name,String xinghao,String guige,int num,String date,String danwei,String pname) { this.id=id; this.name=name; this.xinghao=xinghao; this.guige=guige; this.num=num; this.date=date; this.danwei=danwei; this.pname=pname; } public courselist(String name,String xinghao,String guige,int num,String date,String danwei,String pname) { this.name=name; this.xinghao=xinghao; this.guige=guige; this.num=num; this.date=date; this.danwei=danwei; this.pname=pname; } }
然后是dao层,代码如下。
package dao; import java.sql.*; import java.util.List; import entity.course; import entity.courselist; import util.DButil; import util.DButil2; import java.util.ArrayList; public class coursedao2 { public boolean add2(courselist courselist) {//建立course类的对象 String sql="insert into danju(name,xinghao,guige,num,date,danwei,pname)values('"+courselist.getName()+"','"+courselist.getXinghao()+"','"+courselist.getGuige()+"','"+courselist.getNum()+"','"+courselist.getDate()+"','"+courselist.getDanwei()+"','"+courselist.getPname()+"')"; //添加语句,调用course类里面的方法,完成添加 System.out.println(sql); Connection conn=DButil2.getConn();//建立桥 Statement state=null;//建立车 Boolean f=false; int a=0; try { state = conn.createStatement();//桥给车一个可以运输货物的功能 a=state.executeUpdate(sql);//把运输货物的多少赋值给a } catch (Exception e) { e.printStackTrace(); } finally { DButil2.close(state, conn);//在运输结束后,毁掉桥和车 } if (a > 0) { f = true; } return f; } public boolean name2(String name) { Boolean flag=false; String sql="select from danju where name='"+name+"'"; Connection conn=DButil2.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 { DButil2.close(rs, state, conn); } return flag; } public courselist getCourseById2(int id) { String sql = "select * from danju where id ='" + id + "'"; Connection conn = DButil2.getConn(); Statement state = null; ResultSet rs = null; courselist courselist = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String name = rs.getString("name"); String xinghao = rs.getString("xinghao"); String guige = rs.getString("guige"); int num = rs.getInt("num"); String date = rs.getString("date"); String danwei = rs.getString("danwei"); String pname = rs.getString("pname"); courselist = new courselist(id,name,xinghao,guige,num,date,danwei,pname); } } catch (Exception e) { e.printStackTrace(); } finally { DButil.close(rs, state, conn); } return courselist; } public courselist getCourseByName2(String name) { String sql = "select * from danju where name ='" + name + "'"; Connection conn = DButil2.getConn(); Statement state = null; ResultSet rs = null; courselist courselist = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { int id = rs.getInt("id"); String xinghao = rs.getString("xinghao"); String guige = rs.getString("guige"); int num = rs.getInt("num"); String date = rs.getString("date"); String danwei = rs.getString("danwei"); String pname = rs.getString("pname"); courselist = new courselist(id,name,xinghao,guige,num,date,danwei,pname); } } catch (Exception e) { e.printStackTrace(); } finally { DButil2.close(rs, state, conn); } return courselist; } public boolean delete2 (int id) { boolean f = false; String sql = "delete from danju where id='" + id + "'"; Connection conn = DButil2.getConn(); Statement state = null; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DButil2.close(state, conn); } if (a > 0) { f = true; } return f; } public List<courselist> search2(String name) { String sql = "select * from danju where "; if (name != "") { sql += "name like '%" + name + "%'"; } List<courselist> list = new ArrayList<>(); Connection conn = DButil2.getConn(); Statement state = null; ResultSet rs = null; System.out.println(sql); try { state = conn.createStatement(); rs = state.executeQuery(sql); courselist bean = null; while (rs.next()) { int id = rs.getInt("id"); String name2 = rs.getString("name"); System.out.println(name2); String xinghao2 = rs.getString("xinghao"); String guige2 = rs.getString("guige"); int num2 = rs.getInt("num"); String date2 = rs.getString("date"); String danwei2 = rs.getString("danwei"); String pname2 = rs.getString("pname"); bean = new courselist(id, name2, xinghao2,guige2,num2,date2,danwei2,pname2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DButil2.close(rs, state, conn); } return list; } public List<courselist> list2() { String sql = "select * from danju"; List<courselist> list = new ArrayList<>(); Connection conn = DButil2.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); courselist bean = null; while (rs.next()) { int id = rs.getInt("id"); String name2 = rs.getString("name"); String xinghao2 = rs.getString("xinghao"); String guige2 = rs.getString("guige"); int num2 = rs.getInt("nume"); String date2 = rs.getString("date"); String danwei2 = rs.getString("danwei"); String pname2 = rs.getString("pname"); bean = new courselist(id, name2, xinghao2,guige2,num2,date2,danwei2,pname2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DButil2.close(rs, state, conn); } return list; } }
然后是service层,代码如下。
package service; import java.util.List; import dao.coursedao; import dao.coursedao2; import entity.course; import entity.courselist; public class courseservice2 { coursedao2 cdao2=new coursedao2(); public boolean add2(courselist courselist) { Boolean f=false; if(!cdao2.name2(courselist.getName())) { cdao2.add2(courselist); f=true; } return f; } public void del2(int id) { cdao2.delete2(id); } public courselist getCourseById2(int id) { return cdao2.getCourseById2(id); } public courselist getCourseByName2(String name) { return cdao2.getCourseByName2(name); } public List<courselist> search2(String name) { return cdao2.search2(name); } public List<courselist> list2() { return cdao2.list2(); } }
然后是servlet层,代码如下。
package servlet; 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 dao.coursedao; import dao.coursedao2; import entity.course; import entity.courselist; 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.coursedao2; import entity.courselist; import service.courseservice; import service.courseservice2; @WebServlet("/courseservlet2") public class courseservlet2 extends HttpServlet { private static final long serialVersionUID = 1L; courseservice2 service2 = new courseservice2(); public courseservlet2() { super(); // TODO Auto-generated constructor stub } protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8");//设置请求的字符编码格式 String method = req.getParameter("method");//获得请求表单中的信息 if ("add".equals(method)) { add2(req, resp); } else if ("del".equals(method)) { del2(req, resp); } else if ("search".equals(method)) { search2(req, resp); } else if ("getcoursebyid".equals(method)) { getCourseById2(req, resp); } else if ("getcoursebyname".equals(method)) { getCourseByName2(req, resp); } else if ("list".equals(method)) { list2(req, resp); } } private void list2(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); List<courselist> courses = service2.list2(); req.setAttribute("courses", courses); req.getRequestDispatcher("list2.jsp").forward(req,resp); } private void getCourseByName2(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); courselist courselist = service2.getCourseByName2(name); if(courselist == null) { req.setAttribute("message", "查无此物品!"); req.getRequestDispatcher("del2.jsp").forward(req,resp); } else { req.setAttribute("courselist", courselist); req.getRequestDispatcher("detail1.jsp").forward(req,resp); } } private void getCourseById2(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); courselist courselist = service2.getCourseById2(id); req.setAttribute("courselist", courselist); req.getRequestDispatcher("detail2.jsp").forward(req,resp); } private void search2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); //String xinghao = req.getParameter("xinghao"); //String guige = req.getParameter("guige"); //int num = Integer.parseInt(req.getParameter("num")); //String date = req.getParameter("date"); //String danwei = req.getParameter("danwei"); //String pname = req.getParameter("pname"); List<courselist> courses2 = service2.search2(name); //req.setAttribute("courses2", courses2); req.setAttribute("courses2", courses2); req.getRequestDispatcher("searchlist2.jsp").forward(req,resp); } private void del2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); service2.del2(id); req.setAttribute("message", "出库成功!"); req.getRequestDispatcher("del2.jsp").forward(req,resp); } private void add2(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); String xinghao = req.getParameter("xinghao"); String guige = req.getParameter("guige"); int num = Integer.parseInt(req.getParameter("num")); String date = req.getParameter("date"); String danwei = req.getParameter("danwei"); String pname = req.getParameter("pname"); courselist courselist = new courselist(name,xinghao,guige,num,date,danwei,pname); //添加后消息显示 if(service2.add2(courselist)) { req.setAttribute("message", "添加成功");//传递参数给jsp req.getRequestDispatcher("add2.jsp").forward(req,resp);//与前台add.jsp文件取得联系 } else { req.setAttribute("message", "物品名称重复,请重新录入"); req.getRequestDispatcher("add.2jsp").forward(req,resp); } } }
以上是后台的文件代码,之后是后台.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; 160px; color: white; background-color: greenyellow; } </style> </head> <body> <% Object message = request.getAttribute("message");//接收从sevelet里面传来的参数 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="courseservlet2?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="xinghao" name="xinghao"/> </div> <div class="a"> 物品规格<input type="text" id="guige" name="guige"/> </div> <div class="a"> 物品入库数量<input type="text" id="num" name="num"/> </div> <div class="a"> 物品入库日期<input type="text" id="date" name="date"/> </div> <div class="a"> 物品入库单位<input type="text" id="danwei" name="danwei"/> </div> <div class="a"> 物品入库送货人<input type="text" id="pname" name="pname"/> </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 xinghao = document.getElementById("xinghao"); var guige = document.getElementById("guige"); var num = document.getElementById("num"); var date = document.getElementById("date"); var danwei = document.getElementById("danwei"); var pname = document.getElementById("pname"); //非空 if(name.value == '') { alert('物品名称为空'); name.focus();//// return false; } if(xinghao.value == '') { alert('物品型号为空'); classroom.focus(); return false; } if(guige.value == '') { alert('物品规格为空'); classroom.focus(); return false; } if(num.value == '') { alert('物品数量为空'); classroom.focus(); return false; } if(date.value == '') { alert('物品入库日期为空'); classroom.focus(); return false; } if(danwei.value == '') { alert('物品入库单位为空'); classroom.focus(); return false; } if(pname.value == '') { alert('物品送货人为空'); classroom.focus(); return false; } } </script> </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> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; 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="courseservlet2?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>
<%@ 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; 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>${courselist.name}</td> </tr> <tr> <td>物品型号</td> <td>${courselist.xinghao}</td> </tr> <tr> <td>物品规格</td> <td>${courselist.guige}</td> </tr> <tr> <td>物品数量</td> <td>${courselist.num}</td> </tr> <tr> <td>物品出库日期</td> <td>${courselist.date}</td> </tr> <tr> <td>物品出库时间</td> <td>${courselist.date}</td> </tr> <tr> <td>物品出库送货人</td> <td>${courselist.danwei}</td> </tr> </table> <div class="a"> <a onclick="return check()" href="courseservlet2?method=del&id=${courselist.id}">出库</a> </div> </div> <script type="text/javascript"> function check() { if (confirm("真的要出库吗?")){ return true; }else{ return false; } } </script> </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> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; 160px; color: white; background-color: greenyellow; } </style> </head> <body> <div align="center"> <h1 style="color: red;">物品出入库信息查询</h1> <a href="index.jsp">返回主页</a> <form action="courseservlet2?method=search" 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");; var xinghao = document.getElementById("xinghao"); var guige = document.getElementById("guige"); var num = document.getElementById("nume"); var date = document.getElementById("date"); var danwei = document.getElementById("danwei"); var pname = document.getElementById("pname"); //非空 if(name.value == '' && xinghao.value == ''&&guige.value==''&&num.value==''&&date.value==''&&danwei.value==''&&pname.value=='') { alert('请填写一个条件'); return false; } } </script> </body> </html>
<%@ 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; 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> </tr> <!-- forEach遍历出adminBeans --> <c:forEach items="${courses2}" var="item" varStatus="status"> <tr> <td>${item.id}</td> <td>${item.name}</td> <td>${item.xinghao}</td> <td>${item.guige}</td> <td>${item.num}</td> <td>${item.date}</td> <td>${item.danwei}</td> <td>${item.pname}</td> </tr> </c:forEach> </table> </div> </body> </html>
以上就所有的代码内容。之后是我的商品增删改查的截图。