标签:query port -- trace 实体 imp cal seda bsp
首先建两张数据表,一张用来商品的增删改查,一张记录商品的出库与入库信息
建两个实体类,用来传递商品与单据的信息
在添加与删除时,填写入库或者出库单据,入库时增加商品,出库时删除商品。course类写商品的增删改查,另一个dao类只写单据的添加,自后将两张表连接起来
package com.hjf.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 鏁版嵁搴撹繛鎺ュ伐鍏? * @author Hu * */ public class DBUtil { public static String db_url = "jdbc:mysql://localhost:3306/ware?useSSL=false&useUnicode=true&characterEncoding=UTF-8"; public static String db_user = "root"; public static String db_pass = "15568958907lx"; 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(); } } } }
package com.hjf.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.hjf.dao.CourseDao; import com.hjf.entity.Course; import com.hjf.service.CourseService; @WebServlet("/CourseServlet") public class CourseServlet extends HttpServlet { private static final long serialVersionUID = 1L; CourseDao dao = new CourseDao(); /** * 方法选择 */ protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("add".equals(method)) { add(req, resp); } else if ("del".equals(method)) { del(req, resp); } else if ("update".equals(method)) { update(req, resp); } else if ("search".equals(method)) { search(req, resp); } else if ("getcoursebyid".equals(method)) { getCourseById(req, resp); } else if ("getcoursebyname".equals(method)) { getCourseByName(req, resp); } else if ("list".equals(method)) { list(req, resp); } } /** * 添加 * @param req * @param resp * @throws IOException * @throws ServletException */ private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); //获取数据 String name = req.getParameter("name"); String factory = req.getParameter("factory"); String model = req.getParameter("model"); String spec = req.getParameter("spec"); Course course = new Course(name, factory, model,spec); //添加后消息显示 if(dao.add(course)) { req.setAttribute("message", "添加成功"); req.getRequestDispatcher("add.jsp").forward(req,resp); } else { req.setAttribute("message", "名称重复,请重新录入"); req.getRequestDispatcher("add.jsp").forward(req,resp); } } /** * 全部 * @param req * @param resp * @throws ServletException */ private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); List<Course> courses = dao.list(); req.setAttribute("courses", courses); req.getRequestDispatcher("list.jsp").forward(req,resp); } /** * 通过ID得到Course * @param req * @param resp * @throws ServletException */ private void getCourseById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); Course course = dao.getCourseById(id); req.setAttribute("course", course); req.getRequestDispatcher("detail2.jsp").forward(req,resp); } /** * 通过名字查找 * 跳转至删除 * @param req * @param resp * @throws IOException * @throws ServletException */ private void getCourseByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); Course course = dao.getCourseByName(name); if(course == null) { req.setAttribute("message", "查无此课程!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } else { req.setAttribute("course", course); req.getRequestDispatcher("detail.jsp").forward(req,resp); } } /** * 删除 * @param req * @param resp * @throws IOException * @throws ServletException */ private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); dao.delete(id); req.setAttribute("message", "删除成功!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } /** * 修改 * @param req * @param resp * @throws IOException * @throws ServletException */ private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); String name = req.getParameter("name"); String factory = req.getParameter("factory"); String model = req.getParameter("model"); String spec = req.getParameter("spec"); Course course = new Course(id, name, factory, model,spec); dao.update(course); req.setAttribute("message", "修改成功"); req.getRequestDispatcher("CourseServlet?method=list").forward(req,resp); } /** * 查找 * @param req * @param resp * @throws ServletException */ private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); String factory = req.getParameter("factory"); String model = req.getParameter("model"); String spec = req.getParameter("spec"); List<Course> courses = dao.search(name, factory, model,spec); req.setAttribute("courses", courses); req.getRequestDispatcher("searchlist.jsp").forward(req,resp); } }
package com.hjf.entity; public class Course { private int id; private String name; private String factory; private String model; private String spec; 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 getfactory() { return factory; } public void setTeacher(String factory) { this.factory = factory; } public String getmodel() { return model; } public void setClassroom(String model) { this.model = model; } public void setspec(String spec){ this.spec=spec; } public String getspec(){ return spec; } public Course() {} public Course(int id, String name, String teacher, String classroom,String spec) { this.id = id; this.name = name; this.factory = factory; this.model = model; this.spec=spec; } public Course(String name, String teacher, String classroom,String spec) { this.name = name; this.factory = factory; this.model = model; this.spec=spec; } }
package com.hjf.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 com.hjf.entity.Course; import com.hjf.util.DBUtil; /** * 璇剧▼Dao * Dao灞傛搷浣滄暟鎹? * @author Hu * */ public class CourseDao { /** * 娣诲姞 * @param course * @return */ public boolean add(Course course) { String sql = "insert into ware(name, factory, model ,spec) values(‘" + course.getName() + "‘,‘" + course.getfactory() + "‘,‘" + course.getmodel() + "‘,‘"+course.getspec() + "‘)"; Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 鍒犻櫎 * * @param id * @return */ public boolean delete (int id) { boolean f = false; String sql = "delete from ware where id=‘" + id + "‘"; Connection conn = DBUtil.getConn(); Statement state = null; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 淇敼 * @param name * @param pass */ public boolean update(Course course) { String sql = "update ware set name=‘" + course.getName() + "‘,factory=‘" + course.getfactory() + "‘, model=‘" + course.getmodel() + "‘, spec=‘" + course.getspec() + "‘ where id=‘" + course.getId() + "‘"; Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 楠岃瘉璇剧▼鍚嶇О鏄惁鍞竴 * true --- 涓嶅敮涓? * @param name * @return */ public boolean name(String name) { boolean flag = false; String sql = "select name from ware where name = ‘" + name + "‘"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { flag = true; } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return flag; } /** * 閫氳繃ID寰楀埌绫? * @param id * @return */ public Course getCourseById(int id) { String sql = "select * from ware 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 factory = rs.getString("factory"); String model = rs.getString("model"); String spec = rs.getString("spec"); course = new Course(id, name, factory, model,spec); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return course; } /** * 閫氳繃name寰楀埌Course * @param name * @return */ public Course getCourseByName(String name) { String sql = "select * from ware 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 factory = rs.getString("factory"); String model = rs.getString("model"); String spec = rs.getString("spec"); course = new Course(id, name, factory, model,spec); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return course; } /** * 鏌ユ壘 * @param name * @param teacher * @param classroom * @return */ public List<Course> search(String name, String factory, String model,String spec) { String sql = "select * from ware where "; if (name != "") { sql += "name like ‘%" + name + "%‘"; } if (factory != "") { sql += "factory like ‘%" + factory + "%‘"; } if (model != "") { sql += "model like ‘%" + model + "%‘"; } if (spec != "") { sql += "spec like ‘%" + spec + "%‘"; } 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 factory2 = rs.getString("factory"); String model2 = rs.getString("model"); String spec2 = rs.getString("spec"); bean = new Course(id, name2, factory2, model2,spec2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } /** * 鍏ㄩ儴鏁版嵁 * @param name * @param teacher * @param classroom * @return */ public List<Course> list() { String sql = "select * from ware"; 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 factory2 = rs.getString("factory"); String model2 = rs.getString("model"); String spec2 = rs.getString("spec"); bean = new Course(id, name2, factory2, model2,spec2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return 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> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 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=add" method="post" onsubmit="return check()"> <div class="a"> 名称<input type="text" id="name" name="name"/> </div> <div class="a"> 生产商家<input type="text" id="factory" name="factory" /> </div> <div class="a"> 型号<input type="text" id="model" name="model" /> </div> <div class="a"> 规格<input type="text" id="spec" name="spec"/> </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 factory = document.getElementById("factory"); var model = document.getElementById("model"); var spec = document.getElementById("spec"); //非空 if(name.value == ‘‘) { alert(‘名称为空‘); name.focus(); return false; } if(factory.value == ‘‘) { alert(‘生产商家为空‘); factory.focus(); return false; } if(model.value == ‘‘) { alert(‘型号为空‘); model.focus(); return false; } if(spec.value == ‘‘) { alert(‘规格为空‘); spec.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; width: 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; width: 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>${ware.name}</td> </tr> <tr> <td>生产商家</td> <td>${ware.factory}</td> </tr> <tr> <td>型号</td> <td>${ware.model}</td> </tr> <tr> <td>规格</td> <td>${ware.spec}</td> </tr> </table> <div class="a"> <a onclick="return check()" href="CourseServlet?method=del&id=${ware.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; width: 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="${ware.name}"/> </div> <div class="a"> 生产商家<input type="text" id="factory" name="factory" value="${ware.factory}"/> </div> <div class="a"> 型号<input type="text" id="model" name="model" value="${ware.model}"/> </div> <div class="a"> 规格<input type="text" id="spec" name="spec" value="${ware.spec}"/> </div> <input type="hidden" id="id" name="id" value="${ware.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 factory = document.getElementById("factory"); var model = document.getElementById("model"); var spec = document.getElementById("spec"); //非空 if(name.value == ‘‘) { alert(‘名称为空‘); name.focus(); return false; } if(factory.value == ‘‘) { alert(‘生产商家为空‘); factory.focus(); return false; } if(model.value == ‘‘) { alert(‘型号为空‘); model.focus(); return false; } if(spec.value == ‘‘) { alert(‘规格为空‘); spec.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>首页</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> </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; width: 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>规格</td> <td align="center" colspan="2">操作</td> </tr> <c:forEach items="${wares}" var="item"> <tr> <td>${item.id}</td> <td>${item.name}</td> <td>${item.factory}</td> <td>${item.model}</td> <td>${item.spec}</td> <td><a href="CourseServlet?method=getcoursebyid&id=${item.id}">修改</a></td> </tr> </c:forEach> </table> </div> </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; width: 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="factory" name="factory" /> </div> <div class="a"> 型号<input type="text" id="model" name="model" /> </div> <div class="a"> 规格<input type="text" id="spec" name="spec" /> </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 factory = document.getElementById("factory"); var model = document.getElementById("model"); var spec = document.getElementById("spec"); //非空 if(name.value == ‘‘ && factory.value == ‘‘ && model.value == ‘‘ && spec.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; width: 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> </tr> <!-- forEach遍历出adminBeans --> <c:forEach items="${wares}" var="item" varStatus="status"> <tr> <td>${item.id}</td> <td><a>${item.name}</a></td> <td>${item.factory}</td> <td>${item.model}</td> <td>${item.spec}</td> </tr> </c:forEach> </table> </div> </body> </html>
标签:query port -- trace 实体 imp cal seda bsp
原文地址:https://www.cnblogs.com/ljm-zsy/p/10116972.html