1、MVC实现增删改查的小例子MVCSearchList.jsp92期学员信息如下 id 姓名 密码 性别 年龄 操 作 a href=Delete?id=删除 a href=SearchById?id=更新 返回 Update.jsp 您要更新的数据如下: Id: input name=id value= readonly 姓名: input name=name value= 密码: input type=password name=password value= 性别: input type=radio name=sex value=男 男 input type=radio nam
2、e=sex value=女 女 年龄: input type=text name=age value= 查询 Insert.jsp 92期学院管理页面 姓名: 密码: 性别: 男 女 年龄: 查询 Error.jsp 对不起,您失败 Servlet部分Update.javapublic void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException /获得前台表单信息 String sid=request.getParameter(id); in
3、t id=Integer.parseInt(sid); String name=request.getParameter(name); String password=request.getParameter(password); String sex=request.getParameter(sex); String Sage=request.getParameter(age); int age=Integer.parseInt(Sage); /封装到JavaBean对象中去 JavaBean jBean=new JavaBean(); jBean.setId(id); jBean.setN
4、ame(name); jBean.setPassword(password); jBean.setSex(sex); jBean.setAge(age); String sql=update student set name=?,password=?,sex=?,age=? where id=?; SqlBean sBean=new SqlBean(); int count=sBean.getUpdate(sql, jBean); String url=; if(count0) url=SearchList.jsp; else url=Error.jsp; request.setAttribu
5、te(error, 更新); request.getRequestDispatcher(url).forward(request, response); SearchById.javapublic void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException /获得id String sid=request.getParameter(id); int id=Integer.parseInt(sid); String sql=select * f
6、rom student where id=?; SqlBean sBean=new SqlBean(); JavaBean jBean=sBean.getSearchById(sql, id); /ID request.setAttribute(id, jBean.getId(); /姓名 request.setAttribute(name,jBean.getName(); /密码 request.setAttribute(password, jBean.getPassword(); /性别 String sex=; String man=; String woman=; if(jBean.g
7、etSex()!=null) sex=jBean.getSex().trim(); if(sex.equals(男) man=checked; else woman=checked; request.setAttribute(man,man); request.setAttribute(woman,woman); /年龄 request.setAttribute(age, jBean.getAge(); /转发 request.getRequestDispatcher(Update.jsp).forward(request, response); Insert.javapublic void
8、doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException /获取前台页面信息 String name=request.getParameter(name); String password=request.getParameter(password); String sex=request.getParameter(sex); String Sage=request.getParameter(age); int age=Integer.parseIn
9、t(Sage); /封装到JavaBean对象中 com.jq92.bean.JavaBean jBean=new com.jq92.bean.JavaBean(); jBean.setName(name); jBean.setPassword(password); jBean.setSex(sex); jBean.setAge(age); /调用模型层 String sql=insert into student values(?,?,?,?); SqlBean sBean=new SqlBean(); int count=sBean.getInsert(sql, jBean); Strin
10、g url=; if(count0) url=SearchList.jsp; else url=Error.jsp; request.setAttribute(error,注册); /转发 request.getRequestDispatcher(url).forward(request, response); Delete.javapublic void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException /获得删除超链接传来的参数 Stri
11、ng sId=request.getParameter(id); int id=Integer.parseInt(sId); /调用模型层删除方法 String sql=delete from student where id=?; SqlBean sBean=new SqlBean(); int count=sBean.getDelete(sql, id); String url=; if(count0) url=SearchList.jsp; else url=Error.jsp; request.setAttribute(error, 删除); request.getRequestDis
12、patcher(url).forward(request, response); JavaBean.javapackage com.jq92.bean;public class JavaBean private int id; private String name; private String password; private String sex; private int age; public int getAge() return age; public void setAge(int age) this.age = age; public int getId() return i
13、d; 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(Str
14、ing sex) this.sex = sex; SqlBean.javapackage com.jq92.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;public class SqlBean Connection con; PreparedS
15、tatement pre; ResultSet rs; public SqlBean() try Class.forName(com.microsoft.jdbc.sqlserver.SQLServerDriver); catch (ClassNotFoundException e) / TODO 自动生成 catch 块 e.printStackTrace(); try con=DriverManager.getConnection(jdbc:microsoft:sqlserver:/localhost:1433;DatabaseName=jq92Model2,sa,sa); catch (
16、SQLException e) / TODO 自动生成 catch 块 e.printStackTrace(); public int getInsert(String sql,JavaBean jBean) int count=0; try pre=con.prepareStatement(sql); pre.setString(1,jBean.getName(); pre.setString(2,jBean.getPassword(); pre.setString(3, jBean.getSex(); pre.setInt(4, jBean.getAge(); count=pre.exec
17、uteUpdate(); catch (SQLException e) / TODO 自动生成 catch 块 e.printStackTrace(); finally try pre.close(); con.close(); catch (SQLException e) / TODO 自动生成 catch 块 e.printStackTrace(); return count; public int getDelete(String sql,int id) int count=0; try pre=con.prepareStatement(sql); pre.setInt(1, id);
18、count=pre.executeUpdate(); catch (SQLException e) / TODO 自动生成 catch 块 e.printStackTrace(); finally try pre.close(); con.close(); catch (SQLException e) / TODO 自动生成 catch 块 e.printStackTrace(); return count; public JavaBean getSearchById(String sql,int id) JavaBean jBean=new JavaBean(); try pre=con.p
19、repareStatement(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) / TODO 自动生成 catch 块 e.printStackT
20、race(); return jBean; public int getUpdate(String sql,JavaBean jBean) int count=0; try pre=con.prepareStatement(sql); pre.setString(1,jBean.getName(); pre.setString(2, jBean.getPassword(); pre.setString(3, jBean.getSex(); pre.setInt(4, jBean.getAge(); pre.setInt(5, jBean.getId(); count=pre.executeUpdate(); catch (SQLException e) / TODO 自动生成 catch 块 e.printStackTrace(); finally try pre.close(); con.close(); catch (SQLException e) / TODO 自动生成 catch 块 e.printStackTrace(); return count;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1