1.MVC是一种开发模式
Model-View-Controller
View 视图层
JSP,JS框架
实现用户界面
Controller 控制层
Servlet,Filter
实现接收请求和返回响应
Model 模型层
实体类,DAO
实现数据库访问和业务逻辑
2.创建的包
.
3.创建的jsp文件以及导入的数据库jar包
4.模型层的实体类,DAO类以及业务逻辑类
package com.hanqi.entity; //实体类 public class T_BALANCE { private String card_ID; private String user_ID; private String user_Name; private String password; private double T_BALANCE; private String state; private String create_time; public String getCard_ID() { return card_ID; } public void setCard_ID(String card_ID) { this.card_ID = card_ID; } public String getUser_ID() { return user_ID; } public void setUser_ID(String user_ID) { this.user_ID = user_ID; } public String getUser_Name() { return user_Name; } public void setUser_Name(String user_Name) { this.user_Name = user_Name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getCreate_time() { return create_time; } public void setCreate_time(String create_time) { this.create_time = create_time; } public T_BALANCE(String user_ID, String user_Name, String password, String state) { super(); this.user_ID = user_ID; this.user_Name = user_Name; this.password = password; this.state = state; } public double getT_BALANCE() { return T_BALANCE; } public void setT_BALANCE(double t_BALANCE) { T_BALANCE = t_BALANCE; } @Override public String toString() { return "T_BALANCE [card_ID=" + card_ID + ", user_ID=" + user_ID + ", user_Name=" + user_Name + ", password=" + password + ", T_BALANCE=" + T_BALANCE + ", state=" + state + ", create_time=" + create_time + "]"; } public T_BALANCE() { super(); // TODO Auto-generated constructor stub } }
package com.hanqi.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.hanqi.entity.T_BALANCE; import com.hanqi.utility.DBHepler; //数据库操作类 public class T_BALANCEDAO { // 查 // 单条查询 public T_BALANCE getT_BALANCE(String cardid) { T_BALANCE rtn = null; // 查询 Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = DBHepler.getDBHepler().getConnection(); String sql = "select * from T_BALANCE where card_id=?"; ps = conn.prepareStatement(sql); ps.setString(1, cardid); rs = ps.executeQuery(); if (rs.next()) { rtn = new T_BALANCE(); rtn.setCard_ID(cardid); rtn.setT_BALANCE(rs.getDouble("T_BALANCE")); rtn.setCreate_time(rs.getString("create_time")); rtn.setPassword(rs.getString("password")); rtn.setState(rs.getString("state")); rtn.setUser_ID(rs.getString("user_id")); rtn.setUser_Name(rs.getString("user_name")); } } catch (SQLException e) { e.printStackTrace(); } finally { try { ps.close(); } catch (SQLException e1) { e1.printStackTrace(); } try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return rtn; } //查询全部数据 public List<T_BALANCE>getAll() { List<T_BALANCE> rtn =new ArrayList<>(); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = DBHepler.getDBHepler().getConnection(); String sql = "select * from T_BALANCE"; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { T_BALANCE tb = new T_BALANCE(); tb.setCard_ID(rs.getString("card_id")); tb.setT_BALANCE(rs.getDouble("T_BALANCE")); tb.setCreate_time(rs.getString("create_time")); tb.setPassword(rs.getString("password")); tb.setState(rs.getString("state")); tb.setUser_ID(rs.getString("user_id")); tb.setUser_Name(rs.getString("user_name")); rtn.add(tb); } } catch (SQLException e) { e.printStackTrace(); } finally { try { ps.close(); } catch (SQLException e1) { e1.printStackTrace(); } try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return rtn; } // 增 //使用实体类接收数据 public int insertBankCard(T_BALANCE tb) { int rtn = 0; Connection conn = null; PreparedStatement ps = null; try { conn = DBHepler.getDBHepler().getConnection(); String sql = "insert into T_BALANCE(card_id,user_id,user_name,password,t_balance,create_time,state) values(?,?,?,?,?,?,?)"; ps = conn.prepareStatement(sql); ps.setString(1, tb.getCard_ID()); ps.setString(2, tb.getUser_ID()); ps.setString(3, tb.getUser_Name()); ps.setDouble(4, tb.getT_BALANCE()); ps.setString(5, tb.getPassword()); ps.setString(6, tb.getCreate_time()); ps.setString(7, tb.getState()); //执行插入数据 rtn =ps.executeUpdate(); //时间戳 //Timestamp ts =new Timestamp(tb.getCreate_time()) //表示精确时间 //ps.setTimestamp(6, ts); } catch (SQLException e) { e.printStackTrace(); } finally { try { ps.close(); } catch (SQLException e1) { e1.printStackTrace(); } try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return rtn; } // 改 public int updateBankCard(T_BALANCE tb) { int rtn=0; Connection conn = null; PreparedStatement ps = null; try { conn = DBHepler.getDBHepler().getConnection(); String sql ="update T_BALANCE set user_id=?," + "user_name=?, t_balance=? ,password=?, " + "create_time=? ,state=? where card_id=?"; ps = conn.prepareStatement(sql); ps.setString(1, tb.getUser_ID()); ps.setString(2, tb.getUser_Name()); ps.setDouble(3, tb.getT_BALANCE()); ps.setString(4, tb.getPassword()); ps.setString(5, tb.getCreate_time()); ps.setString(6, tb.getState()); ps.setString(7, tb.getCard_ID()); //执行插入数据 rtn =ps.executeUpdate(); //时间戳 //Timestamp ts =new Timestamp(tb.getCreate_time()) //表示精确时间 //ps.setTimestamp(6, ts); } catch (SQLException e) { e.printStackTrace(); } finally { try { ps.close(); } catch (SQLException e1) { e1.printStackTrace(); } try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return rtn; } // 删除 public int deleteBankCard(String cardid) { int rtn=0; Connection conn = null; PreparedStatement ps = null; try { conn = DBHepler.getDBHepler().getConnection(); String sql = "delete T_BALANCE where card_id=?"; ps = conn.prepareStatement(sql); ps.setString(1, cardid); rtn = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { try { ps.close(); } catch (SQLException e1) { e1.printStackTrace(); } try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return rtn; } }
package com.hanqi.Service; import java.util.List; import com.hanqi.dao.T_BALANCEDAO; import com.hanqi.entity.T_BALANCE; //业务逻辑类 public class BankCardService { //取单条数据 public T_BALANCE getT_BALANCE(String cardid) { return new T_BALANCEDAO().getT_BALANCE(cardid); } //验证登录方法 public T_BALANCE CheckLogin(String cardid,String password) { T_BALANCE rtn=null; //得到卡信息 T_BALANCE tb=new T_BALANCEDAO().getT_BALANCE(cardid); System.out.println(tb); if(tb!=null) { //验证数据有效性 if(tb.getCard_ID().equals(cardid) &&tb.getPassword().equals(password)) { rtn =tb; } } return rtn; } //添加数据的方法 public boolean addBankCard(T_BALANCE tb) { boolean rtn = false; //生成cardid String cardid =((int)(Math.random()*1000000)+"000000").substring(0,6); tb.setT_BALANCE(0); tb.setCreate_time("2011-11-22"); tb.setCard_ID(cardid); //调用dao.保存数据 rtn=new T_BALANCEDAO().insertBankCard(tb)==1; return rtn; } public List<T_BALANCE>getAllT_BALANCE() { return new T_BALANCEDAO().getAll(); } //删除 public void removeBankCard(String cardid) throws Exception { //过滤不允许删除的数据 if(!cardid.equals("459333")){ new T_BALANCEDAO().deleteBankCard(cardid); } else { throw new Exception("管理员信息不能被删除!"); } } //修改 public boolean editBankCard(T_BALANCE tb) { return new T_BALANCEDAO().updateBankCard(tb)==1; } }
5.工具类
package com.hanqi.utility; import java.sql.Connection; import java.sql.SQLException; import com.mchange.v2.c3p0.ComboPooledDataSource; //单例模式的数据库工具类 //返回数据库连接 public class DBHepler { //1.隐藏默认构造方法 private DBHepler() { } //2.静态的实例 private static DBHepler dh; //3.返回实例的静态方法 public static DBHepler getDBHepler() { if(dh==null) { dh = new DBHepler(); } return dh; } private ComboPooledDataSource cps = new ComboPooledDataSource("helloc3p0"); //返回连接的方法 public Connection getConnection() throws SQLException { return cps.getConnection(); } }
6.Servlet检查类以及过滤类
package com.hanqi.Controller; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class MyFilter implements Filter { public MyFilter() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //设置编码 request.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub } }
package com.hanqi.Controller; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.hanqi.Service.BankCardService; import com.hanqi.entity.T_BALANCE; public class CheckLoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; public CheckLoginServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //response.getWriter().append("Served at: ").append(request.getContextPath()); String cardid =request.getParameter("cardid"); String password =request.getParameter("password"); //对get,post同时有效 //username = new String(request.getParameter(username).getBytes("ISO-8859-1"),"UTF-8"); //检查参数 if(cardid == null||password==null|| cardid.trim().equals("")||password.trim().equals("")) { response.getWriter().println("请正确登录!"); response.setHeader("refresh", "3;url=Login.jsp"); } else { //验证参数 //调用Service层 T_BALANCE tb =new BankCardService().CheckLogin(cardid, password); if(tb!=null) { //创建session HttpSession hs = request.getSession(); hs.setAttribute("T_BALANCE", tb); response.sendRedirect("Main.jsp"); } else { response.getWriter().println("用户名或密码错误!"); response.setHeader("refresh", "3;url=Login.jsp"); } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package com.hanqi.Controller; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.hanqi.Service.BankCardService; import com.hanqi.entity.T_BALANCE; public class SaveBankCardServlet extends HttpServlet { private static final long serialVersionUID = 1L; public SaveBankCardServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //完成卡数据的保存 String userid = request.getParameter("userid"); String username = request.getParameter("username"); String password = request.getParameter("password"); String state = request.getParameter("state"); //检查参数 if(userid == null||password==null|| username ==null ||state==null|| userid.trim().equals("")||password.trim().equals("") ||username.trim().equals("")||state.trim().equals("")) { response.getWriter().println("请正确提交!"); response.setHeader("refresh", "3;url=Main.jsp"); } //调用模型层 else { T_BALANCE tb = new T_BALANCE(userid,username,password,state ); if (new BankCardService().addBankCard(tb)) { response.getWriter().println("数据添加成功!"); response.setHeader("refresh", "3;url=Main.jsp"); } else { response.getWriter().println("数据添加失败!"); response.setHeader("refresh", "3;url=Main.jsp"); } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.hanqi.Controller; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.hanqi.Service.BankCardService; public class DeleteBankCard extends HttpServlet { private static final long serialVersionUID = 1L; public DeleteBankCard() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String cardid = request.getParameter("cardid"); if(cardid==null||cardid.trim().equals("")) { response.getWriter().println("请正确提交"); response.setHeader("refresh", "3;url=Main.jsp"); } else { try { new BankCardService().removeBankCard(cardid); response.getWriter().println("删除成功"); } catch (Exception e) { response.getWriter().print(e.getMessage()); } response.setHeader("refresh", "3;Main.jsp"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package com.hanqi.Controller; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.hanqi.Service.BankCardService; import com.hanqi.entity.T_BALANCE; /** * Servlet implementation class UpdateBankCardServlet */ public class UpdateBankCardServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public UpdateBankCardServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //完成卡数据的保存 String cardid = request.getParameter("cardid"); String userid = request.getParameter("userid"); String username = request.getParameter("username"); String password = request.getParameter("password"); String balance = request.getParameter("balance"); String state = request.getParameter("state"); String createtime = request.getParameter("createtime"); //检查参数 if(userid == null||password==null|| username ==null ||state==null|| userid.trim().equals("")||password.trim().equals("") ||username.trim().equals("")||state.trim().equals("")) { response.getWriter().println("请正确提交!"); response.setHeader("refresh", "3;url=Main.jsp"); } //调用模型层 else { T_BALANCE tb = new T_BALANCE(userid,username,password,state ); tb.setCard_ID(cardid); tb.setT_BALANCE(Double.parseDouble(balance)); tb.setCreate_time(createtime); if (new BankCardService().editBankCard(tb)) { response.getWriter().println("数据保存成功!"); response.setHeader("refresh", "3;url=EditBankCard.jsp"); } else { response.getWriter().println("数据保存失败!"); response.setHeader("refresh", "3;url=EditBankCard.jsp"); } } } /** * @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); } }
登录界面
<%@ 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> <form action="CheckLoginServlet" method="post"> 卡号:<input type="text" name="cardid" value=""><br> 密码:<input type="password" name="password"><br> <input type="submit" value="登录"> </form> </body> </html>
主界面
<%@page import="java.text.SimpleDateFormat"%> <%@page import="java.util.List"%> <%@page import="com.hanqi.Service.BankCardService"%> <%@page import="com.hanqi.entity.T_BALANCE"%> <%@ 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> 主页面 <br> <% Object obj =session.getAttribute("T_BALANCE"); if(obj==null) { out.print("会话超时或未登录"); response.setHeader("refresh", "3;url=Login.jsp"); } else { T_BALANCE tb =(T_BALANCE)obj; out.print("欢迎登录"+tb.getUser_Name()); } %> <br> 添加卡记录 <br> <form action="SaveBankCardServlet"method="post"> 身份证号码:<input type="text" name="userid"><br> 用户名称:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> 状态:<input type="text" name="state"><br> <input type="submit" value="保存"> </form> <br> <br> 银行卡数据列表 <br> <br> <table cellpadding="0" cellspacing="0" border="1"> <tr> <td>序号</td> <td>卡号</td> <td>用户id</td> <td>用户名</td> <td>余额</td> <td>状态</td> <td>创建时间</td> <td>操作</td> </tr> <% //获取所有数据 List<T_BALANCE>ls = new BankCardService().getAllT_BALANCE(); //遍历结果集 int i=0; //时间格式转换的工具 //SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm"); for(T_BALANCE tb :ls) { i++; out.print("<tr>"); out.print("<td>"); out.print(i); out.print("</td>"); out.print("<td>"); out.print(tb.getCard_ID()); out.print("</td>"); out.print("<td>"); out.print(tb.getUser_ID()); out.print("</td>"); out.print("<td>"); out.print(tb.getUser_Name()); out.print("</td>"); out.print("<td>"); out.print(tb.getT_BALANCE()); out.print("</td>"); out.print("<td>"); out.print(tb.getState()); out.print("</td>"); out.print("<td>"); out.print(tb.getCreate_time()); out.print("</td>"); out.print("<td>"); out.print("【<a href='EditBankCard.jsp?cardid="+tb.getCard_ID()+"'>修改</a>】 【<a href='DeleteBankCard?cardid="+tb.getCard_ID()+"'>删除</a>】"); out.print("</td>"); out.print("</tr>"); } %> </table> </body> </html>
修改界面
<%@page import="com.hanqi.entity.T_BALANCE"%> <%@page import="com.hanqi.Service.BankCardService"%> <%@ 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> <% //接收信息 String cardid = request.getParameter("cardid"); T_BALANCE tb=null; if(cardid==null||cardid.trim().equals("")) { response.getWriter().print("请正确访问"); response.setHeader("refresh", "3;url=Main.jsp"); } else { tb=new BankCardService().getT_BALANCE(cardid); if(tb ==null) { response.getWriter().print("获取数据失败"); response.setHeader("refresh", "3;url=Main.jsp"); %> 编辑银行卡信息<br><br> <form action="UpdateBankCardServlet"method="post"> 卡号:<input type="text" name="cardid" value="<%=tb.getCard_ID()%>" readonly="readonly"><br> 身份证号:<input type="text" name="userid"value="<%= tb.getUser_ID()%>"><br> 用户名:<input type="text" name="username"value="<%=tb.getUser_Name() %>"><br> 密码:<input type="password" name="password"value="<%=tb.getPassword()%>"><br> 余额:<input type="text" name="balance" value="<%=tb.getT_BALANCE() %>"><br> 状态:<input type="text" name="state" value="<%=tb.getState()%>"><br> 创建时间:<input type="text" name="createtime" value="<%=tb.getCreate_time()%>"><br> <br><br> <input type="submit" value="保存"> </form> <% } } %> </body> </html>