一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - 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("操作中出現錯誤?。。?quot;) ; 
  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("操作中出現錯誤?。。?quot;) ; 
  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("操作中出現錯誤?。。?quot;) ; 
  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> 

以上就是本文的全部內容,希望對大家的學習有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 深夜福利软件 | 国产香蕉一区二区在线观看 | 日韩一级精品视频在线观看 | 亚洲人成影院午夜网站 | 久久受www免费人成_看片中文 | 成人综合婷婷国产精品久久免费 | bl动漫在线观看 | 娇喘嗯嗯 轻点啊视频福利 九九九九在线精品免费视频 | 青柠影院在线观看免费完整版1 | 四虎影视在线观看2413 | 国产青草视频在线观看免费影院 | 男生操女生动态图 | 性欧美4khdxxxx | 亚洲精品久久久久福利网站 | 美女张开腿让男人桶的 视频 | 青青91 | 456亚洲人成高清在线 | 女子张腿让男人桶免费 | 午夜影院在线免费观看 | 天堂8在线天堂资源在线 | 四虎国产精品免费久久麻豆 | 午夜精品国产自在现线拍 | 精品国产一区二区三区久 | 日韩免费一级片 | 久久成人永久免费播放 | 欧亚精品一区二区三区 | 艾秋麻豆果冻传媒老狼仙踪林 | 国精视频一区二区视频 | 99re7在线精品免费视频 | 亚洲乱亚洲乱妇41p 亚洲乱码一区二区三区国产精品 | aa一级护士医生毛片 | 日本肥熟| 国产一区二区三区高清 | 日本人成大片在线 | 五月天黄网 | 国产精品免费网站 | 免费视频片在线观看大片 | 日本人添下面的全过程 | 99欧美视频 | 日韩精品欧美激情国产一区 | 久久成人a毛片免费观看网站 |