服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java留言管理系统中模糊查询实例分享

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

2020-04-22 11:38java教程网 Java教程

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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

  1. package cn.mldn.lxh.note.dao.impl ; 
  2.   
  3. import java.sql.* ; 
  4. import java.util.* ; 
  5. import cn.mldn.lxh.note.vo.* ; 
  6. import cn.mldn.lxh.note.dao.* ; 
  7. import cn.mldn.lxh.note.dbc.* ; 
  8.   
  9. public class NoteDAOImpl implements NoteDAO 
  10.   // 增加操作 
  11.   public void insert(Note note) throws Exception 
  12.   { 
  13.     String sql = "INSERT INTO note(id,title,author,content) VALUES(note_sequ.nextVal,?,?,?)" ; 
  14.     PreparedStatement pstmt = null ; 
  15.     DataBaseConnection dbc = null ; 
  16.     dbc = new DataBaseConnection() ; 
  17.     try 
  18.     { 
  19.       pstmt = dbc.getConnection().prepareStatement(sql) ; 
  20.       pstmt.setString(1,note.getTitle()) ; 
  21.       pstmt.setString(2,note.getAuthor()) ; 
  22.       pstmt.setString(3,note.getContent()) ; 
  23.       pstmt.executeUpdate() ; 
  24.       pstmt.close() ; 
  25.     } 
  26.     catch (Exception e) 
  27.     { 
  28.       // System.out.println(e) ; 
  29.       throw new Exception("操作中出现错误!!!") ; 
  30.     } 
  31.     finally 
  32.     { 
  33.       dbc.close() ; 
  34.     } 
  35.   } 
  36.   // 修改操作 
  37.   public void update(Note note) throws Exception 
  38.   { 
  39.     String sql = "UPDATE note SET title=?,author=?,content=? WHERE id=?" ; 
  40.     PreparedStatement pstmt = null ; 
  41.     DataBaseConnection dbc = null ; 
  42.     dbc = new DataBaseConnection() ; 
  43.     try 
  44.     { 
  45.       pstmt = dbc.getConnection().prepareStatement(sql) ; 
  46.       pstmt.setString(1,note.getTitle()) ; 
  47.       pstmt.setString(2,note.getAuthor()) ; 
  48.       pstmt.setString(3,note.getContent()) ; 
  49.       pstmt.setInt(4,note.getId()) ; 
  50.       pstmt.executeUpdate() ; 
  51.       pstmt.close() ; 
  52.     } 
  53.     catch (Exception e) 
  54.     { 
  55.       throw new Exception("操作中出现错误!!!") ; 
  56.     } 
  57.     finally 
  58.     { 
  59.       dbc.close() ; 
  60.     } 
  61.   } 
  62.   // 删除操作 
  63.   public void delete(int id) throws Exception 
  64.   { 
  65.     String sql = "DELETE FROM note WHERE id=?" ; 
  66.     PreparedStatement pstmt = null ; 
  67.     DataBaseConnection dbc = null ; 
  68.     dbc = new DataBaseConnection() ; 
  69.     try 
  70.     { 
  71.       pstmt = dbc.getConnection().prepareStatement(sql) ; 
  72.       pstmt.setInt(1,id) ; 
  73.       pstmt.executeUpdate() ; 
  74.       pstmt.close() ; 
  75.     } 
  76.     catch (Exception e) 
  77.     { 
  78.       throw new Exception("操作中出现错误!!!") ; 
  79.     } 
  80.     finally 
  81.     { 
  82.       dbc.close() ; 
  83.     } 
  84.   } 
  85.   // 按ID查询,主要为更新使用 
  86.   public Note queryById(int id) throws Exception 
  87.   { 
  88.     Note note = null ; 
  89.     String sql = "SELECT id,title,author,content FROM note WHERE id=?" ; 
  90.     PreparedStatement pstmt = null ; 
  91.     DataBaseConnection dbc = null ; 
  92.     dbc = new DataBaseConnection() ; 
  93.     try 
  94.     { 
  95.       pstmt = dbc.getConnection().prepareStatement(sql) ; 
  96.       pstmt.setInt(1,id) ; 
  97.       ResultSet rs = pstmt.executeQuery() ; 
  98.       if(rs.next()) 
  99.       { 
  100.         note = new Note() ; 
  101.         note.setId(rs.getInt(1)) ; 
  102.         note.setTitle(rs.getString(2)) ; 
  103.         note.setAuthor(rs.getString(3)) ; 
  104.         note.setContent(rs.getString(4)) ; 
  105.       } 
  106.       rs.close() ; 
  107.       pstmt.close() ; 
  108.     } 
  109.     catch (Exception e) 
  110.     { 
  111.       throw new Exception("操作中出现错误!!!") ; 
  112.     } 
  113.     finally 
  114.     { 
  115.       dbc.close() ; 
  116.     } 
  117.     return note ; 
  118.   } 
  119.   // 查询全部 
  120.   public List queryAll() throws Exception 
  121.   { 
  122.     List all = new ArrayList() ; 
  123.     String sql = "SELECT id,title,author,content FROM note" ; 
  124.     PreparedStatement pstmt = null ; 
  125.     DataBaseConnection dbc = null ; 
  126.     dbc = new DataBaseConnection() ; 
  127.     try 
  128.     { 
  129.       pstmt = dbc.getConnection().prepareStatement(sql) ; 
  130.       ResultSet rs = pstmt.executeQuery() ; 
  131.       while(rs.next()) 
  132.       { 
  133.         Note note = new Note() ; 
  134.         note.setId(rs.getInt(1)) ; 
  135.         note.setTitle(rs.getString(2)) ; 
  136.         note.setAuthor(rs.getString(3)) ; 
  137.         note.setContent(rs.getString(4)) ; 
  138.         all.add(note) ; 
  139.       } 
  140.       rs.close() ; 
  141.       pstmt.close() ; 
  142.     } 
  143.     catch (Exception e) 
  144.     { 
  145.       System.out.println(e) ; 
  146.       throw new Exception("操作中出现错误!!!") ; 
  147.     } 
  148.     finally 
  149.     { 
  150.       dbc.close() ; 
  151.     } 
  152.     return all ; 
  153.   } 
  154.   // 模糊查询 
  155.   public List queryByLike(String cond) throws Exception 
  156.   { 
  157.     List all = new ArrayList() ; 
  158.     String sql = "SELECT id,title,author,content FROM note WHERE title LIKE ? or AUTHOR LIKE ? or CONTENT LIKE ?" ;  
  159.     PreparedStatement pstmt = null ; 
  160.     DataBaseConnection dbc = null ; 
  161.     dbc = new DataBaseConnection() ; 
  162.     try 
  163.     { 
  164.       pstmt = dbc.getConnection().prepareStatement(sql) ; 
  165.       pstmt.setString(1,"%"+cond+"%") ; 
  166.       pstmt.setString(2,"%"+cond+"%") ; 
  167.       pstmt.setString(3,"%"+cond+"%") ; 
  168.       ResultSet rs = pstmt.executeQuery() ; 
  169.       while(rs.next()) 
  170.       { 
  171.         Note note = new Note() ; 
  172.         note.setId(rs.getInt(1)) ; 
  173.         note.setTitle(rs.getString(2)) ; 
  174.         note.setAuthor(rs.getString(3)) ; 
  175.         note.setContent(rs.getString(4)) ; 
  176.         all.add(note) ; 
  177.       } 
  178.       rs.close() ; 
  179.       pstmt.close() ; 
  180.     } 
  181.     catch (Exception e) 
  182.     { 
  183.       System.out.println(e) ; 
  184.       throw new Exception("操作中出现错误!!!") ; 
  185.     } 
  186.     finally 
  187.     { 
  188.       dbc.close() ; 
  189.     } 
  190.     return all ; 
  191.   } 
  192. }; 

