SoFunction
Updated on 2025-03-06

Sharing of fuzzy query instances in Java message management system

This article shares a message management system based on MVC+DAO, which includes addition, deletion, modification and query, and the query has all queries and fuzzy query by keywords. The specific content is as follows

package  ; 
 
import .* ; 
import .* ; 
 
public interface NoteDAO 
{ 
  // Add operations  public void insert(Note note) throws Exception ; 
  // Modify the operation  public void update(Note note) throws Exception ; 
  // Delete operation  public void delete(int id) throws Exception ; 
  // Query by ID, mainly for update use  public Note queryById(int id) throws Exception ; 
  // Query all  public List queryAll() throws Exception ; 
  // Fuzzy query  public List queryByLike(String cond) throws Exception ; 
}; 


package  ; 
 
import .* ; 
import .* ; 
import .* ; 
import .* ; 
import .* ; 
 
public class NoteDAOImpl implements NoteDAO 
{ 
  // Add operations  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 = ().prepareStatement(sql) ; 
      (1,()) ; 
      (2,()) ; 
      (3,()) ; 
      () ; 
      () ; 
    } 
    catch (Exception e) 
    { 
      // (e) ; 
      throw new Exception("An error occurred during operation!!!") ; 
    } 
    finally 
    { 
      () ; 
    } 
  } 
  // Modify the operation  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 = ().prepareStatement(sql) ; 
      (1,()) ; 
      (2,()) ; 
      (3,()) ; 
      (4,()) ; 
      () ; 
      () ; 
    } 
    catch (Exception e) 
    { 
      throw new Exception("An error occurred during operation!!!") ; 
    } 
    finally 
    { 
      () ; 
    } 
  } 
  // Delete operation  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 = ().prepareStatement(sql) ; 
      (1,id) ; 
      () ; 
      () ; 
    } 
    catch (Exception e) 
    { 
      throw new Exception("An error occurred during operation!!!") ; 
    } 
    finally 
    { 
      () ; 
    } 
  } 
  // Query by ID, mainly for update use  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 = ().prepareStatement(sql) ; 
      (1,id) ; 
      ResultSet rs = () ; 
      if(()) 
      { 
        note = new Note() ; 
        ((1)) ; 
        ((2)) ; 
        ((3)) ; 
        ((4)) ; 
      } 
      () ; 
      () ; 
    } 
    catch (Exception e) 
    { 
      throw new Exception("An error occurred during operation!!!") ; 
    } 
    finally 
    { 
      () ; 
    } 
    return note ; 
  } 
  // Query all  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 = ().prepareStatement(sql) ; 
      ResultSet rs = () ; 
      while(()) 
      { 
        Note note = new Note() ; 
        ((1)) ; 
        ((2)) ; 
        ((3)) ; 
        ((4)) ; 
        (note) ; 
      } 
      () ; 
      () ; 
    } 
    catch (Exception e) 
    { 
      (e) ; 
      throw new Exception("An error occurred during operation!!!") ; 
    } 
    finally 
    { 
      () ; 
    } 
    return all ; 
  } 
  // Fuzzy query  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 = ().prepareStatement(sql) ; 
      (1,"%"+cond+"%") ; 
      (2,"%"+cond+"%") ; 
      (3,"%"+cond+"%") ; 
      ResultSet rs = () ; 
      while(()) 
      { 
        Note note = new Note() ; 
        ((1)) ; 
        ((2)) ; 
        ((3)) ; 
        ((4)) ; 
        (note) ; 
      } 
      () ; 
      () ; 
    } 
    catch (Exception e) 
    { 
      (e) ; 
      throw new Exception("An error occurred during operation!!!") ; 
    } 
    finally 
    { 
      () ; 
    } 
    return all ; 
  } 
}; 


package  ; 
 
import .* ; 
import .* ; 
import .* ; 
import .* ; 
import .* ; 
 
