MVC模式
M:Model,即模型,对于JavaBean
V:View, 即试图,对应JSP页面
C:Controller,即控制器,对应Servlet
1. 以下为MVC实现一个简单的 增删改查 功能
1> 显示记录
2> 增加一条记录
3> 修改一条记录
4> 删除一条记录
程序源代码:
M层:模型层
1.封装一条信息的所有属性JavaBean.java ,即VO
package muta.bean; /** * @author help *封装一条信息的所有属性 */ public class JavaBean { private int id; private String name; private String password; private String sex; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } |
2.封装数据库连接以及操作JavaBean所用到的方法,即DAO
package muta.bean; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; /** * @author help *操作数据库的方法 */ public class SqlBean { Connection con; PreparedStatement pre; ResultSet rs; public SqlBean() { try { Class.forName("sql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/MyServlet","root","122828"); } catch (SQLException e) { e.printStackTrace(); } } /** * @author help * *插入新的一条记录 * @return */ public int getInsert(String sql,JavaBean jBean) { int count =0; try { pre = con.prepareStatement(sql); pre.setString(Name()); pre.setString(Password()); pre.setString(Sex()); pre.setInt(Age()); count=pre.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { try { pre.close(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return count; } /** * @author help * *删除一条记录 * @return */ public int getDelete(String sql,int id) { int count =0; try { pre = con.prepareStatement(sql); pre.setInt(1, id); count=pre.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { try { pre.close(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return count; } /** * @author help * *根据ID查询某一条记录 * @return */ public JavaBean getSearchById(String sql,int id) { JavaBean jBean = new JavaBean(); try { pre = con.prepareStatement(sql); pre.setInt(1, id); rs=pre.executeQuery(); while(rs.next()) { jBean.setId(rs.getInt("id")); jBean.setName(rs.getString("name")); jBean.setPassword(rs.getString("password")); jBean.setSex(rs.getString("sex")); jBean.setAge(rs.getInt("age")); } } catch (SQLException e) { jfinal增删改查 e.printStackTrace(); } return jBean; } /** * @author help * *更新某一条记录 * @return */ public int getUpdate(String sql,JavaBean jBean) { int count =0; try { pre = con.prepareStatement(sql); pre.setInt(Id()); pre.setString(Name()); pre.setString(Password()); pre.setString(Sex()); pre.setInt(Age()); count = pre.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { pre.close(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return count; } /** * @author help * *显示所有记录 * @return */ public List getSearch(String sql) { List list = new ArrayList(); //获取prepareStatement对象 try { pre = con.prepareStatement(sql); rs =pre.executeQuery(); while(rs.next()) { JavaBean jBean =new JavaBean(); jBean.setId(rs.getInt("id")); jBean.setName(rs.getString("name")); jBean.setPassword(rs.getString("password")); jBean.setSex(rs.getString("sex")); jBean.setAge(rs.getInt("age")); list.add(jBean); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { pre.close(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return list; } } |
V层:试图层
1. 显示记录集的页面 SearchList.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = ContextPath(); String basePath = Scheme()+"://"+ServerName()+":"+ServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>显示记录</title> </head> <body> <center > <font color=red size=72>学生信息如下:</font> <hr> <table border=1 bgColor="#ffffff" width="500px" height="100px"> <tr> <td>ID</td> <td>姓名</td> <td>密码</td> <td>性别</td> <td>年龄</td> <td><center>操作</center></td> </tr> <jsp:useBean id="sBean"class="muta.bean.SqlBean"/> <jsp:useBean id="jBean" class="muta.bean.JavaBean"/> <% String sql ="select * from student order by id"; java.util.List list =Search(sql); for(java.util.Iterator it =list.iterator();it.hasNext();) { //获取一个JavaBean对象 jBean =(muta.bean.(); %> <tr> <td><%=Id() %></td> <td><%=Name() %></td> <td><%=Password() %></td> <td><%=Sex() %></td> <td><%=Age() %></td> <td> <a href="Insert.jsp">增加</a> <a href="Delete?id=<%=Id()%>">删除</a> <a href="SearchById?id=<%=Id()%>">更新</a> </td> </tr> <% }%> </table> </center> </body> </html> |
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论