这个是对存放用户名和密码的数据库的连接
package DBUtil; 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/jiatingjizhangben"; public static String db_user = "root"; public static String db_pass = "zxh521+."; public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver");//加载驱动 conn = DriverManager.getConnection(db_url, db_user, db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; } public static void main(String[] args) { try { //第三步:获取连接类实例con,用con创建Statement对象类实例 sql_statement Connection con = getConn(); Statement sql_statement = con.createStatement(); String query = "select * from jiatinghuaxiao"; ResultSet result = sql_statement.executeQuery(query); //显示数据中network表中的内容: System.out.println("数据库jiatingjizhangben表jiatinghuaxiao中的数据如下:"); System.out.println("------------------------"); System.out.println("zhurou"+" "+"yurou"+" "+"jidan"+" "+"niurou"+" "+"yangrou"+" "+"hongbao"+" "+"qita"); System.out.println("------------------------"); //对获得的查询结果进行处理,对Result类的对象进行操作 while (result.next()) { String zhurou = result.getString("zhurou"); String yurou= result.getString("yurou"); String jidan= result.getString("jidan"); String niurou= result.getString("niurou"); String yangrou= result.getString("yangrou"); String hongbao= result.getString("hongbao"); String qita= result.getString("qita"); //取得数据库中的数据 System.out.println(""+zhurou + " " +yurou+" "+jidan+" "+niurou+" "+yangrou+" "+hongbao+" "+qita+" "); } } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } } /** * 关闭连接 * @param state * @param 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(); } } } }
这个是对家庭花销数据库的连接
package DBUtil; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; //连接数据库 public class DButil2 { public static String db_url = "jdbc:mysql://localhost:3306/jiatingjizhangben"; public static String db_user = "root"; public static String db_pass = "zxh521+."; public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver");//加载驱动 conn = DriverManager.getConnection(db_url, db_user, db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; } public static void main(String[] args) { try { //第三步:获取连接类实例con,用con创建Statement对象类实例 sql_statement Connection con = getConn(); Statement sql_statement = con.createStatement(); String query = "select * from denglu"; ResultSet result = sql_statement.executeQuery(query); //显示数据中network表中的内容: System.out.println("数据库jiatingjiazhangben表denglu中的数据如下:"); System.out.println("------------------------"); System.out.println("username"+" "+"password"+" "); System.out.println("------------------------"); //对获得的查询结果进行处理,对Result类的对象进行操作 while (result.next()) { String username = result.getString("username"); String password = result.getString("password"); //取得数据库中的数据 System.out.println(""+username + " " + password+" "); } } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } } /** * 关闭连接 * @param state * @param 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(); } } } }
然后是set get方法以及构造函数的类
package entity; public class entity { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public entity(int id,String username,String password) { this.id = id; this.username = username; this.password = password; } public entity(String username,String password) { this.username = username; this.password = password; } }
package entity; public class entity2 { int id; private String zhurou; private String yurou; private String jidan; private String niurou; private String yangrou; private String hongbao; private String qita; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getZhurou() { return zhurou; } public void setZhurou(String zhurou) { this.zhurou = zhurou; } public String getYurou() { return yurou; } public void setYurou(String yurou) { this.yurou = yurou; } public String getJidan() { return jidan; } public void setJidan(String jidan) { this.jidan = jidan; } public String getNiurou() { return niurou; } public void setNiurou(String niurou) { this.niurou = niurou; } public String getYangrou() { return yangrou; } public void setYangrou(String yangrou) { this.yangrou = yangrou; } public String getHongbao() { return hongbao; } public void setHongbao(String hongbao) { this.hongbao = hongbao; } public String getQita() { return qita; } public void setQita(String qita) { this.qita = qita; } public entity2(int id,String zhurou,String yurou,String jidan,String niurou,String yangrou,String hongbao,String qita) { this.id = id; this.zhurou = zhurou; this.yurou = yurou; this.jidan = jidan; this.niurou = niurou; this.yangrou = yangrou; this.hongbao = hongbao; this.qita = qita; } public entity2(String zhurou,String yurou,String jidan,String niurou,String yangrou,String hongbao,String qita) { this.zhurou = zhurou; this.yurou = yurou; this.jidan = jidan; this.niurou = niurou; this.yangrou = yangrou; this.hongbao = hongbao; this.qita = qita; } }
下面是dao层
package 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 DBUtil.DButil; import DBUtil.DButil2; import entity.entity; import entity.entity2; public class dao { public boolean xiugaimima1(entity entity) { String sql = "insert into denglu(username, password) values('" + entity.getUsername() + "','" + entity.getPassword() + "')"; Connection conn = DButil2.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { DButil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 登录,比对密码 * @param username * @return */ public String denglu1(String username) { String sql = "select * from denglu where "; if (username != "") { sql= "password like '%" + username + "%'"; } Connection conn = DButil2.getConn(); Statement state = null; ResultSet rs = null; String password2 = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { password2 = rs.getString("password"); } } catch (SQLException e) { e.printStackTrace(); } finally { DButil.close(rs, state, conn); } return password2; } /** * 登录,比对用户名 * @param username * @return */ public String denglu11(String username) { String sql = "select * from denglu where "; if (username != "") { sql= "username like '%" + username + "%'"; } Connection conn = DButil2.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { username = rs.getString("username"); } } catch (SQLException e) { e.printStackTrace(); } finally { DButil.close(rs, state, conn); } return username; } public boolean zhurou(String zhurou) { boolean flag = true; String sql = "select zhurou from jiatinghuaxiao where zhurou = '" + zhurou + "'"; Connection conn = DButil2.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { flag = false; } } catch (SQLException e) { e.printStackTrace(); } finally { DButil.close(rs, state, conn); } return flag; } public boolean add(entity2 entity2) { String sql = "insert into jiatinghuaxiao(zhurou,yurou,jidan,niurou,yangrou,hongbao,qita) values('" +entity2.getZhurou() + "','" + entity2.getYurou() + "','" + entity2.getJidan() + "','" + entity2.getNiurou()+ "','" + entity2.getYangrou()+"','"+entity2.getHongbao() +"','"+entity2.getQita()+ "')"; Connection conn = DButil.getConn(); Statement state = null; boolean f = false; int a = 0; if(zhurou(entity2.getZhurou())){ try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { DButil.close(state, conn); } } if (a > 0) { f = true; } return f; } public entity2 getStudentById(int id) { String sql = "select * from jiatinghuaxiao where id ='" + id + "'"; Connection conn = DButil2.getConn(); Statement state = null; ResultSet rs = null; entity2 entity2 = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String zhurou = rs.getString("zhurou"); String yurou = rs.getString("yurou"); String jidan = rs.getString("jidan"); String niurou = rs.getString("niurou"); String yangrou = rs.getString("yangrou"); String hongbao = rs.getString("hongbao"); String qita= rs.getString("qita"); entity2 = new entity2(id,zhurou, yurou, jidan,niurou,yangrou,hongbao,qita); } } catch (Exception e) { e.printStackTrace(); } finally { DButil.close(rs, state, conn); } return entity2; } public entity2 getStudentByName(String zhurou) { String sql = "select * from jiatinghuaxiao where zhurou ='" + zhurou + "'"; Connection conn = DButil2.getConn(); Statement state = null; ResultSet rs = null; entity2 entity2 = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { int id = rs.getInt("id"); String yurou = rs.getString("yurou"); String jidan = rs.getString("jidan"); String niurou = rs.getString("niurou"); String yangrou = rs.getString("yangrou"); String hongbao = rs.getString("hongbao"); String qita= rs.getString("qita"); entity2 = new entity2(id,zhurou, yurou, jidan,niurou,yangrou,hongbao,qita); } } catch (Exception e) { e.printStackTrace(); } finally { DButil.close(rs, state, conn); } return entity2; } public boolean delete(int id) { boolean f = false; String sql = "delete from jiatinghuaxiao where id='" + id + "'"; Connection conn = DButil2.getConn(); Statement state = null; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DButil.close(state, conn); } if (a > 0) { f = true; } return f; } public List<entity2> list() { String sql = "select * from jiatinghuaxiao"; List<entity2> list1 = new ArrayList<>(); Connection conn = DButil2.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); entity2 entity2 = null; while (rs.next()) { int id = rs.getInt("id"); String zhurou = rs.getString("zhurou"); String yurou = rs.getString("yurou"); String jidan = rs.getString("jidan"); String niurou = rs.getString("niurou"); String yangrou = rs.getString("yangrou"); String hongbao = rs.getString("hongbao"); String qita= rs.getString("qita"); entity2 = new entity2(id,zhurou, yurou, jidan,niurou,yangrou,hongbao,qita); list1.add(entity2); } } catch (SQLException e) { e.printStackTrace(); } finally { DButil.close(rs, state, conn); } return list1; } public boolean update(entity2 entity2) { String sql = "update jiatinghuaxiao set zhurou='" + entity2.getZhurou() + "', yurou='" + entity2.getYurou()+"',jidan='"+entity2.getJidan()+"',niurou='"+entity2.getNiurou()+"',yangrou='"+entity2.getYangrou()+"',hongbao='"+entity2.getHongbao()+"',qita='"+entity2.getQita() + "' where id='" + entity2.getId() + "'"; Connection conn = DButil2.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DButil.close(state, conn); } if (a > 0) { f = true; } return f; } public entity2 search(String zhurou,String yurou) { String sql = "select * from jiatinghuaxiao where "; String sql1 =sql+ "zhurou like '%" + zhurou + "%'"; String sql2 =sql+ "yurou like '%" + yurou + "%'"; entity2 s1 = null; entity2 s2 = null; Connection conn = DButil.getConn(); Statement state = null; ResultSet rs = null; ResultSet rs1 = null; try { state = conn.createStatement(); rs = state.executeQuery(sql1); while (rs.next()) { int id = rs.getInt("id"); String zhurou2 = rs.getString("zhurou"); String yurou2 = rs.getString("yurou"); String jidan2 = rs.getString("jidan"); String niurou2 = rs.getString("niurou"); String yangrou2 = rs.getString("yangrou"); String hongbao2 = rs.getString("hongbao"); String qita2= rs.getString("qita"); s1 = new entity2(id,zhurou2, yurou2, jidan2,niurou2,yangrou2,hongbao2,qita2); } rs1 = state.executeQuery(sql2); while (rs1.next()) { int id = rs1.getInt("id"); String zhurou2 = rs1.getString("zhurou"); String yurou2 = rs1.getString("yurou"); String jidan2 = rs1.getString("jidan"); String niurou2 = rs1.getString("niurou"); String yangrou2 = rs1.getString("yangrou"); String hongbao2 = rs1.getString("hongbao"); String qita2= rs1.getString("qita"); s2 = new entity2(id,zhurou2, yurou2, jidan2,niurou2,yangrou2,hongbao2,qita2); } } catch (SQLException e) { e.printStackTrace(); } finally { DButil.close(rs, state, conn); DButil.close(rs1, state, conn); } if(s1.getId()==s2.getId()){ return s1; } else{ return null; } } }
下面是servlet层
package servlet; import java.io.IOException; import java.util.List; 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 Dao.dao; import entity.entity; import entity.entity2; /** * Servlet implementation class servlet */ @WebServlet("/servlet") public class servlet extends HttpServlet { private static final long serialVersionUID = 1L; dao dao = new dao(); protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("denglu".equals(method)) { denglu(req, resp); }else if ("add".equals(method)) { add(req, resp); }else if ("getstudentbyid".equals(method)) { getStudentById(req, resp); }else if ("getstudentbyzhurou".equals(method)) { getStudentByZhurou(req, resp); }else if ("delete".equals(method)) { delete(req, resp); }else if ("update".equals(method)) { update(req, resp); }else if ("list".equals(method)) { list(req, resp); }else if ("search".equals(method)) { search(req, resp); } } private void denglu(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); String username = req.getParameter("username"); String password = req.getParameter("password"); String password1 = dao.denglu1(username); String username1 = dao.denglu11(username); if(password.equals(password1)||username.equals(username1)) { req.setAttribute("message", "登陆成功!"); req.getRequestDispatcher("zhuyemian.jsp").forward(req,resp); } else { req.setAttribute("message", "用户不存在或密码错误!"); req.getRequestDispatcher("denglu.jsp").forward(req,resp); } } private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); //获取数据 String zhurou = req.getParameter("zhurou"); String yurou = req.getParameter("yurou"); String jidan = req.getParameter("jidan"); String niurou = req.getParameter("niurou"); String yangrou = req.getParameter("yangrou"); String hongbao = req.getParameter("hongbao"); String qita = req.getParameter("qita"); entity2 entity2 = new entity2(zhurou,yurou,jidan,niurou,yangrou,hongbao,qita); if(dao.add(entity2)) { req.setAttribute("message", "添加成功"); req.getRequestDispatcher("add.jsp").forward(req,resp); } else { req.setAttribute("message", "姓名重复,请重新录入"); req.getRequestDispatcher("add.jsp").forward(req,resp); } } private void getStudentById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); entity2 entity2 = dao.getStudentById(id); req.setAttribute("entity2", entity2); req.getRequestDispatcher("update.jsp").forward(req,resp); } private void getStudentByZhurou(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String zhurou = req.getParameter("zhurou"); entity2 entity2 = dao.getStudentByName(zhurou); if(entity2 == null) { req.setAttribute("message", "查无此信息!"); req.getRequestDispatcher("delete.jsp").forward(req,resp); } else { req.setAttribute("entity2", entity2); req.getRequestDispatcher("deletelist.jsp").forward(req,resp); } } private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); dao.delete(id); req.setAttribute("message", "删除成功!"); req.getRequestDispatcher("delete.jsp").forward(req,resp); } private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); List<entity2> entity2 = dao.list(); req.setAttribute("entity2", entity2); req.getRequestDispatcher("list.jsp").forward(req,resp); } private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); String zhurou = req.getParameter("zhurou"); String yurou = req.getParameter("yurou"); String jidan = req.getParameter("jidan"); String niurou = req.getParameter("niurou"); String yangrou = req.getParameter("yangrou"); String hongbao = req.getParameter("hongbao"); String qita = req.getParameter("qita"); entity2 entity2 = new entity2(id,zhurou,yurou,jidan,niurou,yangrou,hongbao,qita); dao.update(entity2); req.setAttribute("message", "修改成功"); req.getRequestDispatcher("servlet?method=list").forward(req,resp); } private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String zhurou = req.getParameter("zhurou"); String yurou = req.getParameter("yurou"); entity2 entity2 = dao.search(zhurou,yurou); if(entity2!=null){ req.setAttribute("entity2", entity2); req.getRequestDispatcher("searchlist.jsp").forward(req,resp); } else{ req.setAttribute("message", "查无此信息!"); req.getRequestDispatcher("search.jsp").forward(req,resp); } } }
下面是登陆的denglu.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> <% Object message = request.getAttribute("message");//放置一个字符串,并取出 if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <div><h1>登录</h1></div> <form action="servlet?method=denglu" method="post" onsubmit="return check()"> <table align="center" border="1" width="50%" cellpadding="6"> <tr> <th colspan="2" align="center" >登录页面</th> </tr> <tr> <td align="center" >用户名</td> <td align="center" ><input type="text" name="username"></td> </tr> <tr> <td align="center" >密码</td> <td align="center" ><input type="text" name="password"></td> </tr> <tr> <th colspan="2" align="center" ><button type="submit" class="b">登 陆</button></th> </tr> </table> </form> <div><a href="zhuce.jsp"><font SIZE="4" >注册</font></a></div> </div> <script type="text/javascript"> function check() { var username = document.getElementById("username");; var password = document.getElementById("password"); //非空 if(username.value == '') { alert('账号为空'); username.focus(); return false; } if(password.value == '') { alert('密码为空'); password.focus(); return false; } } </script> </body> </html>
下面是主页面的zhuyemian.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> <a href="denglu.jsp">返回登陆页面</a> <div class="a"> <a href="add.jsp">添加花销</a> </div> <div class="a"> <a href="servlet?method=list">修改花销</a> </div> <div class="a"> <a href="delete.jsp">删除花销</a> </div> <div class="a"> <a href="search.jsp">查询花销</a> </div> </div> </body> </html>
下面是增加的add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>添加花销</title> </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: black;">添加花销</h1> <a href="zhuyemian.jsp">返回主页面</a> <form action="servlet?method=add" method="post" onsubmit="return check()"> <div class="a"> 猪肉<input type="text" id="zhurou" name="zhurou"/> </div> <div class="a"> 鱼肉<input type="text" id="yurou" name="yurou" /> </div> <div class="a"> 鸡蛋<input type="text" id="jidan" name="jidan" /> </div> <div class="a"> 牛肉<input type="text" id="niurou" name="niurou"/> </div> <div class="a"> 羊肉<input type="text" id="yangrou" name="yangrou"/> </div> <div class="a"> 红包<input type="text" id="hongbao" name="hongbao"/> </div> <div class="a"> 其他<input type="text" id="qita" name="qita"/> </div> <div class="a"> <button type="submit" class="b">保存</button> </div> </form> </div> <script type="text/javascript"> function check() { var zhurou = document.getElementById("zhurou"); var yurou = document.getElementById("yurou"); var jidan= document.getElementById("jidan"); var niurou = document.getElementById("niurou"); var yangrou= document.getElementById("yangrou"); var hongbao = document.getElementById("hongbao"); var qita = document.getElementById("qita"); //非空 if(zhurou.value == '') { alert('猪肉为空'); zhurou.focus(); return false; } if(yurou.value == '') { alert('鱼肉为空'); yurou.focus(); return false; } if(jidan.value == '') { alert('鸡蛋为空'); jidan.focus(); return false; } if(niurou.value == '') { alert('牛肉为空'); niurou.focus(); return false; } if(yangrou.value == '') { alert('羊肉为空'); yangrou.focus(); return false; } if(hongbao.value == '') { alert('红包为空'); hongbao.focus(); return false; } if(qita.value == '') { alert('其他为空'); qita.focus(); return false; } } </script> </body> </html>
下面是删除的delete.jsp和deletelist.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>删除界面</title> </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: black;">花销删除</h1> <a href="zhuyemian.jsp">返回主页</a> <form action="servlet?method=getstudentbyzhurou" method="post" onsubmit="return check()"> <div class="a"> 猪肉<input type="text" id="zhurou" name="zhurou"/> </div> <div class="a"> <button type="submit" class="b">查找</button> </div> </form> </div> <script type="text/javascript"> function check() { var zhurou = document.getElementById("zhurou");; //非空 if(zhurou.value == '') { alert('猪肉为空'); zhurou.focus(); return false; } } </script> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>删除</title> </head> <body> <div align="center"> <h1 style="color: black;">花销删除</h1> <a href="zhuyemian.jsp">返回主页</a> <table class="tb"> <tr> <td>猪肉</td> <td>${entity2.zhurou}</td> </tr> <tr> <td>鱼肉</td> <td>${entity2.yurou}</td> </tr> <tr> <td>鸡蛋</td> <td>${entity2.jidan}</td> </tr> <tr> <td>牛肉</td> <td>${entity2.niurou}</td> </tr> <tr> <td>羊肉</td> <td>${entity2.yangrou}</td> </tr> <tr> <td>红包</td> <td>${entity2.hongbao}</td> </tr> <tr> <td>其他</td> <td>${entity2.qita}</td> </tr> </table> <div class="a"> <a onclick="return check()" href="servlet?method=delete&id=${entity2.id}">删除</a> </div> </div> <script type="text/javascript"> function check() { if (confirm("真的要删除吗?")){ return true; }else{ return false; } } </script> </body> </html>
下面是修改的list.jsp和update.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> </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: black;">花销列表</h1> <a href="zhuyemian.jsp">返回主页</a> <table class="tb" border="1"> <tr> <td hidden="hidden">id</td> <td>id</td> <td>猪肉</td> <td>鱼肉</td> <td>鸡蛋</td> <td>牛肉</td> <td>羊肉</td> <td>红包</td> <td>其他</td> <td align="center" colspan="2">操作</td> </tr> <c:forEach items="${entity2}" var="item"> <tr> <td hidden="hidden">${item.id}</td> <td>${item.id}</td> <td>${item.zhurou}</td> <td>${item.yurou}</td> <td>${item.jidan}</td> <td>${item.niurou}</td> <td>${item.yangrou}</td> <td>${item.hongbao}</td> <td>${item.qita}</td> <td><a href="servlet?method=getstudentbyid&id=${item.id}">修改</a></td> </tr> </c:forEach> </table> </div> </body> </html>
<%@ 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; 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="zhuyemian.jsp">返回主页</a> <form action="servlet?method=update" method="post" onsubmit="return check()"> <div class="a"> 猪肉<input type="text" id="zhurou" name="zhurou" value="${entity2.zhurou}"/> </div> <div class="a"> 鱼肉<input type="text" id="yurou" name="zhurou" value="${entity2.yurou}"/> </div> <div class="a"> 鸡蛋<input type="text" id="jidan" name="jidan" value="${entity2.jidan}"/> </div> <div class="a"> 牛肉<input type="text" id="niurou" name="niurou" value="${entity2.niurou}"/> </div> <div class="a"> 羊肉<input type="text" id="yangrou" name="yangrou" value="${entity2.yangrou}"/> </div> <div class="a"> 红包<input type="text" id="hongbao" name="hongbao" value="${entity2.hongbao}"/> </div> <div class="a"> 其他<input type="text" id="qita" name="qita" value="${entity2.qita}"/> </div> <input type="hidden" id="id" name="id" value="${entity2.id}"/> <div class="a"> <button type="submit" class="b">修 改</button> </div> </form> </div> <script type="text/javascript"> function check() { var zhurou = document.getElementById("zhurou"); var yurou = document.getElementById("yurou"); var jidan= document.getElementById("jidan"); var niurou = document.getElementById("niurou"); var yangrou= document.getElementById("yangrou"); var hongbao = document.getElementById("hongbao"); var qita = document.getElementById("qita"); //非空 if(zhurou.value == '') { alert('猪肉为空'); zhurou.focus(); return false; } if(yurou.value == '') { alert('鱼肉为空'); yurou.focus(); return false; } if(jidan.value == '') { alert('鸡蛋为空'); jidan.focus(); return false; } if(niurou.value == '') { alert('牛肉为空'); niurou.focus(); return false; } if(yangrou.value == '') { alert('羊肉为空'); yangrou.focus(); return false; } if(hongbao.value == '') { alert('红包为空'); hongbao.focus(); return false; } if(qita.value == '') { alert('其他为空'); qita.focus(); return false; } } </script> </body> </html>
下面是查询的search.jsp和searchliat.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>查询</title> </head> <body> <div align="center"> <h1 style="color: black;">花销查询</h1> <a href="zhuyemian">返回主页面</a> <form action="servlet?method=search" method="post" onsubmit="return check()"> <div class="a"> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>查询</title> </head> <body> <div align="center"> <h1 style="color: black;">花销查询</h1> <a href="zhuyemian">返回主页面</a> <form action="servlet?method=search" method="post" onsubmit="return check()"> <div class="a"> 猪肉<input type="text" id="zhurou" name="zhurou"/> </div> <div class="a"> 鱼肉<input type="text" id="yurou" name="yurou"/> </div> <div class="a"> <button type="submit" class="b">查询</button> </div> </form> </div> <script type="text/javascript"> function check() { var zhurou = document.getElementById("zhurou"); if(zhurou.value == '' ) { alert('请补全条件'); return false; } } </script> </body> </html> 猪肉<input type="text" id="zhurou" name="zhurou"/> </div> <div class="a"> 鱼肉<input type="text" id="yurou" name="yurou"/> </div> <div class="a"> <button type="submit" class="b">查询</button> </div> </form> </div> <script type="text/javascript"> function check() { var zhurou = document.getElementById("zhurou"); if(zhurou.value == '' ) { alert('请补全条件'); return false; } } </script> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>查询界面</title> </head> <body> <div align="center"> <h1 style="color: black;">查询结果</h1> <a href="zhuyemian.jsp">返回主页</a> <table class="tb" border="1"> <tr> <td>猪肉</td> <td>鱼肉</td> <td>鸡蛋</td> <td>牛肉</td> <td>羊肉</td> <td>红包</td> <td>其他</td> </tr> <tr> <td>${entity2.zhurou}</td> <td>${entity2.yurou}</td> <td>${entity2.jidan}</td> <td>${entity2.niurou}</td> <td>${entity2.yangrou}</td> <td>${entity2.hongbao}</td> <td>${entity2.qita}</td> </tr> </table> </div> </body> </html>
下面是操作过程图,第一个图片是生成的小软件的标志,点击这个图标,可以直接进入登陆界面。下面的图片是我家初一的花销情况,因为之前有检验,id为9的是我家初一的花销情况,初一到c初七的花销情况当时记到手机记事本上了,现在就统一把初一到初七的花销情况记录下来。以下就是对初一花销情况记录的操作过程图。