NoteServlet.java

  1. package cn.mldn.lxh.note.servlet ; 
  2.   
  3. import java.io.* ; 
  4. import javax.servlet.* ; 
  5. import javax.servlet.http.* ; 
  6. import cn.mldn.lxh.note.factory.* ; 
  7. import cn.mldn.lxh.note.vo.* ; 
  8.   
  9. public class NoteServlet extends HttpServlet 
  10.   public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException 
  11.   { 
  12.     this.doPost(request,response) ; 
  13.   } 
  14.   public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException  
  15.   { 
  16.     request.setCharacterEncoding("GB2312") ; 
  17.     String path = "errors.jsp" ; 
  18.     // 接收要操作的参数值 
  19.     String status = request.getParameter("status") ; 
  20.     if(status!=null
  21.     { 
  22.       // 参数有内容,之后选择合适的方法 
  23.       // 查询全部操作 
  24.       if("selectall".equals(status)) 
  25.       { 
  26.         try 
  27.         { 
  28.           request.setAttribute("all",DAOFactory.getNoteDAOInstance().queryAll()) ; 
  29.         } 
  30.         catch (Exception e) 
  31.         { 
  32.         } 
  33.         path = "list_notes.jsp" ; 
  34.       } 
  35.       // 插入操作 
  36.       if("insert".equals(status)) 
  37.       { 
  38.         // 1、接收插入的信息 
  39.         String title = request.getParameter("title") ; 
  40.         String author = request.getParameter("author") ; 
  41.         String content = request.getParameter("content") ; 
  42.         // 2、实例化VO对象 
  43.         Note note = new Note() ; 
  44.         note.setTitle(title) ; 
  45.         note.setAuthor(author) ; 
  46.         note.setContent(content) ; 
  47.         // 3、调用DAO完成数据库的插入操作 
  48.         boolean flag = false ; 
  49.         try 
  50.         { 
  51.           DAOFactory.getNoteDAOInstance().insert(note) ; 
  52.           flag = true ; 
  53.         } 
  54.         catch (Exception e) 
  55.         {} 
  56.         request.setAttribute("flag",new Boolean(flag)) ; 
  57.         path = "insert_do.jsp" ; 
  58.       } 
  59.       // 按ID查询操作,修改之前需要将数据先查询出来 
  60.       if("selectid".equals(status)) 
  61.       { 
  62.         // 接收参数 
  63.         int id = 0 ; 
  64.         try 
  65.         { 
  66.           id = Integer.parseInt(request.getParameter("id")) ; 
  67.         } 
  68.         catch(Exception e) 
  69.         {} 
  70.         try 
  71.         { 
  72.           request.setAttribute("note",DAOFactory.getNoteDAOInstance().queryById(id)) ; 
  73.         } 
  74.         catch (Exception e) 
  75.         { 
  76.         }         
  77.         path = "update.jsp" ; 
  78.       } 
  79.       // 更新操作 
  80.       if("update".equals(status)) 
  81.       { 
  82.         int id = 0 ; 
  83.         try 
  84.         { 
  85.           id = Integer.parseInt(request.getParameter("id")) ; 
  86.         } 
  87.         catch(Exception e) 
  88.         {} 
  89.         String title = request.getParameter("title") ; 
  90.         String author = request.getParameter("author") ; 
  91.         String content = request.getParameter("content") ; 
  92.         Note note = new Note() ; 
  93.         note.setId(id) ; 
  94.         note.setTitle(title) ; 
  95.         note.setAuthor(author) ; 
  96.         note.setContent(content) ; 
  97.         boolean flag = false ; 
  98.         try 
  99.         { 
  100.           DAOFactory.getNoteDAOInstance().update(note) ; 
  101.           flag = true ; 
  102.         } 
  103.         catch (Exception e) 
  104.         {} 
  105.         request.setAttribute("flag",new Boolean(flag)) ; 
  106.         path = "update_do.jsp" ; 
  107.       } 
  108.       // 模糊查询 
  109.       if("selectbylike".equals(status)) 
  110.       { 
  111.         String keyword = request.getParameter("keyword") ; 
  112.         try 
  113.         { 
  114.           request.setAttribute("all",DAOFactory.getNoteDAOInstance().queryByLike(keyword)) ; 
  115.         } 
  116.         catch (Exception e) 
  117.         { 
  118.         } 
  119.         path = "list_notes.jsp" ; 
  120.       } 
  121.       // 删除操作 
  122.       if("delete".equals(status)) 
  123.       { 
  124.         // 接收参数 
  125.         int id = 0 ; 
  126.         try 
  127.         { 
  128.           id = Integer.parseInt(request.getParameter("id")) ; 
  129.         } 
  130.         catch(Exception e) 
  131.         {} 
  132.         boolean flag = false ; 
  133.         try 
  134.         { 
  135.           DAOFactory.getNoteDAOInstance().delete(id) ; 
  136.           flag = true ; 
  137.         } 
  138.         catch (Exception e) 
  139.         {} 
  140.         request.setAttribute("flag",new Boolean(flag)) ; 
  141.         path = "delete_do.jsp" ; 
  142.       } 
  143.     } 
  144.     else 
  145.     { 
  146.       // 则表示无参数,非法的客户请求 
  147.     } 
  148.     request.getRequestDispatcher(path).forward(request,response) ; 
  149.   } 
  150. }; 
  151. /* 
  152.  <servlet> 
  153.   <servlet-name>note</servlet-name> 
  154.   <servlet-class>cn.mldn.lxh.note.servlet.NoteServlet</servlet-class> 
  155.  </servlet> 
  156.  <servlet-mapping> 
  157.   <servlet-name>note</servlet-name> 
  158.   <url-pattern>/note/note_mvc/Note</url-pattern> 
  159.  </servlet-mapping> 
  160. */ 

