admin 管理员组文章数量: 887032
2024年1月13日发(作者:fprintf s)
MVC模式
M:Model,即模型,对于JavaBean
V:View, 即试图,对应JSP页面
C:Controller,即控制器,对应Servlet
1. 以下为MVC实现一个简单的 增删改查 功能
1> 显示记录
2> 增加一条记录
3> 修改一条记录
4> 删除一条记录
程序源代码:
M层:模型层
1.封装一条信息的所有属性 ,即VO
package ;
/**
* @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) {
= id;
}
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
rd = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
= sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
= age;
}
}
2.封装数据库连接以及操作JavaBean所用到的方法,即DAO
package ;
import tion;
import Manager;
import edStatement;
import Set;
import eption;
import ist;
import ;
/**
* @author help
*操作数据库的方法
*/
public class SqlBean {
Connection con;
PreparedStatement pre;
ResultSet rs;
public SqlBean()
{
try {
e("");
} catch (ClassNotFoundException e) {
tackTrace();
}
try {
con=nection("jdbc:mysql://127.0.0.1:3306/MyServlet","root","122828");
} catch (SQLException e) {
tackTrace();
}
}
/**
* @author help
*
*插入新的一条记录
* @return
*/
public int getInsert(String sql,JavaBean jBean)
{
int count =0;
try {
pre = eStatement(sql);
ing(1,e());
ing(2,sword());
ing(3,());
(4,());
count=eUpdate();
} catch (SQLException e) {
tackTrace();
}
finally
{
try {
();
();
} catch (SQLException e) {
// TODO Auto-generated catch block
tackTrace();
}
}
return count;
}
/**
* @author help
*
*删除一条记录
* @return
*/
public int getDelete(String sql,int id)
{
int count =0;
try {
pre = eStatement(sql);
(1, id);
count=eUpdate();
} catch (SQLException e) {
tackTrace();
}
finally
{
try {
();
();
} catch (SQLException e) {
// TODO Auto-generated catch block
tackTrace();
}
}
return count;
}
/**
* @author help
*
*根据ID查询某一条记录
* @return
*/
public JavaBean getSearchById(String sql,int id)
{
JavaBean jBean = new JavaBean();
try {
pre = eStatement(sql);
(1, id);
rs=eQuery();
while(())
{
(("id"));
e(ing("name"));
sword(ing("password"));
(ing("sex"));
(("age"));
}
} catch (SQLException e)
{
tackTrace();
}
return jBean;
}
/**
* @author help
*
*更新某一条记录
* @return
*/
public int getUpdate(String sql,JavaBean jBean)
{
int count =0;
try {
}
pre = eStatement(sql);
(5,());
ing(1,e());
ing(2,sword());
ing(3,());
(4,());
count = eUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
tackTrace();
}
finally
{
try {
();
();
} catch (SQLException e) {
// TODO Auto-generated catch block
tackTrace();
}
}
return count;
/**
* @author help
*
*显示所有记录
* @return
*/
public List getSearch(String sql)
{
List list = new ArrayList();
//获取prepareStatement对象
try {
pre = eStatement(sql);
rs =eQuery();
while(())
{
JavaBean jBean =new JavaBean();
(("id"));
}
}
e(ing("name"));
sword(ing("password"));
(ing("sex"));
(("age"));
(jBean);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
tackTrace();
}
finally
{
try {
();
();
} catch (SQLException e) {
// TODO Auto-generated catch block
tackTrace();
}
}
return list;
V层:试图层
1. 显示记录集的页面
<%@ page language="java" import=".*" pageEncoding="UTF-8"%>
<%
String path = textPath();
String basePath = eme()+"://"+verName()+":"+verPort()+path+"/";
%>
学生信息如下:
ID | 姓名 | 密码 | 性别 | 年龄 | |
<%=() %> | <%=e() %> | <%=sword() %> | <%=() %> | <%=() %> |
|
2. 插入页面
<%@ page language="java" import=".*" pageEncoding="UTF-8"%>
<%
String path = textPath();
String basePath =
eme()+"://"+verName()+":"+verPort()+path+"/";
%>
学生管理页面
3. 更新页面
<%@ page language="java" import=".*" pageEncoding="UTF-8"%>
<%
String path = textPath();
String basePath =
eme()+"://"+verName()+":"+verPort()+path+"/";
%>
学生管理页面
4. 出错页面
<%@ page language="java" import=".*" pageEncoding="UTF-8"%>
<%
String path = textPath();
String basePath = eme()+"://"+verName()+":"+verPort()+path+"/";
%>
对不起!您<%=ribute("error") %>失败
C层:控制层—Servlet
1.显示记录集的
package t;
import ption;
import tException;
import rvlet;
import rvletRequest;
import rvletResponse;
import an;
import n;
public class SearchById extends HttpServlet {
tentType("text/html");
racterEncoding("utf-8");
racterEncoding("utf-8");
//获取用户ID
String sid = ameter("id");
int id =nt(sid);
String sql ="select * from student where id=?";
SqlBean sBean = new SqlBean();
JavaBean jBean = rchById(sql, id);
//用户ID
ribute("id",());
//用户姓名
ribute("name",e());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
private static final long serialVersionUID = 1L;
}
}
//用户密码
ribute("password",sword());
//用户性别
String sex="";
String man="";
String woman="";
if(()!=null)
{
}
ribute("man",man);
ribute("woman",woman);
//用户年龄
ribute("age",());
//转发
uestDispatcher("").forward(request, response);
sex=().trim();
if(("男"))
{
}
else
{
}
woman ="checked";
man ="checked";
2.增加记录的
package t;
import ption;
import tException;
import rvlet;
import rvletRequest;
import rvletResponse;
import n;
import an;
public class Insert extends HttpServlet {
private static final long serialVersionUID = 1L;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
}
throws ServletException, IOException {
throws ServletException, IOException {
tentType("text/html");
doPost(request,response);
tentType("text/html");
racterEncoding("utf-8");
racterEncoding("utf-8");
//获取前台页面数据
String name =ameter("name");
String password =ameter("password");
String sex =ameter("sex");
String sage = ameter("age");
int age =nt(sage);
//封装到JavaBean对象中去
JavaBean jBean = new JavaBean();
e(name);
sword(password);
(sex);
(age);
//调用模型层
String sql = "insert into student(name,password,sex,age) values(?,?,?,?)";
SqlBean sBean = new SqlBean();
int count =ert(sql,jBean);
String url="";
if(count>0)
{
}
else
{
}
//转发
uestDispatcher("").forward(request, response);
url ="";
ribute("error", "");
url="";
3.更新记录的
package t;
import ption;
import riter;
import tException;
import rvlet;
import rvletRequest;
import rvletResponse;
import an;
import n;
public class Update extends HttpServlet {
tentType("text/html");
racterEncoding("utf-8");
racterEncoding("utf-8");
//获得前台表单信息
String sid = ameter("id");
int id =nt(sid);
String name =ameter("name");
String password =ameter("password");
String sex =ameter("sex");
String sage = ameter("age");
int age =nt(sage);
//封装到JavaBean对象中去
JavaBean jBean = new JavaBean();
(id);
e(name);
sword(password);
(sex);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
(age);
String sql ="update student set name=?,password=?,sex=?,age=? where id=?";
SqlBean sBean = new SqlBean();
int count =ate(sql,jBean);
String url="";
if(count>0)
{
}
else
{
}
uestDispatcher("").forward(request, response);
url ="";
ribute("error", "更新");
url="";
4.删除记录的
package t;
import ption;
import tException;
import rvlet;
import rvletRequest;
import rvletResponse;
import n;
public class Delete extends HttpServlet {
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
}
throws ServletException, IOException {
tentType("text/html");
//获取超链接传来的数据
String sId = ameter("id");
int id =nt(sId);
//调用模型层删除方法
String sql = "delete from student where id=?";
SqlBean sBean = new SqlBean();
int count =ete(sql, id);
String url="";
if(count>0)
{
}
else
{
}
//转发
uestDispatcher(url).forward(request, response);
url ="";
ribute("error", "删除");
url="";
tentType("text/html");
doPost(request,response);
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
XML文件
xmlns="/xml/ns/javaee" xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_2_">
版权声明:本文标题:Servlet实现增删改查功能 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1705102610h473305.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论