1、MVC模式 M:Model,即模型,对于 JavaBean V:View, 即试图,对应 JSP页面 C:Controller,即控制器,对应 Servlet1. 以下为 MVC实现一个简单的 增删改查 功能1 显示记录2 增加一条记录3 修改一条记录4 删除一条记录程序源代码:M层:模型层1.封装一条信息的所有属性 JavaBean.java ,即 VOpackage muta.bean;/* author help*封装一条信息的所有属性*/public class JavaBean private int id;private String name;private String pas
2、sword;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 = pass
3、word;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 所用到的方法,即 DAOpackage muta.bean;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStateme
4、nt;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(“com.mysql.jdbc.Driver“); catch (ClassNotFoundException e) e.pr
5、intStackTrace();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(1,
6、jBean.getName();pre.setString(2,jBean.getPassword();pre.setString(3,jBean.getSex();pre.setInt(4,jBean.getAge();count=pre.executeUpdate(); catch (SQLException e) e.printStackTrace();finallytry pre.close();con.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();return
7、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();finallytry pre.close();con.close(); catch (SQLException e) / TODO Auto-generated cat
8、ch blocke.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.get
9、String(“name“);jBean.setPassword(rs.getString(“password“);jBean.setSex(rs.getString(“sex“);jBean.setAge(rs.getInt(“age“); catch (SQLException e) e.printStackTrace();return jBean;/* author help* *更新某一条记录* return */public int getUpdate(String sql,JavaBean jBean) int count =0;try pre = con.prepareState
10、ment(sql);pre.setInt(5,jBean.getId();pre.setString(1,jBean.getName();pre.setString(2,jBean.getPassword();pre.setString(3,jBean.getSex();pre.setInt(4,jBean.getAge();count = pre.executeUpdate(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();finallytry pre.close();con.clos
11、e(); catch (SQLException e) / TODO Auto-generated catch blocke.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 =
12、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 blocke.printStackTrace();finallytry pre.close();
13、con.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();return list;V层:试图层1.显示记录集的页面 SearchList.jsp显示记录学生信息如下:ID姓名密码性别年龄操作 增加“删除“更新2.插入页面 Insert.jsp插入新值学生管理页面姓名: 密码: 性别: 男女年龄: 查询3.更新页面 Update.jsp更新学生管理页面学生ID:“ readonly学生姓名: “ 学生密码: “ 学生性别: 男女学生年龄: “ 查询4.出错页面 Error.js
14、p出错啦! 对不起!您失败C层:控制层Servlet1.显示记录集的 Servlet-SearchById.javapackage muta.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import muta.bean.JavaBean;i
15、mport muta.bean.SqlBean;public class SearchById extends HttpServlet private static final long serialVersionUID = 1L;/* The doDelete method of the servlet. * This method is called when a HTTP delete request is received.* * param request the request send by the client to the server* param response the
16、 response send by the server to the client* throws ServletException if an error occurred* throws IOException if an error occurred*/public void doDelete(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException / Put your code here/* The doGet method of the servlet
17、. * This method is called when a form has its tag value method equals to get.* * param request the request send by the client to the server* param response the response send by the server to the client* throws ServletException if an error occurred* throws IOException if an error occurred*/public voi
18、d doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException doPost(request,response);/* The doPost method of the servlet. * This method is called when a form has its tag value method equals to post.* * param request the request send by the client to the serv
19、er* param response the response send by the server to the client* throws ServletException if an error occurred* throws IOException if an error occurred*/public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException response.setContentType(“text/html
20、“);request.setCharacterEncoding(“utf-8“);response.setCharacterEncoding(“utf-8“);/获取用户IDString sid = request.getParameter(“id“);int id =Integer.parseInt(sid);String sql =“select * from student where id=?“;SqlBean sBean = new SqlBean();JavaBean jBean = sBean.getSearchById(sql, id);/用户IDrequest.setAttr
21、ibute(“id“,jBean.getId();/用户姓名request.setAttribute(“name“,jBean.getName();/用户密码request.setAttribute(“password“,jBean.getPassword();/用户性别String sex=“;String man=“;String woman=“;if(jBean.getSex()!=null)sex=jBean.getSex().trim();if(sex.equals(“男“)man =“checked“;elsewoman =“checked“;request.setAttribut
22、e(“man“,man);request.setAttribute(“woman“,woman);/用户年龄request.setAttribute(“age“,jBean.getAge();/转发request.getRequestDispatcher(“Update.jsp“).forward(request, response);2.增加记录的 Servlet-Insert.javapackage muta.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import muta.bean.SqlBean;import muta.bean.JavaBean;public class Insert extends HttpServlet