list_notes.jsp

  1. <%@ page contentType="text/html;charset=gb2312"%> 
  2. <%@ page import="java.util.*"%> 
  3. <%@ page import="cn.mldn.lxh.note.vo.*"%> 
  4. <html> 
  5. <head> 
  6.   <title>MVC+DAO 留言管理程序——登陆</title> 
  7. </head> 
  8. <body> 
  9. <center> 
  10.   <h1>留言管理范例 —— MVC + DAO实现</h1> 
  11.   <hr> 
  12.   <br> 
  13.   <% 
  14.     // 编码转换 
  15.     request.setCharacterEncoding("GB2312") ; 
  16.     if(session.getAttribute("uname")!=null
  17.     { 
  18.       // 用户已登陆 
  19.   %> 
  20.   <% 
  21.     // 如果有内容,则修改变量i,如果没有,则根据i的值进行无内容提示 
  22.     int i = 0 ; 
  23.     String keyword = request.getParameter("keyword") ; 
  24.     List all = null ; 
  25.     all = (List)request.getAttribute("all") ; 
  26.   %> 
  27. <form action="Note" method="POST"
  28.   请输入查询内容:<input type="text" name="keyword"
  29.   <input type="hidden" name="status" value="selectbylike"
  30.   <input type="submit" value="查询"
  31. </form> 
  32. </h3><a href="insert.jsp">添加新留言</a></h3> 
  33. <table width="80%" border="1"
  34.   <tr> 
  35.     <td>留言ID</td> 
  36.     <td>标题</td> 
  37.     <td>作者</td> 
  38.     <td>内容</td> 
  39.     <td>删除</td> 
  40.   </tr> 
  41.   <% 
  42.       Iterator iter = all.iterator() ; 
  43.       while(iter.hasNext()) 
  44.       { 
  45.         Note note = (Note)iter.next() ; 
  46.         i++ ; 
  47.         // 进行循环打印,打印出所有的内容,以表格形式 
  48.         // 从数据库中取出内容 
  49.         int id = note.getId() ; 
  50.         String title = note.getTitle() ; 
  51.         String author = note.getAuthor() ; 
  52.         String content = note.getContent() ; 
  53.           
  54.         // 因为要关键字返红,所以此处需要接收查询关键字 
  55.         // String keyword = request.getParameter("keyword") ; 
  56.         if(keyword!=null
  57.         { 
  58.           // 需要将数据返红 
  59.           title = title.replaceAll(keyword,"<font color="red">"+keyword+"</font>")   
  60.   
  61.           author = author.replaceAll(keyword,"<font color="red">"+keyword 
  62.   
  63. +"</font>") ; 
  64.           content = content.replaceAll(keyword,"<font color="red">"+keyword 
  65.   
  66. +"</font>") ; 
  67.         } 
  68.   %> 
  69.         <tr> 
  70.           <td><%=id%></td> 
  71.           <td><a href="Note?id=<%=id%>&status=selectid"><%=title%></a></td> 
  72.           <td><%=author%></td> 
  73.           <td><%=content%></td> 
  74.           <td><a href="Note?id=<%=id%>&status=delete">删除</a></td> 
  75.         </tr> 
  76.   <% 
  77.       } 
  78.       // 判断i的值是否改变,如果改变,则表示有内容,反之,无内容 
  79.       if(i==0) 
  80.         { 
  81.       // 进行提示 
  82.   %> 
  83.         <tr> 
  84.           <td colspan="5">没有任何内容!!!</td> 
  85.         </tr> 
  86.   <% 
  87.       } 
  88.   %> 
  89. </table> 
  90.   
  91.   <% 
  92.     } 
  93.     else 
  94.     { 
  95.       // 用户未登陆,提示用户登陆,并跳转 
  96.       response.setHeader("refresh","2;URL=login.jsp") ; 
  97.   %> 
  98.       您还未登陆,请先登陆!!!<br> 
  99.       两秒后自动跳转到登陆窗口!!!<br> 
  100.       如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br> 
  101.   <% 
  102.     } 
  103.   %> 
  104. </center> 
  105. </body> 
  106. </html> 

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

延伸 · 阅读

精彩推荐