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
NoteDAO.Java
package cn.mldn.lxh.note.dao ; import java.util.* ; import cn.mldn.lxh.note.vo.* ; public interface NoteDAO { // Add operation public void insert(Note note) throws Exception ; // Modify operation public void update(Note note) throws Exception ; // Delete operation public void delete(int id) throws Exception ; // Query by ID, mainly for updating public Note queryById(int id) throws Exception ; // Query all public List queryAll() throws Exception ; // Fuzzy query 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 { // Add operation 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("Error occurred in the operation!!!") ; } finally { dbc.close() ; } } // 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 = 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("An error occurred during the operation! ! ! ") ; } finally { dbc.close() ; } } // 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 = dbc.getConnection().prepareStatement(sql) ; pstmt.setInt(1,id) ; pstmt.executeUpdate() ; pstmt.close() ; } catch (Exception e) { throw new Exception("An error occurred in the operation!!!") ; } finally { dbc.close() ; } } // Query by ID, mainly for updates 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("An error occurred during the operation! ! ! ") ; } finally { dbc.close() ; } 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 = 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("Error occurred in the operation!!!") ; } finally { dbc.close() ; } 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 = 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("An error occurred during the operation! ! ! ") ; } 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" ; // Receive the parameter value to be operated String status = request.getParameter("status") ; if(status!=null) { // The parameter has content, then select the appropriate method // Query all operations if("selectall".equals(status)) { try { request.setAttribute("all",DAOFactory.getNoteDAOInstance().queryAll()) ; } catch (Exception e) { } path = "list_notes.jsp" ; } // Insert operation if("insert".equals(status)) { // 1. Receive the inserted information String title = request.getParameter("title") ; String author = request.getParameter("author") ; String content = request.getParameter("content") ; // 2. Instantiate the VO object Note note = new Note() ; note.setTitle(title) ; note.setAuthor(author) ; note.setContent(content) ; // 3. Call DAO to complete the database insertion operation boolean flag = false ; try { DAOFactory.getNoteDAOInstance().insert(note) ; flag = true ; } catch (Exception e) {} request.setAttribute("flag",new Boolean(flag)) ; path = "insert_do.jsp" ; } // Query operation by ID. Before modifying, you need to query the data first if("selectid".equals(status)) { // Receive parameter 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" ; } // Update operation 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" ; } // Fuzzy query if("selectbylike".equals(status)) { String keyword = request.getParameter("keyword") ; try { request.setAttribute("all",DAOFactory.getNoteDAOInstance().queryByLike(keyword)) ; } catch (Exception e) { } path = "list_notes.jsp" ; } // Delete operation if("delete".equals(status)) { // Receive parameter 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 { // It means no parameters, illegal customer request} request.getRequestDispatcher(path).forward(request,response) ; } }; /* <servlet> <servlet-name>note</servlet-name> <servlet-class>cn.mldn.lxh.note.servlet.NoteServlet</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="java.util.*"%> <%@ page import="cn.mldn.lxh.note.vo.*"%> <html> <head> <title>MVC+DAO Message Management Program-Login</title> </head> <body> <center> <h1>Message Management Example-MVC + DAO Implementation</h1> <hr> <br> <% // Encoding Conversion Request.setCharacterEncoding("GB2312") ; if(session.getAttribute("uname")!=null) { // User is logged in %> <% // If there is content, modify the variable i. If there is no, there is no content prompt based on i's value int i = 0 ; String keyword = request.getParameter("keyword") ; List all = null ; all = (List)request.getAttribute("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="insert.jsp">Add a new message</a></h3> <table> <tr> <td>Message ID</td> <td>Title</td> <td>Author</td> <td>Content</td> <td>Delete</td> </tr> <% Iterator iter = all.iterator() ; while(iter.hasNext()) { Note note = (Note)iter.next() ; i++ ; // Perform a loop printing to print out all contents in table form // Remove content from the database int id = note.getId() ; String title = note.getTitle() ; String author = note.getAuthor() ; String content = note.getContent() ; // Because the keyword is red, you need to receive query keywords here // String keyword = request.getParameter("keyword") ; if(keyword!=null) { // The data needs to be reddited title = title.replaceAll(keyword,"<font color=/"red/">"+keyword+"</font>") ; author = author.replaceAll(keyword,"<font color=/"red/">"+keyword +"</font>") ; content = content.replaceAll(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 that there is content. Otherwise, there is no content if(i==0) { // Make a prompt %> <tr> <td colspan="5">Nothing! ! ! </td> </tr> <% } %> </table> <% } else { // The user is not logged in, prompt the user to log in, and jump to response.setHeader("refresh","2;URL=login.jsp"); %> You are not logged in yet, please log in first! ! ! <br> It will automatically jump to the login window in two seconds! ! ! <br> If there is no jump, press <a href="login.jsp">here</a>! ! ! <br> <% } %> </center> </body> </html>The above is all about this article, I hope it will be helpful to everyone's learning.