java留言管理系统中模糊查询实例分享

这篇文章主要为大家详细介绍了基于MVC+DAO的留言管理系统中java模糊查询的简单使用方法,感兴趣的小伙伴们可以参考一下

本文分享了一个基于MVC+DAO的留言管理系统,包含增删改查,其中查询,有全部查询和按关键字进行模糊查询的功能,具体内容如下
NoteDAO.Java

 package cn.mldn.lxh.note.dao ; import java.util.* ; import cn.mldn.lxh.note.vo.* ; public interface NoteDAO { // 增加操作 public void insert(Note note) throws Exception ; // 修改操作 public void update(Note note) throws Exception ; // 删除操作 public void delete(int id) throws Exception ; // 按ID查询,主要为更新使用 public Note queryById(int id) throws Exception ; // 查询全部 public List queryAll() throws Exception ; // 模糊查询 public List queryByLike(String cond) throws Exception ; }; 

NoteDAOImpl.java

 package cn.mldn.lxh.note.dao.impl ; import java.sql.* ; import java.util.* ; import cn.mldn.lxh.note.vo.* ; import cn.mldn.lxh.note.dao.* ; import cn.mldn.lxh.note.dbc.* ; public class NoteDAOImpl implements NoteDAO { // 增加操作 public void insert(Note note) throws Exception { String sql = "INSERT INTO note(id,title,author,content) VALUES(note_sequ.nextVal,?,?,?)" ; PreparedStatement pstmt = null ; DataBaseConnection dbc = null ; dbc = new DataBaseConnection() ; try { pstmt = dbc.getConnection().prepareStatement(sql) ; pstmt.setString(1,note.getTitle()) ; pstmt.setString(2,note.getAuthor()) ; pstmt.setString(3,note.getContent()) ; pstmt.executeUpdate() ; pstmt.close() ; } catch (Exception e) { // System.out.println(e) ; throw new Exception("操作中出现错误!!!") ; } finally { dbc.close() ; } } // 修改操作 public void update(Note note) throws Exception { String sql = "UPDATE note SET title=?,author=?,content=? WHERE id=?" ; PreparedStatement pstmt = null ; DataBaseConnection dbc = null ; dbc = new DataBaseConnection() ; try { pstmt = dbc.getConnection().prepareStatement(sql) ; pstmt.setString(1,note.getTitle()) ; pstmt.setString(2,note.getAuthor()) ; pstmt.setString(3,note.getContent()) ; pstmt.setInt(4,note.getId()) ; pstmt.executeUpdate() ; pstmt.close() ; } catch (Exception e) { throw new Exception("操作中出现错误!!!") ; } finally { dbc.close() ; } } // 删除操作 public void delete(int id) throws Exception { String sql = "DELETE FROM note WHERE id=?" ; PreparedStatement pstmt = null ; DataBaseConnection dbc = null ; dbc = new DataBaseConnection() ; try { pstmt = dbc.getConnection().prepareStatement(sql) ; pstmt.setInt(1,id) ; pstmt.executeUpdate() ; pstmt.close() ; } catch (Exception e) { throw new Exception("操作中出现错误!!!") ; } finally { dbc.close() ; } } // 按ID查询,主要为更新使用 public Note queryById(int id) throws Exception { Note note = null ; String sql = "SELECT id,title,author,content FROM note WHERE id=?" ; PreparedStatement pstmt = null ; DataBaseConnection dbc = null ; dbc = new DataBaseConnection() ; try { pstmt = dbc.getConnection().prepareStatement(sql) ; pstmt.setInt(1,id) ; ResultSet rs = pstmt.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)) ; } rs.close() ; pstmt.close() ; } catch (Exception e) { throw new Exception("操作中出现错误!!!") ; } finally { dbc.close() ; } return note ; } // 查询全部 public List queryAll() throws Exception { List all = new ArrayList() ; String sql = "SELECT id,title,author,content FROM note" ; PreparedStatement pstmt = null ; DataBaseConnection dbc = null ; dbc = new DataBaseConnection() ; try { pstmt = dbc.getConnection().prepareStatement(sql) ; ResultSet rs = pstmt.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() ; pstmt.close() ; } catch (Exception e) { System.out.println(e) ; throw new Exception("操作中出现错误!!!") ; } finally { dbc.close() ; } return all ; } // 模糊查询 public List queryByLike(String cond) throws Exception { List all = new ArrayList() ; String sql = "SELECT id,title,author,content FROM note WHERE title LIKE ? or AUTHOR LIKE ? or CONTENT LIKE ?" ; PreparedStatement pstmt = null ; DataBaseConnection dbc = null ; dbc = new DataBaseConnection() ; try { pstmt = dbc.getConnection().prepareStatement(sql) ; pstmt.setString(1,"%"+cond+"%") ; pstmt.setString(2,"%"+cond+"%") ; pstmt.setString(3,"%"+cond+"%") ; ResultSet rs = pstmt.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() ; pstmt.close() ; } catch (Exception e) { System.out.println(e) ; throw new Exception("操作中出现错误!!!") ; } finally { dbc.close() ; } return all ; } }; 

NoteServlet.java

 package cn.mldn.lxh.note.servlet ; import java.io.* ; import javax.servlet.* ; import javax.servlet.http.* ; import cn.mldn.lxh.note.factory.* ; import cn.mldn.lxh.note.vo.* ; public class NoteServlet extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException { this.doPost(request,response) ; } public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException { request.setCharacterEncoding("GB2312") ; String path = "errors.jsp" ; // 接收要操作的参数值 String status = request.getParameter("status") ; if(status!=null) { // 参数有内容,之后选择合适的方法 // 查询全部操作 if("selectall".equals(status)) { try { request.setAttribute("all",DAOFactory.getNoteDAOInstance().queryAll()) ; } catch (Exception e) { } path = "list_notes.jsp" ; } // 插入操作 if("insert".equals(status)) { // 1、接收插入的信息 String title = request.getParameter("title") ; String author = request.getParameter("author") ; String content = request.getParameter("content") ; // 2、实例化VO对象 Note note = new Note() ; note.setTitle(title) ; note.setAuthor(author) ; note.setContent(content) ; // 3、调用DAO完成数据库的插入操作 boolean flag = false ; try { DAOFactory.getNoteDAOInstance().insert(note) ; flag = true ; } catch (Exception e) {} request.setAttribute("flag",new Boolean(flag)) ; path = "insert_do.jsp" ; } // 按ID查询操作,修改之前需要将数据先查询出来 if("selectid".equals(status)) { // 接收参数 int id = 0 ; try { id = Integer.parseInt(request.getParameter("id")) ; } catch(Exception e) {} try { request.setAttribute("note",DAOFactory.getNoteDAOInstance().queryById(id)) ; } catch (Exception e) { } path = "update.jsp" ; } // 更新操作 if("update".equals(status)) { int id = 0 ; try { id = Integer.parseInt(request.getParameter("id")) ; } catch(Exception e) {} String title = request.getParameter("title") ; String author = request.getParameter("author") ; String content = request.getParameter("content") ; Note note = new Note() ; note.setId(id) ; note.setTitle(title) ; note.setAuthor(author) ; note.setContent(content) ; boolean flag = false ; try { DAOFactory.getNoteDAOInstance().update(note) ; flag = true ; } catch (Exception e) {} request.setAttribute("flag",new Boolean(flag)) ; path = "update_do.jsp" ; } // 模糊查询 if("selectbylike".equals(status)) { String keyword = request.getParameter("keyword") ; try { request.setAttribute("all",DAOFactory.getNoteDAOInstance().queryByLike(keyword)) ; } catch (Exception e) { } path = "list_notes.jsp" ; } // 删除操作 if("delete".equals(status)) { // 接收参数 int id = 0 ; try { id = Integer.parseInt(request.getParameter("id")) ; } catch(Exception e) {} boolean flag = false ; try { DAOFactory.getNoteDAOInstance().delete(id) ; flag = true ; } catch (Exception e) {} request.setAttribute("flag",new Boolean(flag)) ; path = "delete_do.jsp" ; } } else { // 则表示无参数,非法的客户请求 } request.getRequestDispatcher(path).forward(request,response) ; } }; /*  notecn.mldn.lxh.note.servlet.NoteServlet note/note/note_mvc/Note */ 

list_notes.jsp

 <%@ page contentType="text/html;charset=gb2312"%><%@ page import="java.util.*"%><%@ page import="cn.mldn.lxh.note.vo.*"%>  MVC+DAO 留言管理程序――登陆 

留言管理范例 ―― MVC + DAO实现



<% // 编码转换 request.setCharacterEncoding("GB2312") ; if(session.getAttribute("uname")!=null) { // 用户已登陆 %><% // 如果有内容,则修改变量i,如果没有,则根据i的值进行无内容提示 int i = 0 ; String keyword = request.getParameter("keyword") ; List all = null ; all = (List)request.getAttribute("all") ; %>
请输入查询内容:
添加新留言<% Iterator iter = all.iterator() ; while(iter.hasNext()) { Note note = (Note)iter.next() ; i++ ; // 进行循环打印,打印出所有的内容,以表格形式 // 从数据库中取出内容 int id = note.getId() ; String title = note.getTitle() ; String author = note.getAuthor() ; String content = note.getContent() ; // 因为要关键字返红,所以此处需要接收查询关键字 // String keyword = request.getParameter("keyword") ; if(keyword!=null) { // 需要将数据返红 title = title.replaceAll(keyword,""+keyword+"") ; author = author.replaceAll(keyword,""+keyword +"") ; content = content.replaceAll(keyword,""+keyword +"") ; } %> <% } // 判断i的值是否改变,如果改变,则表示有内容,反之,无内容 if(i==0) { // 进行提示 %><% } %>
留言ID标题作者内容删除
<%=id%><%=title%><%=author%><%=content%>删除
没有任何内容!!!
<% } else { // 用户未登陆,提示用户登陆,并跳转 response.setHeader("refresh","2;URL=login.jsp") ; %> 您还未登陆,请先登陆!!!
两秒后自动跳转到登陆窗口!!!
如果没有跳转,请按这里!!!
<% } %>

以上就是本文的全部内容,希望对大家的学习有所帮助。

以上就是java留言管理系统中模糊查询实例分享的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java