实验要求:
本次实验花费了很多时间,主要问题还是习惯的去改代码,而不是去写,最后改的自己很迷糊,程序也很多的错误,跟老师说的一样,越改越难,错误越多,但这次也得到了一个教训,那就是还得自己构思,虽说系统间有相同的内容,很类似,但是一味的更改代码还是很麻烦的,在今后的学习中,要避免这种错误。
而这次实验费时比较多的还是servlet比较费时,自己掌握的还是不够,在这边费时巨多,建立完数据库之后就开始写javabean,在servlet的编码过程中遇到很多的问题,归根结底还是对servlet掌握不牢固,下课后,一些问题经过百度,询问舍友,最终自己重新书写了一份。
功能设计:
拿到题目后,觉得很简单,只是涉及到单条数据的数据库的增删改查,然后自己就拿起上次课的代码开始胡乱改造……结果……不说了
1.分析题目,建立数据库
通过题目可以看出来,需要两个数据库表,一个存放商品信息,另一个存放出入库单据
2。jdbc数据库连接
这一部分直接拿出之前的代码就ok,没有什么问题
3.根据数据库建立两个javabean
最基础的,这要是再不会,估计就得退学了
4.建立dao操作
主要就是涉及到功能的实现,包括:货物入库对货物进行添加,同时交易记录进行添加,检查商品名和日期是否存在和根据关键词进行查找
5.建立servlet
这一部分可是占用了好多时间,主要是数据的传递,然后调用dao的函数,对数据库进行操作
6.建立界面
准备就绪后就开始界面的搭建,这个用了之前的界面
主页,不知道用了多少次了,几乎每次都用这个,还挺好用
商品入库,分为两布,先输入商品信息,然后在填表单,之前是直接填表单,然后分别存到两个表,看舍友这么写的,觉得挺好,就copy过来了
看着效果确实不错,剩下的就是功能页(图片上传不全)。
部分功能代码
package dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import bean.Housebean; import bean.Housebean; import dbutil.DButil; public class Dao { public boolean checkdate(String date) { boolean flag = true; // 查询用户是否已存在 Connection connection = DButil.getConn(); Statement statement = null; ResultSet rs = null; try { statement = connection.createStatement(); rs = statement.executeQuery("select * from documents");// 数据库结果集 while (rs.next()) { if (date.equals(rs.getString("date"))) { flag = false; } } } catch (Exception e) { e.printStackTrace(); } finally { DButil.close(rs, statement, connection); } return flag; }// 检查date是否存在 public boolean checkReg(String name) { boolean flag = true; // 查询用户是否已存在 Connection connection = DButil.getConn(); Statement statement = null; ResultSet rs = null; try { statement = connection.createStatement(); rs = statement.executeQuery("select * from goods");// 数据库结果集 while (rs.next()) { if (name.equals(rs.getString("name"))) { flag = false; } } } catch (Exception e) { e.printStackTrace(); } finally { DButil.close(rs, statement, connection); } return flag; }// 检查商品名是否存在 public void Goodsinsert(String name, String manufacturer, String model, String specifications) { Connection connnection = DButil.getConn(); ResultSet rs = null; Statement statement = null; try { String sql = "insert into goods(name,shop,xinghao,guige) values('" + name + "','" + manufacturer + "','" + model + "','" + specifications + "')"; statement = connnection.prepareStatement(sql); statement.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { DButil.close(rs, statement, connnection); } } public void Documentsinsert(String name, String manufacturer, String model, String specifications, int amount, String date, String time, String entity, String deliveryman) { Connection connnection = DButil.getConn(); ResultSet rs = null; Statement statement = null; try { String sql = "insert into documents(name,shop,xinghao,guige,num,date,time,inname,outname) values('" + name + "','" + manufacturer + "','" + model + "','" + specifications + "','" + amount + "','" + date + "','" + time + "','" + entity + "','" + deliveryman + "')"; statement = connnection.prepareStatement(sql); statement.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { DButil.close(rs, statement, connnection); } } public void Delete(String name) { ResultSet rs = null; Statement statement = null; Connection connection = DButil.getConn(); try { String sql = "delete from goods where name='" + name + "'"; statement = connection.createStatement(); statement.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { DButil.close(rs, statement, connection); } } public Housebean selectname(String sname) { Housebean selectbean = new Housebean(); Connection connection = DButil.getConn(); Statement statement = null; ResultSet rs = null; try { statement = connection.createStatement(); // 要执行的SQL语句 String sql = "select * from documents where name='" + sname + "'"; // 结果集 rs = statement.executeQuery(sql); while (rs.next()) { String name = rs.getString("name"); String manufacturer = rs.getString("shop"); String model = rs.getString("xinghao"); String specifications = rs.getString("guige"); int amount = rs.getInt("num"); String date = rs.getString("date"); String time = rs.getString("time"); String entity = rs.getString("inname"); String deliveryman = rs.getString("outname"); selectbean.setManufacturer(manufacturer); selectbean.setModel(model); selectbean.setName(name); selectbean.setSpecifications(specifications); } } catch (Exception e) { e.printStackTrace(); } finally { DButil.close(rs, statement, connection); } return selectbean; } public Housebean selectdate(String date) { Housebean selectbean = new Housebean(); Connection connection = DButil.getConn(); Statement statement = null; ResultSet rs = null; try { statement = connection.createStatement(); // 要执行的SQL语句 String sql = "select * from documents where date='" + date + "'"; // 结果集 rs = statement.executeQuery(sql); while (rs.next()) { String name = rs.getString("name"); String manufacturer = rs.getString("shop"); String model = rs.getString("xinghao"); String specifications = rs.getString("guige"); selectbean.setManufacturer(manufacturer); selectbean.setModel(model); selectbean.setName(name); selectbean.setSpecifications(specifications); } } catch (Exception e) { e.printStackTrace(); } finally { DButil.close(rs, statement, connection); } return selectbean; } }
package servlet; import dao.Dao; 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; /** * Servlet implementation class dateselect */ @WebServlet("/dateselect") public class dateselect extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public dateselect() { 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 response.setContentType("text/html;charset=utf8"); request.setCharacterEncoding("utf-8"); response.getWriter().append("Served at: ").append(request.getContextPath()); String date=request.getParameter("date"); Dao dao=new Dao(); if(!dao.checkdate(date)) { String name=dao.selectdate(date).getName(); String manufacturer=dao.selectdate(date).getManufacturer(); String model=dao.selectdate(date).getModel(); String specifications=dao.selectdate(date).getSpecifications(); request.setAttribute("name", name); request.setAttribute("manufacturer", manufacturer); request.setAttribute("model", model); request.setAttribute("specifications", specifications); request.getRequestDispatcher("selectsuccess.jsp").forward(request, response); } else request.getRequestDispatcher("selectfail.jsp").forward(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf8"); request.setCharacterEncoding("utf-8"); response.getWriter().append("Served at: ").append(request.getContextPath()); String name=request.getParameter("name"); String manufacturer=request.getParameter("manufacturer"); String model=request.getParameter("model"); String specifications=request.getParameter("specifications"); int amount=Integer.valueOf(request.getParameter("amount")); String date=request.getParameter("date"); String time=request.getParameter("time"); String entity=request.getParameter("entity"); String deliveryman=request.getParameter("deliveryman"); Dao dao=new Dao(); int check=0; if(dao.checkReg(name)) { check=1; dao.Documentsinsert(name, manufacturer, model, specifications, amount, date, time, entity, deliveryman); dao.Goodsinsert(name, manufacturer, model, specifications); } request.setAttribute("check", check); request.getRequestDispatcher("insuccess.jsp").forward(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf8"); request.setCharacterEncoding("utf-8"); response.getWriter().append("Served at: ").append(request.getContextPath()); String name=request.getParameter("name"); Dao dao=new Dao(); if(!dao.checkReg(name)) { String sname=dao.selectname(name).getName(); String manufacturer=dao.selectname(name).getManufacturer(); String model=dao.selectname(name).getModel(); String specifications=dao.selectname(name).getSpecifications(); request.setAttribute("name", sname); request.setAttribute("manufacturer", manufacturer); request.setAttribute("model", model); request.setAttribute("specifications", specifications); request.getRequestDispatcher("selectsuccess.jsp").forward(request, response); } else request.getRequestDispatcher("selectfail.jsp").forward(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf8"); request.setCharacterEncoding("utf-8"); response.getWriter().append("Served at: ").append(request.getContextPath()); String name=request.getParameter("name"); Dao dao=new Dao(); int check=0; if(!dao.checkReg(name)) { check=1; dao.Delete(name); } request.setAttribute("check", check); request.getRequestDispatcher("outsuccess.jsp").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }