一、实验思路
在数据库中建立两个表,flow表存放出入库的单据,Warehouse表存放商品的信息。FlowBean类和WarehouseBean类处理所用到的变量;通过FlowDao类和WarehouseDao类进行连接;通com.servlet包中的类进行增删改查的操作.
结构及数据库表如下图所示:
二、源代码
(1)jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录界面</title> </head> <body> <table width = "400" border ="0" height = "300" align = "center" cellpading ="0" style = "font-size:12px;color:#FFFFF;front-weight:bold"> <Tr height = "50" ><td> </td></Tr> <tr> <td height = "200"><table width = "98%" border ="0" align = "center" cellpading = "0"> <form name = "form1" action = "addServlet" method = "post"> <tr> <td>商品名称:</td><td><input type="text" name="addname" /></td> </tr> <tr> <td>生产厂家:</td><td><input type="text" name="addfactory" /></td> </tr> <tr> <td>型号:</td><td><input type="text" name="addmodel" /></td> </tr> <tr> <td>规格:</td><td><input type="text" name="addstandard" /></td> </tr> <tr> <td>数量:</td><td><input type="text" name="addnumber" /></td> </tr> <tr> <td>入库单位名称:</td><td><input type="text" name="adddanwei" /></td> </tr> <tr> <td>送货人姓名:</td><td><input type="text" name="addperson" /></td> </tr> <tr> <td><input name = "login" type = "submit" id = "login" value = "提交" class = "button"></td> </tr> </form> </table> </td> </tr> </table> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录界面</title> </head> <body> <table width = "400" border ="0" height = "300" align = "center" cellpading ="0" style = "font-size:12px;color:#FFFFF;front-weight:bold"> <Tr height = "50" ><td> </td></Tr> <tr> <td height = "200"><table width = "98%" border ="0" align = "center" cellpading = "0"> <form name = "form1" action = "AmendServlet" method = "post"> <tr> <td>商品名称:</td><td><input type="text" name="Amendname" /></td> </tr> <tr> <td>生产厂家:</td><td><input type="text" name="Amendfactory" /></td> </tr> <tr> <td>型号:</td><td><input type="text" name="Amendmodel" /></td> </tr> <tr> <td>规格:</td><td><input type="text" name="Amendstandard" /></td> </tr> <tr> <td>数量:</td><td><input type="text" name="Amendnumber" /></td> </tr> <tr> <td>出库单位名称:</td><td><input type="text" name="Amenddanwei" /></td> </tr> <tr> <td>提货人姓名:</td><td><input type="text" name="Amendperson" /></td> </tr> <tr> <td><input name = "login" type = "submit" id = "login" value = "提交" class = "button"></td> </tr> </form> </table> </td> </tr> </table> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录界面</title> </head> <body> <table width = "400" border ="0" height = "300" align = "center" cellpading ="0" style = "font-size:12px;color:#FFFFF;front-weight:bold"> <Tr height = "50" ><td> </td></Tr> <tr> <td height = "200"><table width = "98%" border ="0" align = "center" cellpading = "0"> <form name = "form2" action = "AmendWareServlet" method = "post"> <tr> <td>要修改商品的名称:</td><td><input type="text" name="Amendname" /></td> </tr> <tr> <td>改后商品名称:</td><td><input type="text" name="amendname" /></td> </tr> <tr> <td>生产厂家:</td><td><input type="text" name="amendfactory" /></td> </tr> <tr> <td>型号:</td><td><input type="text" name="amendmodel" /></td> </tr> <tr> <td>规格:</td><td><input type="text" name="amendstandard" /></td> </tr> <tr> <td><input name = "login" type = "submit" id = "login" value = "保存" class = "button"></td> </tr> </form> </table> </td> </tr>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.util.List" %> <%@page import="com.bean.WarehouseBean" %> <%@page import="com.dao.WarehouseDao" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录界面</title> </head> <body> <table width = "400" border ="0" height = "300" align = "center" cellpading ="0" style = "font-size:12px;color:#FFFFF;front-weight:bold"> <Tr height = "50" ><td> </td></Tr> <tr> <td height = "200"><table width = "98%" border ="0" align = "center" cellpading = "0"> <form > <tr> <td>欢迎使用本系统</td> </tr> <tr> <% request.setCharacterEncoding("UTF-8"); String name=request.getParameter("Selectname"); WarehouseBean c=null; List<WarehouseBean> beans = WarehouseDao.select(name); for(int i=0;i<beans.size();i++) { c=beans.get(i); out.println("</br>"); out.println("商品名称="+c.getName()+",生产厂家="+c.getFactory()+",型号="+c.getModel()+",规格="+c.getStandard()); } %> </tr> <tr> <td><a href="jiemian.jsp">返回</a></td> </tr> <tr> <td><a href="jieshu.jsp">结束</a></td> </tr> </form> </table> </td> </tr> </table> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.util.List" %> <%@page import="com.bean.WarehouseBean" %> <%@page import="com.bean.FlowBean" %> <%@page import="com.dao.WarehouseDao" %> <%@page import="com.dao.FlowDao" %> <%@page import="java.sql.Timestamp" %> <%@page import="java.text.SimpleDateFormat" %> <%@page import="java.util.Date" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录界面</title> </head> <body> <table width = "400" border ="0" height = "300" align = "center" cellpading ="0" style = "font-size:12px;color:#FFFFF;front-weight:bold"> <Tr height = "50" ><td> </td></Tr> <tr> <td height = "200"><table width = "98%" border ="0" align = "center" cellpading = "0"> <form > <tr> <td>欢迎使用本系统</td> </tr> <tr> <% request.setCharacterEncoding("UTF-8"); String time1=request.getParameter("time1"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = sdf.parse("2016-06-06 16:24:50"); FlowBean c=null; int a=0; List<FlowBean> beans = FlowDao.select(); a=beans.size(); for(int i=0;i<beans.size();i++) { c=beans.get(i); if(c.getDate().getTime()>date.getTime()) {out.println("</br>"); out.println("商品名称="+c.getName()+",生产厂家="+c.getFactory()+",型号="+c.getModel()+",规格="+c.getStandard()); } } %> </tr> <tr> <td><a href="jiemian.jsp">返回</a></td> </tr> <tr> <td><a href="jieshu.jsp">结束</a></td> </tr> </form> </table> </td> </tr> </table> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录界面</title> </head> <body> <table width = "400" border ="0" height = "300" align = "center" cellpading ="0" style = "font-size:12px;color:#FFFFF;front-weight:bold"> <Tr height = "50" ><td> </td></Tr> <tr> <td height = "200"><table width = "98%" border ="0" align = "center" cellpading = "0"> <form name = "form1" action = "DeleteWareServlet" method = "post"> <tr> <td>请输入要删除的商品名称:</td><td><input type="text" name="Deletename" /></td> </tr> <tr> <td><input name = "login" type = "submit" id = "login" value = "提交" class = "button"></td> </tr> </form> </table> </td> </tr> </table> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录界面</title>
</head>
<body>
<table width = "400" border ="0" height = "300" align = "center" cellpading ="0" style = "font-size:12px;color:#FFFFF;front-weight:bold">
<Tr height = "50" ><td> </td></Tr>
<tr>
<td height = "200"><table width = "98%" border ="0" align = "center" cellpading = "0">
<form >
<tr>
<td>欢迎使用本系统</td>
</tr>
<tr>
<td>请选择功能:</td>
</tr>
<tr>
<td><a href="addWare.jsp">商品增加</a></td>
</tr>
<tr>
<td><a href="DeleteWare.jsp">商品删除</a></td>
</tr>
<tr>
<td><a href="AmendWare.jsp">商品修改</a></td>
</tr>
<tr>
<td><a href="Selectware.jsp">商品查找</a></td>
</tr>
<tr>
<td><a href="add.jsp">入库</a></td>
</tr>
<tr>
<td><a href="Amend.jsp">出库</a></td>
</tr>
<tr>
<td><a href="Select.jsp">查询</a></td>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录界面</title>
</head>
<body>
<table width = "400" border ="0" height = "300" align = "center" cellpading ="0" style = "font-size:12px;color:#FFFFF;front-weight:bold">
<Tr height = "50" ><td> </td></Tr>
<tr>
<td height = "200"><table width = "98%" border ="0" align = "center" cellpading = "0">
<form >
<tr>
<td>欢迎使用本系统</td>
</tr>
<tr>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录界面</title> </head> <body> <table width = "400" border ="0" height = "300" align = "center" cellpading ="0" style = "font-size:12px;color:#FFFFF;front-weight:bold"> <Tr height = "50" ><td> </td></Tr> <tr> <td height = "200"><table width = "98%" border ="0" align = "center" cellpading = "0"> <form name = "form1" action = "chuli1.jsp" method = "post"> <tr> <td>请输入时间:</td><td><input type="text" name="time1" /></td> </tr> <tr> <td><input name = "login" type = "submit" id = "login" value = "提交" class = "button"></td> </tr> </form> </table> </td> </tr> </table> </html>
<td>请选择功能:</td> </tr> <tr> <td><a href="Selectware.jsp">商品名称查询</a></td> </tr> <tr> <td><a href="Selectdate.jsp">出入库日期查询</a></td> </tr> </form> </table> </td> </tr> </table> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录界面</title> </head> <body> <table width = "400" border ="0" height = "300" align = "center" cellpading ="0" style = "font-size:12px;color:#FFFFF;front-weight:bold"> <Tr height = "50" ><td> </td></Tr> <tr> <td height = "200"><table width = "98%" border ="0" align = "center" cellpading = "0"> <form name = "form1" action = "addWareServlet" method = "post"> <tr> <td>商品名称:</td><td><input type="text" name="addname" /></td> </tr> <tr> <td>生产厂家:</td><td><input type="text" name="addfactory" /></td> </tr> <tr> <td>型号:</td><td><input type="text" name="addmodel" /></td> </tr> <tr> <td>规格:</td><td><input type="text" name="addstandard" /></td> </tr> <tr> <td><input name = "login" type = "submit" id = "login" value = "提交" class = "button"></td> </tr> </form> </table> </td> </tr> </table> </html>
(2)Dbutil文件
package com.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/cangku001?characterEncoding=utf8&useSSL=true" ; public static String db_user = "root"; public static String db_password = "wxy2724921017"; public static Connection getConn() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(db_url, db_user, db_password); } catch (Exception e) { e.printStackTrace(); } return 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(); } } } }
(3)bean文件
package com.bean; import java.util.Date; public class FlowBean { private String name; private String factory; private String model; private String standard; private int number; private String danwei; private String person; private Date date; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getFactory() { return factory; } public void setFactory(String factory) { this.factory = factory; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public String getStandard() { return standard; } public void setStandard(String standard) { this.standard = standard; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getDanwei() { return danwei; } public void setDanwei(String danwei) { this.danwei = danwei; } public String getPerson() { return person; } public void setPerson(String person) { this.person = person; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } }
package com.bean; public class WarehouseBean { private String name; private String factory; private String model; private String standard; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getFactory() { return factory; } public void setFactory(String factory) { this.factory = factory; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public String getStandard() { return standard; } public void setStandard(String standard) { this.standard = standard; } }
(4)dao文件
package com.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.bean.FlowBean; import com.bean.WarehouseBean; import com.util.DBUtil; public class FlowDao { public static int addFlow(String name,String factory,String model,String standard,int number,Timestamp date,String danwei,String person) throws Exception//增加 { int count = 0; Connection conn = DBUtil.getConn(); try { String sql="insert into flow values(?,?,?,?,?,?,?,?)"; PreparedStatement preparedStatement=conn.prepareStatement(sql); preparedStatement.setString(1, name); preparedStatement.setString(2, factory); preparedStatement.setString(3, model); preparedStatement.setString(4, standard); preparedStatement.setInt(5, number); preparedStatement.setTimestamp(6, date); preparedStatement.setString(7, danwei); preparedStatement.setString(8, person); count=preparedStatement.executeUpdate(); conn.close(); }catch (Exception e) { e.printStackTrace(); } return count; } public static List<FlowBean> select() {//查找 List<FlowBean> beans = new ArrayList<FlowBean>(); Connection conn = DBUtil.getConn(); FlowBean adminBean=null; try { String sql="select * from Flow"; Statement state = conn.createStatement(); ResultSet rs = state.executeQuery(sql); while(rs.next()) { adminBean = new FlowBean(); adminBean.setName(rs.getString("name")); adminBean.setFactory(rs.getString("factory")); adminBean.setModel(rs.getString("model")); adminBean.setStandard(rs.getString("standard")); adminBean.setNumber(rs.getInt("number")); adminBean.setDate(rs.getTimestamp("date")); adminBean.setDanwei(rs.getString("danwei")); adminBean.setPerson(rs.getString("person")); beans.add(adminBean); } conn.close(); }catch (Exception e) { e.printStackTrace(); } return beans; /*List return results;*/ } }
package com.dao; import com.util.DBUtil; import com.bean.WarehouseBean; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class WarehouseDao { public static int addWarehouse(String name,String factory,String model,String standard) throws Exception//增加 { int count = 0; Connection conn = DBUtil.getConn(); try { String sql="insert into Warehouse values(?,?,?,?)"; PreparedStatement preparedStatement=conn.prepareStatement(sql); preparedStatement.setString(1, name); preparedStatement.setString(2, factory); preparedStatement.setString(3, model); preparedStatement.setString(4, standard); count=preparedStatement.executeUpdate(); conn.close(); }catch (Exception e) { e.printStackTrace(); } return count; } public int DeleteWare(String name) throws Exception//删除 { int count=0; Connection conn = DBUtil.getConn(); try { String sql="delete from Warehouse where name=?"; PreparedStatement preparedStatement=conn.prepareStatement(sql); preparedStatement.setString(1, name); count=preparedStatement.executeUpdate(); System.out.println(count); conn.close(); }catch (Exception e) { e.printStackTrace(); } return count; } public static List<WarehouseBean> select(String name) {//查找 List<WarehouseBean> beans = new ArrayList<WarehouseBean>(); Connection conn = DBUtil.getConn(); WarehouseBean adminBean=null; try { String sql="select * from Warehouse"; Statement state = conn.createStatement(); ResultSet rs = state.executeQuery(sql); while(rs.next()) { adminBean = new WarehouseBean(); adminBean.setName(rs.getString("name")); adminBean.setFactory(rs.getString("factory")); adminBean.setModel(rs.getString("model")); adminBean.setStandard(rs.getString("standard")); beans.add(adminBean); } conn.close(); }catch (Exception e) { e.printStackTrace(); } List<WarehouseBean> results = new ArrayList<WarehouseBean>(); if(name!="") { Pattern pattern = Pattern.compile(name); for(int i=0;i<beans.size();i++) { Matcher matcher = pattern.matcher(((WarehouseBean)beans.get(i)).getName()); if(matcher.find()){ results.add(beans.get(i)); } } } return results; } public static int testUpdate1(String xiugainame,String xiuname) throws Exception//修改 { int count=0; Connection conn = DBUtil.getConn(); try { String sql="update Warehouse set name=? where name=?"; PreparedStatement preparedStatement=conn.prepareStatement(sql); preparedStatement.setString(1, xiuname); preparedStatement.setString(2, xiugainame); count=preparedStatement.executeUpdate(); System.out.println(count); conn.close(); }catch (Exception e) { e.printStackTrace(); } return count; } public static int testUpdate2(String xiugainame,String xiufactory) throws Exception//修改 { int count=0; Connection conn = DBUtil.getConn(); try { String sql="update Warehouse set factory=? where name=?"; PreparedStatement preparedStatement=conn.prepareStatement(sql); preparedStatement.setString(1, xiufactory); preparedStatement.setString(2, xiugainame); count=preparedStatement.executeUpdate(); System.out.println(count); conn.close(); }catch (Exception e) { e.printStackTrace(); } return count; } public static int testUpdate3(String xiugainame,String xiumodel) throws Exception//修改 { int count=0; Connection conn = DBUtil.getConn(); try { String sql="update Warehouse set model=? where name=?"; PreparedStatement preparedStatement=conn.prepareStatement(sql); preparedStatement.setString(1, xiumodel); preparedStatement.setString(2, xiugainame); count=preparedStatement.executeUpdate(); System.out.println(count); conn.close(); }catch (Exception e) { e.printStackTrace(); } return count; } public static int testUpdate4(String xiugainame,String xiustandard) throws Exception//修改 { int count=0; Connection conn = DBUtil.getConn(); try { String sql="update Warehouse set standard=? where name=?"; PreparedStatement preparedStatement=conn.prepareStatement(sql); preparedStatement.setString(1, xiustandard); preparedStatement.setString(2, xiugainame); count=preparedStatement.executeUpdate(); System.out.println(count); conn.close(); }catch (Exception e) { e.printStackTrace(); } return count; } }
(5)servlet文件
package com.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.swing.JOptionPane; import java.sql.Timestamp; import java.util.Date; import com.bean.WarehouseBean; import com.dao.WarehouseDao; import com.util.DBUtil; import com.dao.FlowDao; @WebServlet("/addServlet") public class addServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String name=request.getParameter("addname"); String factory=request.getParameter("addfactory"); String model=request.getParameter("addmodel"); String standard=request.getParameter("addstandard"); String number=request.getParameter("addnumber"); String danwei=request.getParameter("adddanwei"); String person=request.getParameter("addperson"); Date date = new Date(); Timestamp timeStamp = new Timestamp(date.getTime()); int num = Integer.parseInt(number); int a=0,b=0; FlowDao flow=new FlowDao(); WarehouseDao ware=new WarehouseDao(); try { a=flow.addFlow(name, factory, model, standard, num, timeStamp, danwei, person); b=ware.addWarehouse(name, factory, model, standard); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if(a!=0&&b!=0) { int res=JOptionPane.showConfirmDialog(null, "操作成功", "是否继续", JOptionPane.YES_NO_OPTION); if(res==JOptionPane.YES_OPTION){ request.getRequestDispatcher("jiemian.jsp").forward(request, response); //点击“是”后执行这个代码块 }else{ System.out.println("选择否后执行的代码"); //点击“否”后执行这个代码块 return; } } else { JOptionPane.showMessageDialog(null, "操作错误", "请重新操作", JOptionPane.ERROR_MESSAGE); request.getRequestDispatcher("add.jsp").forward(request, response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.servlet; import java.io.IOException; import java.sql.Timestamp; import java.util.Date; 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 javax.swing.JOptionPane; import com.bean.WarehouseBean; import com.dao.WarehouseDao; import com.util.DBUtil; @WebServlet("/addWareServlet") public class addWareServlet extends HttpServlet { private static final long serialVersionUID = 1L; public addWareServlet() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String name=request.getParameter("addname"); String factory=request.getParameter("addfactory"); String model=request.getParameter("addmodel"); String standard=request.getParameter("addstandard"); Date date = new Date(); Timestamp timeStamp = new Timestamp(date.getTime()); int n=0; WarehouseDao ware=new WarehouseDao(); try { n=ware.addWarehouse(name, factory, model, standard); } catch (Exception e) { e.printStackTrace(); } if(n!=0) { int res=JOptionPane.showConfirmDialog(null, "操作成功", "是否继续", JOptionPane.YES_NO_OPTION); if(res==JOptionPane.YES_OPTION){ request.getRequestDispatcher("jiemian.jsp").forward(request, response); //点击“是”后执行这个代码块 }else{ System.out.println("选择否后执行的代码"); //点击“否”后执行这个代码块 return; } } else { JOptionPane.showMessageDialog(null, "操作错误", "请重新操作", JOptionPane.ERROR_MESSAGE); request.getRequestDispatcher("addWare.jsp").forward(request, response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.servlet; import java.io.IOException; import java.sql.Timestamp; import java.util.Date; 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 javax.swing.JOptionPane; import com.dao.FlowDao; import com.dao.WarehouseDao; @WebServlet("/AmendServlet") public class AmendServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String name=request.getParameter("Amendname"); String factory=request.getParameter("Amendfactory"); String model=request.getParameter("Amendmodel"); String standard=request.getParameter("Amendstandard"); String number=request.getParameter("Amendnumber"); String danwei=request.getParameter("Amenddanwei"); String person=request.getParameter("Amendperson"); Date date = new Date(); Timestamp timeStamp = new Timestamp(date.getTime()); int num = Integer.parseInt(number); int a=0,b=0; FlowDao flow=new FlowDao(); WarehouseDao ware=new WarehouseDao(); try { a=flow.addFlow(name, factory, model, standard, num, timeStamp, danwei, person); b=ware.DeleteWare(name); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if(a!=0&&b!=0) { int res=JOptionPane.showConfirmDialog(null, "操作成功", "是否继续", JOptionPane.YES_NO_OPTION); if(res==JOptionPane.YES_OPTION){ request.getRequestDispatcher("jiemian.jsp").forward(request, response); //点击“是”后执行这个代码块 }else{ System.out.println("选择否后执行的代码"); //点击“否”后执行这个代码块 return; } } else { JOptionPane.showMessageDialog(null, "操作错误", "请重新操作", JOptionPane.ERROR_MESSAGE); request.getRequestDispatcher("Amend.jsp").forward(request, response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.swing.JOptionPane; import com.dao.WarehouseDao; /** * Servlet implementation class AmendWareServlet */ @WebServlet("/AmendWareServlet") public class AmendWareServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String Amendname=request.getParameter("Amendname"); String amendname=request.getParameter("amendname"); String amendfactory=request.getParameter("amendfactory"); String amendmodel=request.getParameter("amendmodel"); String amendstandard=request.getParameter("amendstandard"); WarehouseDao admin1=new WarehouseDao(); int a1=0,a2=0,a3=0,a4=0; try { a1=admin1.testUpdate1(Amendname,amendname); a2=admin1.testUpdate2(Amendname,amendfactory); a3=admin1.testUpdate3(Amendname,amendmodel); a4=admin1.testUpdate4(Amendname,amendstandard); }catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if(a1!=0&&a2!=0&&a3!=0&&a4!=0) { int res=JOptionPane.showConfirmDialog(null, "操作成功", "是否继续", JOptionPane.YES_NO_OPTION); if(res==JOptionPane.YES_OPTION){ request.getRequestDispatcher("jiemian.jsp").forward(request, response); //点击“是”后执行这个代码块 }else{ System.out.println("选择否后执行的代码"); //点击“否”后执行这个代码块 return; } } else { JOptionPane.showMessageDialog(null, "操作错误", "请重新操作", JOptionPane.ERROR_MESSAGE); request.getRequestDispatcher("AmendWare.jsp").forward(request, response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.swing.JOptionPane; import com.bean.WarehouseBean; import com.dao.WarehouseDao; import com.util.DBUtil; @WebServlet("/DeleteWareServlet") public class DeleteWareServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String name=request.getParameter("Deletename"); int n=0; WarehouseDao ware=new WarehouseDao(); try { n=ware.DeleteWare(name); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if(n!=0) { int res=JOptionPane.showConfirmDialog(null, "操作成功", "是否继续", JOptionPane.YES_NO_OPTION); if(res==JOptionPane.YES_OPTION){ request.getRequestDispatcher("jiemian.jsp").forward(request, response); //点击“是”后执行这个代码块 }else{ System.out.println("选择否后执行的代码"); //点击“否”后执行这个代码块 return; } } else { JOptionPane.showMessageDialog(null, "操作错误", "请重新操作", JOptionPane.ERROR_MESSAGE); request.getRequestDispatcher("DeleteWare.jsp").forward(request, response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
三、实验截图
主界面
商品增加页面
商品删除
商品修改
入库
出库
查询
错误提示
四、遇到的问题
今天连接数据库时总是提示client dose not support authentication protocol requested by server;consider upgrading mysql client
后来通过cmd黑框修改了密码的格式SET PASSWORD FOR -> 'root'@'localhost' = OLD_PASSWORD('11111');问题得到解决;
连接数据库时出现No suitable driver found for jdbc:sql://localhost:3307/choose?useSSL=false错误。后来通过查询在数据库后面加上了
?characterEncoding=utf8&useSSL=true一行代码解决了此问题。