开发进度——7
index.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>Insert title here</title>
</head>
<body>
<iframe src = "left1.jsp" width = "150" height = "300" scrolling="no" frameborder="0" marginheight="90"></iframe>
<iframe src="" name="name" id = "name" width = "800" height = "300" frameborder="1" ></iframe>
</body>
</html>
left1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.*" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta charset="UTF-8"> <title>首页</title> <style> div{ text-align:center; margin: 0; padding: 0; font-size: 40px; font-family: "Microsoft Yahei", Verdana, Arial, Helvetica, sans-serif } h1{ font-size:130%; } h2{ font-size:80%; } </style> </head> <body> <h1>功能页</h1> <h2><a href="add.jsp" target="name">信息添加</a></h2> <h2><a href="list.jsp" target="name">记账本修改</a></h2> <h2><a href="accountServlet?method=list1" target="name">记账本删除</a></h2> <h2><a href="find.jsp" target="name">记账本查找</a></h2> </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="index.jsp">返回主页</a> <form action="${pageContext.request.contextPath}/accountServlet?method=add" method="post" onsubmit="return check()"> <div class="a"> 消费人<input type="text" id="name" name="name"/> </div> <div class="a"> 消费金额<input type="text" id="number" name="number" /> </div> <div class="a"> 备注<input type="text" id="remark" name="remark" /> </div> <div class="a"> <button type="submit" class="b">保 存</button> </div> </form> </div> </body> </html>
chuli.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.util.List" %> <%@page import="com.bean.*" %> <%@page import="com.dao.*" %> <%@page import="java.sql.Timestamp" %> <%@page import="java.text.SimpleDateFormat" %> <%@page import="java.util.Date" %> <%@page import="java.util.ArrayList" %> <%@page import="java.util.regex.Matcher" %> <%@page import="java.util.regex.Pattern" %> <!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> <style type="text/css"> div{ text-align:center; margin: 0; padding: 0; font-size: 40px; font-family: "Microsoft Yahei", Verdana, Arial, Helvetica, sans-serif } h1{ font-size:130%; } h2{ font-size:80%; } .input{ font-size: 20px; } </style> </head> <body> <table width = "400" border ="0" height = "300" align = "center" cellpading ="0" style = "font-size:30px;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 = "Updatejibenservlet" method = "post"> <tr> <h1>欢迎使用本系统</h1> </tr> <tr> <table border="1" align = "center" style = "font-size:30px;color:#FFFFF;front-weight:bold"> <tr> <td>姓名</td> <td>花费金额</td> <td>备注</td> <td>时间</td> </tr> <% request.setCharacterEncoding("UTF-8"); String name=request.getParameter("name"); accountbook c=null; int a=0; List<accountbook> beans = accountdao.list(); for(int i=0;i<beans.size();i++) { c=beans.get(i); if(c.getName().equals(name)) { %> <tr> <td><input type="text" name="name" size="10" maxlength="10" class="input" value="<%=c.getName() %>"></td> <td><input type="text" name="banji" size="10" maxlength="10" class="input" value="<%=c.getNumber() %>"></td> <td><input type="text" name="sex" size="10" maxlength="10" class="input" value="<%=c.getRemark() %>"></td> <td><input type="text" name="age" size="10" maxlength="30" class="input" value="<%=c.getTime() %>"></td> </tr> <% } } %> </tr> <tr> <td><a href="index.jsp">返回</a></td> </tr> </form> </table> </td> </tr> </table> </body> </html>
delete.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 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>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="index.jsp">返回主页</a> <table border="1" class="tb"> <tr> <td>id</td> <td>姓名</td> <td>花费金额</td> <td>备注</td> <td>时间</td> <td align="center" colspan="2">操作</td> </tr> <c:forEach items="${account}" var="item"> <tr> <td>${item.id}</td> <td>${item.name}</td> <td>${item.number}</td> <td>${item.remark}</td> <td>${item.time}</td> <td><a href="accountServlet?method=delete&id=${item.id}">删除</a></td> </tr> </c:forEach> </table> </div> </body> </html>
find.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 = "chuli.jsp" method = "post"> <tr> <td>请输入要查找的姓名:</td><td><input type="text" name="name" /></td> </tr> <tr> <td><input name = "login" type = "submit" id = "login" value = "提交" class = "button"></td> </tr> </form> </table> </td> </tr> </table> </html>
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.util.List" %> <%@page import="com.bean.*" %> <%@page import="com.dao.*" %> <%@page import="java.sql.Timestamp" %> <%@page import="java.text.SimpleDateFormat" %> <%@page import="java.util.Date" %> <%@page import="java.util.ArrayList" %> <%@page import="java.util.regex.Matcher" %> <%@page import="java.util.regex.Pattern" %> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!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>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="index.jsp">返回主页</a> <table border="1" class="tb"> <tr> <td>id</td> <td>姓名</td> <td>花费金额</td> <td>备注</td> <td>时间</td> <td align="center" colspan="2">操作</td> </tr> <% request.setCharacterEncoding("UTF-8"); accountbook c=null; int a=0; List<accountbook> beans = accountdao.list(); for(int i=0;i<beans.size();i++) { c=beans.get(i); %> <tr> <td><input type="text" name="id" size="10" maxlength="10" class="input" value="<%=c.getId() %>"></td> <td><input type="text" name="name" size="10" maxlength="10" class="input" value="<%=c.getName() %>"></td> <td><input type="text" name="number" size="10" maxlength="10" class="input" value="<%=c.getNumber() %>"></td> <td><input type="text" name="remark" size="10" maxlength="10" class="input" value="<%=c.getRemark() %>"></td> <td><input type="text" name="time" size="10" maxlength="10" class="input" value="<%=c.getTime() %>"></td> <script type="text/javascript"> var name1 = document.getElementById("name").value; var number = document.getElementById("number"); var remark = document.getElementById("remark"); var time = document.getElementById("time"); </script> <td><a href="accountServlet?method=update&id=<%=c.getId() %>">修改</a></td> </tr> <% } %> </table> </div> </body> </html>
list1.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 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>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="index.jsp">返回主页</a> <table border="1" class="tb"> <tr> <td>id</td> <td>姓名</td> <td>花费金额</td> <td>备注</td> <td>时间</td> <td align="center" colspan="2">操作</td> </tr> <c:forEach items="${account}" var="item"> <tr> <td>${item.id}</td> <td>${item.name}</td> <td>${item.number}</td> <td>${item.remark}</td> <td>${item.time}</td> <td><a href="accountServlet?method=update&id=${item.id}">修改</a></td> </tr> </c:forEach> </table> </div> </body> </html>
update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.util.List" %> <%@page import="com.bean.*" %> <%@page import="com.dao.*" %> <%@page import="java.sql.Timestamp" %> <%@page import="java.text.SimpleDateFormat" %> <%@page import="java.util.Date" %> <%@page import="java.util.ArrayList" %> <%@page import="java.util.regex.Matcher" %> <%@page import="java.util.regex.Pattern" %> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!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>Insert title here</title> </head> <body> <form name = "form2" action = "updateServlet" method = "post"> <% 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="index.jsp">返回主页</a> <table border="1" class="tb"> <tr> <td>id</td> <td>姓名</td> <td>花费金额</td> <td>备注</td> <td align="center" colspan="2">操作</td> </tr> <tr> <td><input type="text" name="ID" size="10" maxlength="10" class="input" ></td> <td><input type="text" name="NAME" size="10" maxlength="10" class="input" ></td> <td><input type="text" name="NUMBER" size="10" maxlength="10" class="input" ></td> <td><input type="text" name="REMARK" size="10" maxlength="10" class="input" ></td> <td><input name = "login" type = "submit" id = "login" value = "保存" class = "button"></td> </tr> </table> </div> </form> </body> </html>
com.util.DBUtil.java
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/sqltestdb?useUnicode=true&characterEncoding=UTF-8"; public static String db_user = "root"; public static String db_password = "751208"; 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(); } } } }
com.servlet.accountServlet
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 java.util.List; import com.bean.*; import com.dao.*; import com.util.DBUtil; @WebServlet("/accountServlet") public class accountServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public accountServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); String method = request.getParameter("method"); if(method.equals("add")) { add(request,response); }else if (method.equals("list")) { list(request,response); }else if(method.equals("update")) { update(request,response); }else if(method.equals("delete")) { delete(request,response); }else if (method.equals("list1")) { list1(request,response); } else if (method.equals("amend")) { list1(request,response); } } private void amend(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); int id = Integer.parseInt(request.getParameter("ID")); String name = request.getParameter("NAME"); String number = request.getParameter("NUMER"); String remark = request.getParameter("REMAEK"); Date date = new Date(); Timestamp timeStamp = new Timestamp(date.getTime()); int n=0; accountdao dao=new accountdao(); try { n=dao.update(id, name, number, timeStamp, remark); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if(n!=0) { request.setAttribute("message", "修改成功"); request.getRequestDispatcher("update.jsp").forward(request,response); }else { request.setAttribute("message", "修改失败"); request.getRequestDispatcher("update.jsp").forward(request,response); } } private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); int id = Integer.parseInt(request.getParameter("id")); accountdao dao=new accountdao(); int n=0; try { n=dao.deleteaccount(id); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if(n!=0) { request.setAttribute("message", "删除成功"); request.getRequestDispatcher("delete.jsp").forward(request,response); }else { request.setAttribute("message", "删除失败"); request.getRequestDispatcher("delete.jsp").forward(request,response); } } private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String id=request.getParameter("id"); System.out.println(id); request.setAttribute("id", id); request.getRequestDispatcher("update.jsp").forward(request,response); } private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO 自动生成的方法存根 request.setCharacterEncoding("UTF-8"); String name = request.getParameter("name"); String number = request.getParameter("number"); String remark = request.getParameter("remark"); System.out.println(remark); Date date = new Date(); Timestamp timeStamp = new Timestamp(date.getTime()); int n=0; accountdao dao=new accountdao(); try { n=dao.addaccount(name, number,timeStamp, remark); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if(n!=0) { request.setAttribute("message", "添加成功"); request.getRequestDispatcher("add.jsp").forward(request,response); }else { request.setAttribute("message", "添加失败"); request.getRequestDispatcher("add.jsp").forward(request,response); } } private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO 自动生成的方法存根 List<accountbook> account = accountdao.list(); request.setAttribute("account", account); request.getRequestDispatcher("list.jsp").forward(request,response); } private void list1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO 自动生成的方法存根 List<accountbook> account = accountdao.list(); request.setAttribute("account", account); request.getRequestDispatcher("delete.jsp").forward(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
updateServlet
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 com.dao.accountdao; /** * Servlet implementation class updateServlet */ @WebServlet("/updateServlet") public class updateServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public updateServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); int id = Integer.parseInt(request.getParameter("ID")); String name = request.getParameter("NAME"); String number = request.getParameter("NUMBER"); String remark = request.getParameter("REMARK"); System.out.println(name); System.out.println(id); System.out.println(number); System.out.println(remark); Date date = new Date(); Timestamp timeStamp = new Timestamp(date.getTime()); int n=0; accountdao dao=new accountdao(); try { n=dao.update(id, name, number, timeStamp, remark); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if(n!=0) { request.setAttribute("message", "修改成功"); request.getRequestDispatcher("update.jsp").forward(request,response); }else { request.setAttribute("message", "修改失败"); request.getRequestDispatcher("update.jsp").forward(request,response); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
com.dao.acountdao
package com.dao; import com.bean.*; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; 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.util.DBUtil; public class accountdao { public static int update(int id,String name,String number,Timestamp date,String remark) throws Exception { int n=0; String sql = "update family set name='" + name + "', number='" + number + "', remark='" + remark+"', time='"+date + "' where id='" + id + "'"; Connection conn = DBUtil.getConn(); Statement state = null; try { state = conn.createStatement(); n = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } return n; } public static int addaccount(String name,String number,Timestamp date,String remark) throws Exception//增加 { int count = 0; Connection conn = DBUtil.getConn(); String sql = "insert into family(name, number, remark,time) values('" + name + "','" + number + "','" + remark + "','"+date+"')"; Statement state = null; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); }catch (Exception e) { e.printStackTrace(); } return a; } public int deleteaccount(int id) throws Exception//删除 { int count=0; Connection conn = DBUtil.getConn(); try { String sql="delete from family where id=?"; PreparedStatement preparedStatement=conn.prepareStatement(sql); preparedStatement.setInt(1, id); count=preparedStatement.executeUpdate(); System.out.println(count); conn.close(); }catch (Exception e) { e.printStackTrace(); } return count; } public static List<accountbook> list() { List<accountbook> beans = new ArrayList<accountbook>(); Connection conn = DBUtil.getConn(); accountbook adminBean=null; try { String sql="select * from family"; Statement state = conn.createStatement(); ResultSet rs = state.executeQuery(sql); while(rs.next()) { adminBean = new accountbook(); adminBean.setId(rs.getInt("id")); adminBean.setName(rs.getString("name")); adminBean.setNumber(rs.getString("number")); adminBean.setTime(rs.getTimestamp("time")); adminBean.setRemark(rs.getString("remark")); beans.add(adminBean); } conn.close(); }catch (Exception e) { e.printStackTrace(); } return beans; } }
com.bean.accountbook
package com.bean; import java.util.Date; public class accountbook { private int id; private String name; private String number; private Date time; private String remark; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public Date getTime() { return time; } public void setTime(Date time) { this.time = time; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public int getId() { return id; } public void setId(int id) { this.id = id; } }