MVC实现增删改查的小例子.docx

上传人:b****6 文档编号:4336202 上传时间:2022-11-29 格式:DOCX 页数:19 大小:18.21KB
下载 相关 举报
MVC实现增删改查的小例子.docx_第1页
第1页 / 共19页
MVC实现增删改查的小例子.docx_第2页
第2页 / 共19页
MVC实现增删改查的小例子.docx_第3页
第3页 / 共19页
MVC实现增删改查的小例子.docx_第4页
第4页 / 共19页
MVC实现增删改查的小例子.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

MVC实现增删改查的小例子.docx

《MVC实现增删改查的小例子.docx》由会员分享,可在线阅读,更多相关《MVC实现增删改查的小例子.docx(19页珍藏版)》请在冰豆网上搜索。

MVC实现增删改查的小例子.docx

MVC实现增删改查的小例子

MVC

SearchList.jsp

92期学员信息如下


id

姓名

密码

性别

年龄

操作

useBeanid="sBean"class="com.jq92.bean.SqlBean"/>

useBeanid="jBean"class="com.jq92.bean.JavaBean"/>

<%

Stringsql="select*fromstudentorderbyid";

java.util.Listlist=sBean.getSearch(sql);

for(Iteratorit=list.iterator();it.hasNext();){

//获得JavaBean的一个对象

jBean=(com.jq92.bean.JavaBean)it.next();

%>

<%=jBean.getId()%>

<%=jBean.getName()%>

<%=jBean.getPassword()%>

<%=jBean.getSex()%>

<%=jBean.getAge()%>

id=<%=jBean.getId()%>">删除 

id=<%=jBean.getId()%>">更新

<%

}

%>

返回

 

Update.jsp

您要更新的数据如下:


Id:

"readonly>

姓名:

">

密码:

">

性别:

>男

>女

年龄:

">

查询

Insert.jsp

92期学院管理页面


姓名:

密码:

性别:

年龄:

查询

Error.jsp

对不起,您<%=request.getAttribute("error")%>失败

Servlet部分

Update.java

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

//获得前台表单信息

Stringsid=request.getParameter("id");

intid=Integer.parseInt(sid);

Stringname=request.getParameter("name");

Stringpassword=request.getParameter("password");

Stringsex=request.getParameter("sex");

StringSage=request.getParameter("age");

intage=Integer.parseInt(Sage);

//封装到JavaBean对象中去

JavaBeanjBean=newJavaBean();

jBean.setId(id);

jBean.setName(name);

jBean.setPassword(password);

jBean.setSex(sex);

jBean.setAge(age);

Stringsql="updatestudentsetname=?

password=?

sex=?

age=?

whereid=?

";

SqlBeansBean=newSqlBean();

intcount=sBean.getUpdate(sql,jBean);

Stringurl="";

if(count>0){

url="SearchList.jsp";

}

else{

url="Error.jsp";

request.setAttribute("error","更新");

}

request.getRequestDispatcher(url).forward(request,response);

}

SearchById.java

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

//获得id

Stringsid=request.getParameter("id");

intid=Integer.parseInt(sid);

Stringsql="select*fromstudentwhereid=?

";

SqlBeansBean=newSqlBean();

JavaBeanjBean=sBean.getSearchById(sql,id);

//ID

request.setAttribute("id",jBean.getId());

//姓名

request.setAttribute("name",jBean.getName());

//密码

request.setAttribute("password",jBean.getPassword());

//性别

Stringsex="";

Stringman="";

Stringwoman="";

if(jBean.getSex()!

=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.java

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

//获取前台页面信息

Stringname=request.getParameter("name");

Stringpassword=request.getParameter("password");

Stringsex=request.getParameter("sex");

StringSage=request.getParameter("age");

intage=Integer.parseInt(Sage);

//封装到JavaBean对象中

com.jq92.bean.JavaBeanjBean=newcom.jq92.bean.JavaBean();

jBean.setName(name);

jBean.setPassword(password);

jBean.setSex(sex);

jBean.setAge(age);

//调用模型层

Stringsql="insertintostudentvalues(?

?

?

?

)";

SqlBeansBean=newSqlBean();

intcount=sBean.getInsert(sql,jBean);

Stringurl="";

if(count>0){

url="SearchList.jsp";

}

else{

url="Error.jsp";

request.setAttribute("error","注册");

}

//转发

request.getRequestDispatcher(url).forward(request,response);

}

Delete.java

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

//获得删除超链接传来的参数

StringsId=request.getParameter("id");

intid=Integer.parseInt(sId);

//调用模型层删除方法

Stringsql="deletefromstudentwhereid=?

";

SqlBeansBean=newSqlBean();

intcount=sBean.getDelete(sql,id);

Stringurl="";

if(count>0){

url="SearchList.jsp";

}

else{

url="Error.jsp";

request.setAttribute("error","删除");

}

request.getRequestDispatcher(url).forward(request,response);

}

JavaBean.java

packagecom.jq92.bean;

publicclassJavaBean{

privateintid;

privateStringname;

privateStringpassword;

privateStringsex;

privateintage;

publicintgetAge(){

returnage;

}

publicvoidsetAge(intage){

this.age=age;

}

publicintgetId(){

returnid;

}

publicvoidsetId(intid){

this.id=id;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicStringgetPassword(){

returnpassword;

}

publicvoidsetPassword(Stringpassword){

this.password=password;

}

publicStringgetSex(){

returnsex;

}

publicvoidsetSex(Stringsex){

this.sex=sex;

}

}

SqlBean.java

packagecom.jq92.bean;

importjava.sql.Connection;

importjava.sql.DriverManager;

importjava.sql.PreparedStatement;

importjava.sql.ResultSet;

importjava.sql.SQLException;

importjava.util.ArrayList;

importjava.util.List;

publicclassSqlBean{

Connectioncon;

PreparedStatementpre;

ResultSetrs;

publicSqlBean(){

try{

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

}catch(ClassNotFoundExceptione){

//TODO自动生成catch块

e.printStackTrace();

}

try{

con=DriverManager.getConnection("jdbc:

microsoft:

sqlserver:

//localhost:

1433;DatabaseName=jq92Model2","sa","sa");

}catch(SQLExceptione){

//TODO自动生成catch块

e.printStackTrace();

}

}

publicintgetInsert(Stringsql,JavaBeanjBean){

intcount=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.executeUpdate();

}catch(SQLExceptione){

//TODO自动生成catch块

e.printStackTrace();

}

finally{

try{

pre.close();

con.close();

}catch(SQLExceptione){

//TODO自动生成catch块

e.printStackTrace();

}

}

returncount;

}

publicintgetDelete(Stringsql,intid){

intcount=0;

try{

pre=con.prepareStatement(sql);

pre.setInt(1,id);

count=pre.executeUpdate();

}catch(SQLExceptione){

//TODO自动生成catch块

e.printStackTrace();

}

finally{

try{

pre.close();

con.close();

}catch(SQLExceptione){

//TODO自动生成catch块

e.printStackTrace();

}

}

returncount;

}

publicJavaBeangetSearchById(Stringsql,intid){

JavaBeanjBean=newJavaBean();

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(SQLExceptione){

//TODO自动生成catch块

e.printStackTrace();

}

returnjBean;

}

publicintgetUpdate(Stringsql,JavaBeanjBean){

intcount=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(SQLExceptione){

//TODO自动生成catch块

e.printStackTrace();

}

finally{

try{

pre.close();

con.close();

}catch(SQLExceptione){

//TODO自动生成catch块

e.printStackTrace();

}

}

returncount;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书 > 辩护词

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1