设计思路:先是创建两个表,一个用来操作库内商品的增删改查,一个用来记录商品的流水信息。
设计过程:先对商品的属性进行创建javaBean编写,之后编写数据库连接类,之后编写数据库操作类,之后编写服务类,之后编写Servlet类,最后编写JSP,然后对web.xml进行写入
代码:Course.java
package com.zh.entity; public class Course { private int ID; private String name; private String mf; private String model; private String spec; public int get_ID() { return ID; } public void set_ID(int ID) { this.ID = ID; } public String get_name() { return name; } public void set_name(String name) { this.name=name; } public String get_mf() { return mf; } public void set_mf(String mf) { this.mf=mf; } public String get_model() { return model; } public void set_model(String model) { this.model=model; } public String get_spec() { return spec; } public void set_spec(String spec) { this.spec=spec; } public Course() {}; public Course(int ID,String name,String mf,String model,String spec) { this.ID=ID; this.name=name; this.mf=mf; this.model=model; this.spec=spec; } public Course(String name,String mf,String model,String spec) { this.name=name; this.mf=mf; this.model=model; this.spec=spec; } }
代码:DButil.java
package com.zh.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 Connection getConnection(){ Connection conn = null; try { conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/saler?serverTimezone=UTC","root","hao19990507."); System.out.println("连接成功"); }catch (Exception e){ e.printStackTrace(); } return conn; } public static void close(Statement sta,Connection con){ if (sta != null) { try { sta.close(); } catch (SQLException e) { e.printStackTrace(); } } if (con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(ResultSet rs,Statement sta,Connection con){ if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (sta != null) { try { sta.close(); } catch (SQLException e) { e.printStackTrace(); } } if (con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
代码:CourseDao.java
package com.zh.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.zh.entity.Course; import com.zh.util.DButil; public class CourseDao { public void main(String []args) { } /*增加 * */ public boolean add(Course Bean) { String sql="insert into goods(name,mf,model,spec) values(‘"+Bean.get_name()+"‘,‘"+Bean.get_mf()+"‘,‘"+Bean.get_model()+"‘,‘"+Bean.get_spec()+"‘)"; Connection conn = null; Statement state = null; boolean f = false; int a=0; try { conn=DButil.getConnection(); state = conn.createStatement(); state.execute(sql); a=state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { DButil.close(state, conn); } if (a>0) { f = true; } return f; } /*删除 * */ public boolean deleteByID(int ID) { boolean f=false; String sql="delete from goods where ID=‘"+ID+"‘"; Connection conn=DButil.getConnection(); Statement state = null; int a = 0; try { state = conn.createStatement(); state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DButil.close(state, conn); } if (a > 0) { f = true; } return f; } /* 修改 * */ public boolean update(Course b) { String sql="updata goods set name=‘"+b.get_name()+"‘, mf=‘"+b.get_mf()+"‘,model=‘"+b.get_model()+"‘ where ID=‘"+b.get_ID()+ "‘"; boolean f = false; int a = 0; Connection conn=DButil.getConnection(); Statement state = null; try { state = conn.createStatement(); state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DButil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 通过ID获得一个bean对象 */ public Course findByID(int ID) { Connection conn=null; Statement state=null; ResultSet rs=null; Course u=null; try { conn=DButil.getConnection(); String sql="select*from goods where ID=‘"+ID+"‘"; state=conn.createStatement(); rs=state.executeQuery(sql); u=new Course(); while(rs.next()) { String name=rs.getString("name"); String mf=rs.getString("mf"); String model=rs.getString("model"); String spec=rs.getString("spec"); u.set_ID(ID); u.set_name(name); u.set_mf(mf); u.set_model(model); u.set_spec(spec); } }catch (Exception e) { e.printStackTrace(); } finally { DButil.close(rs, state, conn); } return u; } /* * 通过name获得一个类对象 */ public Course findByName(String name) { String sql = "select * from goods where name =‘" + name + "‘"; Connection conn = DButil.getConnection(); 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 name1 = rs.getString("name"); String mf = rs.getString("mf"); String model=rs.getString("model"); String spec=rs.getString("spec"); course = new Course(id, name1, mf, model,spec); } } catch (Exception e) { e.printStackTrace(); } finally { DButil.close(rs, state, conn); } return course; } /* * 查找 */ public List<Course> search(String name, String mf, String model,String spec) { String sql = "select * from goods where "; if (name != "") { sql += "name like ‘%" + name + "%‘"; } if (mf != "") { sql += "teacher like ‘%" + mf + "%‘"; } if (model != "") { sql += "classroom like ‘%" + model + "%‘"; } if (spec != "") { sql += "classroom like ‘%" + spec + "%‘"; } List<Course> list = new ArrayList<>(); Connection conn = DButil.getConnection(); 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 name1 = rs.getString("name"); String mf1 = rs.getString("mf"); String model1 = rs.getString("model"); String spec1=rs.getString("spec"); bean = new Course(ID, name1,mf1,model1, spec1); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DButil.close(rs, state, conn); } return list; } /* * 显示全部 */ public List<Course> list() { String sql = "select * from goods"; List<Course> list = new ArrayList<>(); Connection conn = DButil.getConnection(); 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 name1 = rs.getString("name"); String mf1 = rs.getString("mf"); String model1 = rs.getString("model"); String spec1=rs.getString("spec"); bean = new Course(ID, name1,mf1,model1, spec1); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DButil.close(rs, state, conn); } return list; } }
代码:Courseservice.java
package com.zh.service; import java.util.List; import com.zh.dao.CourseDao; import com.zh.entity.Course; /** * CourseService * 服务层 * @author * */ public class CourseService { //CourseDao cDao = new CourseDao(); /** * 添加 * @param course * @return */ public boolean add(Course course) { boolean f = false; try { CourseDao cDao = new CourseDao(); cDao.add(course); f = true; }catch (Exception e) { e.printStackTrace(); } return f; } /** * 删除 */ public void del(int id) { CourseDao cDao = new CourseDao(); cDao.deleteByID(id); } /** * 修改 * @return */ public void update(Course course) { CourseDao cDao = new CourseDao(); cDao.update(course); } /** * 通过ID得到一个Course * @return */ public Course getCourseByID(int ID) { CourseDao cDao = new CourseDao(); return cDao.findByID(ID); } /** * 通过name得到一个Course * @return */ public Course getCourseByName(String name) { CourseDao cDao = new CourseDao(); return cDao.findByName(name); } /** * 查找 * @return */ public List<Course> search(String name, String mf, String model,String spec) { CourseDao cDao = new CourseDao(); return cDao.search(name, mf, model,spec); } /** * 全部数据 * @return */ public List<Course> list() { CourseDao cDao = new CourseDao(); return cDao.list(); } }
代码:CourseServlet.java
package com.zh.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.zh.dao.CourseDao; import com.zh.entity.Course; import com.zh.service.CourseService; public class CourseServlet extends HttpServlet { private static final long serialVersionUID = 1L; CourseService service = new CourseService(); CourseDao cdao=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 mf = req.getParameter("mf"); String model = req.getParameter("model"); String spec = req.getParameter("spec"); Course course = new Course(name, mf, model,spec); //添加后消息显示 if(cdao.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 = service.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 = service.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 = 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); } } /** * 删除 * @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")); service.del(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 mf = req.getParameter("mf"); String model = req.getParameter("model"); String spec=req.getParameter("spec"); Course course = new Course( ID,name,mf, model, spec); service.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 mf = req.getParameter("mf"); String model = req.getParameter("model"); String spec=req.getParameter("spec"); List<Course> courses = service.search(name, mf, model,spec); req.setAttribute("courses", courses); req.getRequestDispatcher("searchlist.jsp").forward(req,resp); } }
代码: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; 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="mf" name="mf" /> </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> </body> </html>
代码:del.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; 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> </body> </html>
代码:detail.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; 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>${course.name}</td> </tr> <tr> <td>生产厂家</td> <td>${course.mf}</td> </tr> <tr> <td>商品型号</td> <td>${course.model}</td> </tr> <tr> <td>商品规格</td> <td>${course.spec}</td> </tr> </table> <div class="a"> <a onclick="return check()" href="CourseServlet?method=del&id=${course.ID}">删 除</a> </div> </div> </body> </html>
代码:detail2.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; 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="${course.name}"/> </div> <div class="a"> 生产厂家<input type="text" id="mf" name="mf" value="${course.mf}"/> </div> <div class="a"> 商品型号<input type="text" id="model" name="model" value="${course.model}"/> </div> <div class="a"> 商品规格<input type="text" id="spec" name="spec" value="${course.spec}"/> </div> <input type="hidden" id="ID" name="ID" value="${course.ID}"/> <div class="a"> <button type="submit" class="b">修 改</button> </div> </form> </div> </body> </html>
代码: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> </body> </html>
代码:list.jsp
<%@ 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="${courses}" var="item"> <tr> <td>${item.ID}</td> <td>${item.name}</td> <td>${item.mf}</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>
代码:search.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; 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="mf" name="mf" /> </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> </body> </html>
代码:searchlist.jsp
<%@ 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="${courses}" var="item" varStatus="status"> <tr> <td><a>${item.name}</a></td> <td>${item.mf}</td> <td>${item.model}</td> <td>${item.spec}</td> </tr> </c:forEach> </table> </div> </body> </html>
代码:web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>dyjgj</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>CourseServlet </servlet-name> <servlet-class>com.zh.servlet.CourseServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CourseServlet</servlet-name> <url-pattern>/CourseServlet</url-pattern> </servlet-mapping> </web-app>
原文地址:https://www.cnblogs.com/zhang188660586/p/10117029.html
时间: 2024-11-08 06:13:35