public class NoteServlet extends HttpServlet 
{ 
  public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException 
  { 
    (request,response) ; 
  } 
  public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException 
  { 
    ("GB2312") ; 
    String path = "" ; 
    // Receive the parameter value to be operated    String status = ("status") ; 
    if(status!=null) 
    { 
      // The parameters have content, then select the appropriate method      // Query all operations      if("selectall".equals(status)) 
      { 
        try 
        { 
          ("all",().queryAll()) ; 
        } 
        catch (Exception e) 
        { 
        } 
        path = "list_notes.jsp" ; 
      } 
      // Insert operation      if("insert".equals(status)) 
      { 
        // 1. Receive inserted information        String title = ("title") ; 
        String author = ("author") ; 
        String content = ("content") ; 
        // 2. Instantiate VO objects        Note note = new Note() ; 
        (title) ; 
        (author) ; 
        (content) ; 
        // 3. Call DAO to complete the database insertion operation        boolean flag = false ; 
        try 
        { 
          ().insert(note) ; 
          flag = true ; 
        } 
        catch (Exception e) 
        {} 
        ("flag",new Boolean(flag)) ; 
        path = "insert_do.jsp" ; 
      } 
      // Query the operation by ID. Before modifying, you need to query the data first.      if("selectid".equals(status)) 
      { 
        // Receive parameters        int id = 0 ; 
        try 
        { 
          id = (("id")) ; 
        } 
        catch(Exception e) 
        {} 
        try 
        { 
          ("note",().queryById(id)) ; 
        } 
        catch (Exception e) 
        { 
        }         
        path = "" ; 
      } 
      // Update operation      if("update".equals(status)) 
      { 
        int id = 0 ; 
        try 
        { 
          id = (("id")) ; 
        } 
        catch(Exception e) 
        {} 
        String title = ("title") ; 
        String author = ("author") ; 
        String content = ("content") ; 
        Note note = new Note() ; 
        (id) ; 
        (title) ; 
        (author) ; 
        (content) ; 
        boolean flag = false ; 
        try 
        { 
          ().update(note) ; 
          flag = true ; 
        } 
        catch (Exception e) 
        {} 
        ("flag",new Boolean(flag)) ; 
        path = "update_do.jsp" ; 
      } 
      // Fuzzy query      if("selectbylike".equals(status)) 
      { 
        String keyword = ("keyword") ; 
        try 
        { 
          ("all",().queryByLike(keyword)) ; 
        } 
        catch (Exception e) 
        { 
        } 
        path = "list_notes.jsp" ; 
      } 
      // Delete operation      if("delete".equals(status)) 
      { 
        // Receive parameters        int id = 0 ; 
        try 
        { 
          id = (("id")) ; 
        } 
        catch(Exception e) 
        {} 
        boolean flag = false ; 
        try 
        { 
          ().delete(id) ; 
          flag = true ; 
        } 
        catch (Exception e) 
        {} 
        ("flag",new Boolean(flag)) ; 
        path = "delete_do.jsp" ; 
      } 
    } 
    else 
    { 
      // It means no parameters, illegal customer request    } 
    (path).forward(request,response) ; 
  } 
}; 
/* 
 <servlet> 
  <servlet-name>note</servlet-name> 
  <servlet-class></servlet-class> 
 </servlet> 
 <servlet-mapping> 
  <servlet-name>note</servlet-name> 
  <url-pattern>/note/note_mvc/Note</url-pattern> 
 </servlet-mapping> 
*/ 

list_notes.jsp

<%@ page contentType="text/html;charset=gb2312"%> 
<%@ page import=".*"%> 
<%@ page import=".*"%> 
<html> 
<head> 
  <title>MVC+DAO Message management program——Log in</title> 
</head> 
<body> 
<center> 
  <h1>Message management example —— MVC + DAOaccomplish</h1> 
  <hr> 
  <br> 
  <% 
    // Encoding conversion    ("GB2312") ; 
    if(("uname")!=null) 
    { 
      // The user is logged in  %> 
  <% 
    // If there is content, modify the variable i. If there is no, no content prompt will be made according to the value of i.    int i = 0 ; 
    String keyword = ("keyword") ; 
    List all = null ; 
    all = (List)("all") ; 
  %> 
<form action="Note" method="POST"> 
  Please enter the query content:<input type="text" name="keyword"> 
  <input type="hidden" name="status" value="selectbylike"> 
  <input type="submit" value="Query"> 
</form> 
</h3><a href="">Add a new message</a></h3> 
<table width="80%" border="1"> 
  <tr> 
    <td>messageID</td> 
    <td>title</td> 
    <td>author</td> 
    <td>content</td> 
    <td>delete</td> 
  </tr> 
  <% 
      Iterator iter = () ; 
      while(()) 
      { 
        Note note = (Note)() ; 
        i++ ; 
        // Perform a loop printing to print out all the contents in the form of a table        // Get content from the database        int id = () ; 
        String title = () ; 
        String author = () ; 
        String content = () ; 
         
        // Because the keywords need to be red, you need to receive query keywords here        // String keyword = ("keyword") ; 
        if(keyword!=null) 
        { 
          // The data needs to be reddit          title = (keyword,"<font color=\"red\">"+keyword+"</font>")  
 
; 
          author = (keyword,"<font color=\"red\">"+keyword 
 
+"</font>") ; 
          content = (keyword,"<font color=\"red\">"+keyword 
 
+"</font>") ; 
        } 
  %> 
        <tr> 
          <td><%=id%></td> 
          <td><a href="Note?id=<%=id%>&status=selectid"><%=title%></a></td> 
          <td><%=author%></td> 
          <td><%=content%></td> 
          <td><a href="Note?id=<%=id%>&status=delete">delete</a></td> 
        </tr> 
  <% 
      } 
      // Determine whether the value of i changes. If it changes, it means there is content. Otherwise, there is no content.      if(i==0) 
        { 
      // Make a prompt  %> 
        <tr> 
          <td colspan="5">没有任何content!!!</td> 
        </tr> 
  <% 
      } 
  %> 
</table> 
 
  <% 
    } 
    else 
    { 
      // The user is not logged in, prompt the user to log in and jump      ("refresh","2;URL=") ; 
  %> 
      您还未Log in,请先Log in!!!<br> 
      两秒后自动跳转到Log in窗口!!!<br> 
      If there is no jump,Please press<a href="">here</a>!!!<br> 
  <% 
    } 
  %> 
</center> 
</body> 
</html>

The above is all about this article, I hope it will be helpful to everyone's learning.