数据库应用表kucun、shangpin
首先建立util包,其中建立JDBUtil.java类
package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBUtil { public static String url="jdbc:mysql://localhost:3306/course?useSSl=false"; public static String user="root"; public static String password="15075289483"; public static Connection getConn() { Connection conn=null; try { Class.forName("com.mysql.jdbc.Driver");//加载驱动 conn = DriverManager.getConnection(url, user, password); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return conn; } //关闭连接 public static void close(Statement state,Connection conn) { if(state!=null) { try { state.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn!=null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void close(ResultSet rs,Statement state,Connection conn) { if(rs!=null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(state!=null) { try { state.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn!=null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
然后建立entity包,其中建立Test类
package entity; import util.JDBUtil; import java.text.DateFormat; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Date; import java.sql.*; import java.util.Scanner; public class Test { static Connection conn; static PreparedStatement ps = null; static ResultSet rs; static String sql = "select * from kucun"; static JDBUtil ut= new JDBUtil(); static Scanner in = new Scanner(System.in); static int id; static String name; static String name_C; static String Xing_hao; static String Gui; static int num; static Date create_date=new Date(); static String date; static String name_Dan; static String name_peo; public static int a_id() { return id; } public static String a_name() { return name; } public static String a_name_C() { return name_C; } public static String a_Xing_hao() { return Xing_hao; } public static String a_Gui() { return Gui; } public static int a_num() { return num; } public static Date a_create_date() { return create_date; } public static String a_date() { return date; } public static String a_name_Dan() { return name_Dan; } public static String a_name_peo() { return name_peo; } public static int add(String Name,int Id,String Name_C,String Xing,String gui,int Num,String Name_Dan,String Name_peo,String Date) { conn= ut.getConn(); String sql="insert into kucun values(?,?,?,?,?,?,?,?,?,?)"; int b=0; try { ps=conn.prepareStatement(sql); ps.setString(2,Name); ps.setInt(1,Id); ps.setString(3, Name_C); ps.setString(4, Xing); ps.setString(5, gui); ps.setInt(6, Num); ps.setString(8, Name_Dan); ps.setString(9, Name_peo); create_date=new Date(); Timestamp t=new Timestamp(create_date.getTime()); System.out.println(t); ps.setTimestamp(7, t); ps.setString(10, Date); /*DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String i=df.format(create); ps.setString(5, i);*/ //ps.setString(5, i); int a=ps.executeUpdate(); if(a>0) { b++; System.out.println("添加成功"); } else { System.out.println("添加失败"); } }catch(Exception e) { e.printStackTrace(); } try { if(ps!=null)ps.close(); if(conn!=null)conn.close(); }catch(Exception e2) { e2.printStackTrace(); } add1(Name,Id,Name_C,Xing,gui,Num); return b; } public static int uqdate(String Name,int Id,String Name_C,String Xing,String gui,int Num,String Name_Dan,String Name_peo,String Date) { int b=0; conn=ut.getConn(); ps=null; sql="update kucun set id=?,name_C=?,Xing_hao=?,Gui=?,num=?,name_Dan=?,name_peo=?,date=? where name=?"; try { ps=conn.prepareStatement(sql); ps.setInt(1,Id); ps.setString(2,Name_C); ps.setString(3,Xing); ps.setString(4,gui); ps.setInt(5,Num); ps.setString(6,Name_Dan); ps.setString(7,Name_peo); ps.setString(8,Date ); ps.setString(9,Name ); int a=ps.executeUpdate(); if(a>0) { b++; System.out.println("修改成功"); } else { System.out.println("修改失败"); } }catch(Exception e) { e.printStackTrace(); } try { if(ps!=null)ps.close(); if(conn!=null)conn.close(); }catch(Exception e2) { e2.printStackTrace(); } return b; } public static void add1(String Name,int Id,String Name_C,String Xing,String gui,int Num) { sql="insert into shangpin values(?,?,?,?,?)"; conn= ut.getConn(); int b=0; try { ps=conn.prepareStatement(sql); ps.setString(2,Name); ps.setInt(1,Id); ps.setString(3, Name_C); ps.setString(4, Xing); ps.setString(5, gui); /*DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String i=df.format(create); ps.setString(5, i);*/ //ps.setString(5, i); int a=ps.executeUpdate(); if(a>0) { b++; System.out.println("添加成功"); } else { System.out.println("添加失败"); } }catch(Exception e) { e.printStackTrace(); } try { if(ps!=null)ps.close(); if(conn!=null)conn.close(); }catch(Exception e2) { e2.printStackTrace(); } } public static int delete(String A) { int b=0; conn=ut.getConn(); ps=null; sql="delete from kucun where name=?"; try { ps=conn.prepareStatement(sql); ps.setString(1,A ); int a=ps.executeUpdate(); if(a>0) { b++; System.out.println("删除成功"); } else { System.out.println("删除失败"); } }catch(Exception e) { e.printStackTrace(); } try { if(ps!=null)ps.close(); if(conn!=null)conn.close(); }catch(Exception e2) { e2.printStackTrace(); } return b; } public static void find_name(String a) { conn=ut.getConn(); ps=null; ResultSet rs=null; sql="select * from kucun where name=?"; try { ps=conn.prepareStatement(sql); ps.setString(1, a); rs=ps.executeQuery(); if(rs.next()) { id = rs.getInt("id"); name = rs.getString("name"); name_C = rs.getString("name_C"); Xing_hao = rs.getString("Xing_hao"); Gui = rs.getString("Gui"); num = rs.getInt("num"); create_date=rs.getDate("create_date"); name_Dan = rs.getString("name_Dan"); name_peo = rs.getString("name_peo"); date = rs.getString("date"); System.out.println(name+id+name_C+Xing_hao+Gui+num+create_date+name_Dan+name_peo+date); } }catch(SQLException e) { e.printStackTrace(); }finally { try { if(ps!=null)ps.close(); if(conn!=null)conn.close(); }catch(Exception e2) { e2.printStackTrace(); } } } public static void find_date(String a) { conn=ut.getConn(); ps=null; ResultSet rs=null; sql="select * from kucun where date=?"; try { ps=conn.prepareStatement(sql); ps.setString(1, a); rs=ps.executeQuery(); if(rs.next()) { id = rs.getInt("id"); name = rs.getString("name"); name_C = rs.getString("name_C"); Xing_hao = rs.getString("Xing_hao"); Gui = rs.getString("Gui"); num = rs.getInt("num"); create_date=rs.getDate("create_date"); name_Dan = rs.getString("name_Dan"); name_peo = rs.getString("name_peo"); System.out.println(name+id+name_C+Xing_hao+Gui+num+create_date+name_Dan+name_peo+date); } }catch(SQLException e) { e.printStackTrace(); }finally { try { if(ps!=null)ps.close(); if(conn!=null)conn.close(); }catch(Exception e2) { e2.printStackTrace(); } } } public static void main(String[] args) { Scanner scan=new Scanner(System.in); String a=scan.next(); delete(a); //find_name(a); //find_date(a); } }
建立zhu.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=ISO-8859-1"> <title>Insert title here</title> <style> .a{ font-size: 26px; margin-top: 20px; } </style> </head> <body> <div align="center"> <div class="a"> <a href = "add.jsp">商品入库</a></br></div> <div class="a"> <a href = "find.jsp">商品名查询 </a></br></div> <div class="a"> <a href = "finddate.jsp">商品时间查询</a></br></div> <div class="a"> <a href = "delete.jsp">商品出库</a></br></div> <div class="a"> <a href = "update.jsp">商品修改</a></br></div> </div> </body> </html></html>
增add.jsp
<%@page import="entity.Test"%> <%@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> <form action="addresult.jsp" method="post" id="form" > <input type="hidden" name="methodName" value="0"/> <h4> 商品名称 :<input type="text" name="name"title="不能为空"></input><br></h4> <h4> 成产厂商 :<input type="text" name="name_C"title="不能为空"></input><br></h4> <h4> 商品型号(数字) :<input type="text" name="id"title="不能为空"></input><br></h4> <h4> 商品规格 :<input type="text" name="Gui"title="不能为空"></input><br></h4> <h4> 商品等级 :<input type="text" name="Xing_hao"title="不能为空"></input><br></h4> <h4> 商品数量(数字) :<input type="text" name="num"title="不能为空"></input><br></h4> <h4> 送货单位 :<input type="text" name="name_Dan"title="不能为空"></input><br></h4> <h4> 送货人姓名:<input type="text" name="name_peo"title="不能为空"></input><br></h4> <h4> 日期 :<input type="text" name="Date"title="不能为空"></input><br></h4> <input type="submit" value="完成"/> </form> </body> </html>
addresult.jsp
<%@page import="entity.Test"%> <%@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> <% request.setCharacterEncoding("utf-8"); String a=request.getParameter("id"); String b=request.getParameter("num"); int a_i=Integer.parseInt(a); int a_s=Integer.parseInt(b); if(Test.add(request.getParameter("name"),a_i,request.getParameter("name_C"),request.getParameter("Xing_hao"),request.getParameter("Gui"),a_s,request.getParameter("name_Dan"),request.getParameter("name_peo"),request.getParameter("Date"))==1) { { out.print("<script language = 'javascript'>alert('入库成功');</script>"); response.setHeader("refresh", "0;url=zhu.jsp"); } } else{ out.print("<script language = 'javascript'>alert('入库失败');</script>"); response.setHeader("refresh", "0;url=zhu.jsp"); //out.println("添加失败"); } %> </body> </html>
删 delete.jsp
<%@page import="entity.Test"%> <%@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=ISO-8859-1"> <title>删除界面</title> </head> <body> <form action="deleteresult.jsp" method="post" id="form" onSubmit="return validate()" > <input type="hidden" name="methodName" value="0"/> <h4> 商品名称 :<input type="text" name="name"title="不能为空"></input><br></h4> <h4> 成产厂商 :<input type="text" name="name_C"title="不能为空"></input><br></h4> <h4> 商品型号(数字) :<input type="text" name="id"title="不能为空"></input><br></h4> <h4> 商品规格 :<input type="text" name="Gui"title="不能为空"></input><br></h4> <h4> 商品等级 :<input type="text" name="Xing_hao"title="不能为空"></input><br></h4> <h4> 商品数量(数字) :<input type="text" name="num"title="不能为空"></input><br></h4> <h4> 取货单位 :<input type="text" name="name_Dan"title="不能为空"></input><br></h4> <h4> 取货人姓名:<input type="text" name="name_peo"title="不能为空"></input><br></h4> <h4> 日期 :<input type="text" name="Date"title="不能为空"></input><br></h4> <input type="submit" value="完成"/> </form> </body> </html>
deleteresult.jsp
<%@page import="entity.Test"%> <%@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> <% request.setCharacterEncoding("utf-8"); if(Test.delete(request.getParameter("name"))==1) { { out.print("<script language = 'javascript'>alert('删除成功');</script>"); response.setHeader("refresh", "0;url=zhu.jsp"); } } else{ out.print("<script language = 'javascript'>alert('删除失败');</script>"); response.setHeader("refresh", "0;url=zhu.jsp"); //out.println("添加失败"); } %> </body> </html>
改update.jsp
<%@page import="entity.Test"%> <%@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=ISO-8859-1"> <title>修改界面</title> </head> <body> <form action="uqdateresult.jsp" method="post" id="form" > <input type="hidden" name="methodName" value="0"/> <h4> 商品名称 :<input type="text" name="name"title="不能为空"></input><br></h4> <h4> 成产厂商 :<input type="text" name="name_C"title="不能为空"></input><br></h4> <h4> 商品型号(数字) :<input type="text" name="id"title="不能为空"></input><br></h4> <h4> 商品规格 :<input type="text" name="Gui"title="不能为空"></input><br></h4> <h4> 商品等级 :<input type="text" name="Xing_hao"title="不能为空"></input><br></h4> <h4> 商品数量(数字) :<input type="text" name="num"title="不能为空"></input><br></h4> <h4> 送货(取货)单位 :<input type="text" name="name_Dan"title="不能为空"></input><br></h4> <h4> 送货(取货)人姓名:<input type="text" name="name_peo"title="不能为空"></input><br></h4> <h4> 日期 :<input type="text" name="Date"title="不能为空"></input><br></h4> <input type="submit" value="完成"/> </form> </body> </html>
updateresult.jsp
<%@page import="entity.Test"%> <%@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=ISO-8859-1"> <title>修改结果</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String a=request.getParameter("id"); String b=request.getParameter("num"); int a_i=Integer.parseInt(a); int a_s=Integer.parseInt(b); if(Test.uqdate(request.getParameter("name"),a_i,request.getParameter("name_C"),request.getParameter("Xing_hao"),request.getParameter("Gui"),a_s,request.getParameter("name_Dan"),request.getParameter("name_peo"),request.getParameter("Date"))==1) { { out.print("<script language = 'javascript'>alert('修改成功');</script>"); response.setHeader("refresh", "0;url=zhu.jsp"); } } else{ out.print("<script language = 'javascript'>alert('修改失败');</script>"); response.setHeader("refresh", "0;url=zhu.jsp"); //out.println("添加失败"); } %> </body> </html>
查find.jsp
<%@page import="entity.Test"%> <%@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=ISO-8859-1"> <title>商品名称 查找界面</title> </head> <body> <form action="findresult.jsp" method="post" id="form" > <input type="hidden" name="methodName" value="0"/> <h4> 商品名称:<input type="text" name="name"title="课程不能为空"></input><br></h4> <input type="submit" value="完成"/> </form> </body> </html>
findresult.jsp
<%@page import="entity.Test"%> <%@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=ISO-8859-1"> <title>查找结果</title> </head> <body> <% request.setCharacterEncoding("utf-8"); Test.find_name(request.getParameter("name")); out.println(Test.a_id()); out.println(Test.a_name()); out.println(Test.a_name_C()); out.println(Test.a_Xing_hao()); out.println(Test.a_Gui()); out.println(Test.a_num()); out.println(Test.a_create_date()); out.println(Test.a_date()); out.println(Test.a_name_Dan()); out.println(Test.a_name_peo()); %> <form action="zhu.jsp" method="post" id="form" > <input type="submit" value="返回首页"/> </body> </html>
finddate.jsp
<%@page import="entity.Test"%> <%@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=ISO-8859-1"> <title>时间查询</title> </head> <body> <form action="finddateresult.jsp" method="post" id="form" > <input type="hidden" name="methodName" value="0"/> <h4> 日期:<input type="text" name="date"title="课程不能为空"></input><br></h4> <input type="submit" value="完成"/> </form> </body> </html>
finddateresult.jsp
<%@page import="entity.Test"%> <%@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=ISO-8859-1"> <title>时间查询结果</title> </head> <body> <% request.setCharacterEncoding("utf-8"); Test.find_date(request.getParameter("date")); out.println(Test.a_id()); out.println(Test.a_name()); out.println(Test.a_name_C()); out.println(Test.a_Xing_hao()); out.println(Test.a_Gui()); out.println(Test.a_num()); out.println(Test.a_create_date()); out.println(Test.a_date()); out.println(Test.a_name_Dan()); out.println(Test.a_name_peo()); %> <form action="zhu.jsp" method="post" id="form" > <input type="submit" value="返回首页"/> </body> </html>
运行截图: