使用DAO+MVC设计模式的小项目

使用DAO+MVC设计模式的小项目


一:DAO和MVC的简单介绍

  • DAO: (Data Access Object,数据访问对象)主要功能就是操作数据库,也就是数据的的增删查改。
  • MVC:(Model View Controller,模型-视图-控制器)主要功能就是强制性的让应用程序的输入、处理、输出分开。

二:项目简介

1.项目主要模块
  • 用户登录模块
  • 留言操作模块
2.模块的主要功能
  • 用户登录模块:用于验证用户登录
  • 留言操作模块:用于操作留言信息

三:功能模块的详细介绍以及关键代码展示

  • 用户登录模块


    以上两张图片中的java文件和jsp文件用于用户登录

1.jsp文件为View层
login.jsp为登录页面
login_success.jsp为登录成功页面

2.带有DAO的java文件为Model层
PersonDAO.java为功能接口
PersonDAOimpl.java为真实操作类

3.带有Servlet的java文件为Controller层
PersonLoginServlet.java将View层与Model层连接起来
接收来自jsp文件中的相关数据
调用DAO层中的真实操作类进行登录验证
跳转到另一个jsp页面(成功跳转到login_success.jsp,失败跳转到errors.jsp)

PersonLoginServlet.java关键代码截图
PersonLoginServlet.java

  • 留言操作模块


    以上两张图片中的java文件和jsp文件用于留言操作

    NoteOperateServlet.java中包含各种用于操作留言的方法

      1.登录成功进入login_success.jsp中,之后点击 ‘进入留言管理系统’ 进入list_notes.jsp页面  
      2.在list_notes.jsp页面中可以进行点击操作来实现留言增删查改(所有操作均需要经过NoteOperateServlet.java类)
    

    NoteOperateServlet.java关键代码块

// 增加操作
	public void insert(Note note) throws Exception {
		String sql = "INSERT INTO note (id, title, author, content) VALUES (?,?,?,?)";
		PreparedStatement ps = null;
		DatabaseConnection db = null;
		db = new DatabaseConnection();
		try {
			ps = db.getConnection().prepareStatement(sql);
			ps.setInt(1, note.getId());
			ps.setString(2, note.getTitle());
			ps.setString(3, note.getAuthor());
			ps.setString(4, note.getContent());
			ps.executeUpdate();
			ps.close();
		} catch (Exception e) {
			throw new Exception("操作中出现错误!!!");
		} finally {
			db.close();
		}
	}

	// 修改操作
	public void update(Note note) throws Exception {
		String sql = "UPDATE note SET title=?, author=?, content=? WHERE id=?";
		PreparedStatement ps = null;
		DatabaseConnection db = null;
		db = new DatabaseConnection();
		try {
			ps = db.getConnection().prepareStatement(sql);
			ps.setString(1, note.getTitle());
			ps.setString(2, note.getAuthor());
			ps.setString(3, note.getContent());
			ps.setInt(4, note.getId());
//			System.out.println("i的值为" + note.getId());
			if (ps.executeUpdate() == 0) {
				System.out.println("12345");
			}else {
				System.out.println("123");
			}
			ps.close();
		} catch (Exception e) {
			throw new Exception("操作中出现错误!!!");
		} finally {
			db.close();
		}
	}

	// 删除操作
	public void delete(int i) throws Exception {
		String sql = "DELETE FROM note WHERE id = ?";
		PreparedStatement ps = null;
		DatabaseConnection db = null;
		db = new DatabaseConnection();
		try {
			ps = db.getConnection().prepareStatement(sql);
			ps.setInt(1, i);
			ps.executeUpdate();
			ps.close();
		} catch (Exception e) {
			throw new Exception("操作中出现错误!!!");
		} finally {
			db.close();
		}
	}

	// 按ID查询,主要为更新使用
	public Note queryById(int id) throws Exception {
		Note note = null;
		String sql = "SELECT id,title,author,content FROM note WHERE id = ?";
		PreparedStatement ps = null;
		DatabaseConnection db = null;
		db = new DatabaseConnection();
		try {
			ps = db.getConnection().prepareStatement(sql);
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				note = new Note();
				note.setId(rs.getInt(1));
				note.setTitle(rs.getString(2));
				note.setAuthor(rs.getString(3));
				note.setContent(rs.getString(4));
			}
			ps.close();
			rs.close();
		} catch (Exception e) {
			throw new Exception("操作中出现错误!!!");
		} finally {
			db.close();
		}
		return note;
	}

	public List<?> queryAll() throws Exception {
		List<Note> all = new ArrayList<Note>();
		String sql = "SELECT * FROM note";
		PreparedStatement ps = null;
		DatabaseConnection db = null;
		db = new DatabaseConnection();
		try {
			ps = db.getConnection().prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				Note note = new Note();
				note.setId(rs.getInt(1));
				note.setTitle(rs.getString(2));
				note.setAuthor(rs.getString(3));
				note.setContent(rs.getString(4));
				all.add(note);
			}
			rs.close();
			ps.close();
		} catch (Exception e) {
			throw new Exception("操作中出现错误!!!");
		} finally {
			db.close();
		}
		return all;
	}

	public List<?> queryByLike(String cond) throws Exception {
		List<Note> all = new ArrayList<Note>();
		String sql = "SELECT * FROM note WHERE title LIKE ? or author LIKE ? or content LIKE ?";
		PreparedStatement ps = null;
		DatabaseConnection db = null;
		db = new DatabaseConnection();
		try {
			ps = db.getConnection().prepareStatement(sql);
			ps.setString(1, "%" + cond + "%");
			ps.setString(2, "%" + cond + "%");
			ps.setString(3, "%" + cond + "%");
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				Note note = new Note();
				note.setId(rs.getInt(1));
				note.setTitle(rs.getString(2));
				note.setAuthor(rs.getString(3));
				note.setContent(rs.getString(4));
				all.add(note);
			}
			rs.close();
			ps.close();
		} catch (Exception e) {
			System.out.println(e);
			throw new Exception("操作中出现错误!!!");
		} finally {
			db.close();
		}
		return all;
	}
小知识

login_success.jsp类
1.response.setHeader(“refresh”, “2;URL=login.jsp”);
正确理解:两秒之后跳转到其他页面(login.jsp)
response.setHeader(“refresh”,“1”);
正确理解:一秒钟刷新一次页面 (在本页面)
2.< a href=“NoteOperateServlet?status=selectall”>
正确理解:是一个转向NoteOperateServlet的链接,?后面跟的是一个参数

list_notes.jsp类
1.< input type=“hidden” name=“statu” value=“selectbylike”>
正确理解:这是隐藏文本域,目的就是为了传值
name就是变量名
value就是变量的字面量
2.< a href=“NoteOperateServlet?id=<%=id %>&status=delete”>
正确理解:是一个转向Note页面的链接,?后面跟的是两个参数,参数用&连接起来

NoteOperateServlet.java类
1.String id = req.getParameter(“id”);
int i = Integer.parseInt(id);
正确理解:将String转化为int

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lw中

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值