一:首先连接数据库
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/test?useSSl=false"; public static String user="root"; public static String password="root"; 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(); } } } }
二:对数据库进行操做
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 test"; 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 test 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 test 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); 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 test 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 test 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 test 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); } }
三:web代码
1
<%@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"/> 输入信息:<br> <h4> 商品名称 : <input type="text" name="name"></input><br></h4> <h4> 成产厂商 : <input type="text" name="name_C"></input><br></h4> <h4> 商品型号(数字) : <input type="text" name="id"></input><br></h4> <h4> 商品规格 : <input type="text" name="Gui"></input><br></h4> <h4> 商品等级 : <input type="text" name="Xing_hao"></input><br></h4> <h4> 商品数量(数字) : <input type="text" name="num"></input><br></h4> <h4> 送货单位 : <input type="text" name="name_Dan"></input><br></h4> <h4> 送货人姓名: <input type="text" name="name_peo"></input><br></h4> <h4> 日期 :<input type="text" name="Date"></input><br></h4> <input type="submit" value="OK"/> </form> </body> </html>
2
<%@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>
3
<%@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>
4
<%@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>
5
<%@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>
6
<%@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>
7
<%@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>
8
<%@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>
9
<%@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>
10
<%@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>
11
<%@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> <div style="background:#000; color:#FFF">背景为黑色</div> .a{ font-size: 26px; margin-top: 26px; } </style> </head> <body> <div align="center"> <div class="a"> <a>物资管理系统</a></br></div> <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>
运行结果
增加
按名称查询
按时间查询
删除
修改
原文地址:https://www.cnblogs.com/1502762920-com/p/10117098.html
时间: 2024-11-02 02:16:31