Fuzzy query instance sharing in java message management system
- 2020-05-10 18:04:14
- OfStack
This paper shares a message management system based on MVC+DAO, including adding, deleting, modifying and checking, in which the query has all the query and fuzzy query by keyword function, 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
{
// Increase the operating
public void insert(Note note) throws Exception ;
// Modify the operating
public void update(Note note) throws Exception ;
// Delete operation
public void delete(int id) throws Exception ;
// According to the ID Query, mainly for update use
public Note queryById(int id) throws Exception ;
// All the query
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
{
// Increase the operating
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 in operation!! ") ;
}
finally
{
dbc.close() ;
}
}
// Modify the operating
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(" Error in 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(" Error in operation!! ") ;
}
finally
{
dbc.close() ;
}
}
// According to the ID Query, 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 = 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(" Error in operation!! ") ;
}
finally
{
dbc.close() ;
}
return note ;
}
// All the query
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 in 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(" Error in 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" ;
// Receives the parameter values to operate on
String status = request.getParameter("status") ;
if(status!=null)
{
// Parameters have content, and 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" ;
}
// The insert
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 And instantiation VO object
Note note = new Note() ;
note.setTitle(title) ;
note.setAuthor(author) ;
note.setContent(content) ;
// 3 , call DAO Complete the database insert operation
boolean flag = false ;
try
{
DAOFactory.getNoteDAOInstance().insert(note) ;
flag = true ;
}
catch (Exception e)
{}
request.setAttribute("flag",new Boolean(flag)) ;
path = "insert_do.jsp" ;
}
// According to the ID Query operation, need to query out the data before modification
if("selectid".equals(status))
{
// Receive parameters
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" ;
}
// The 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 parameters
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
{
// Represents an invalid client request with no parameters
}
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 procedures - login </title>
</head>
<body>
<center>
<h1> Example of message management - MVC + DAO implementation </h1>
<hr>
<br>
<%
// Code conversion
request.setCharacterEncoding("GB2312") ;
if(session.getAttribute("uname")!=null)
{
// User logged in
%>
<%
// If there is content, modify the variable i If not, then according to i The value carries no content prompt
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=" The query ">
</form>
</h3><a href="insert.jsp"> Add new message </a></h3>
<table width="80%" border="1">
<tr>
<td> Leave a message ID</td>
<td> The title </td>
<td> The author </td>
<td> content </td>
<td> delete </td>
</tr>
<%
Iterator iter = all.iterator() ;
while(iter.hasNext())
{
Note note = (Note)iter.next() ;
i++ ;
// Print in a circular fashion, printing out all the content in a table
// Pull the content from the database
int id = note.getId() ;
String title = note.getTitle() ;
String author = note.getAuthor() ;
String content = note.getContent() ;
// You need to receive the query keyword here because you want the keyword to return red
// String keyword = request.getParameter("keyword") ;
if(keyword!=null)
{
// You need to return the data to red
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>
<%
}
// judge i Whether the value of, if changed, there is content, otherwise, there is no content
if(i==0)
{
// For prompt
%>
<tr>
<td colspan="5"> No content!! </td>
</tr>
<%
}
%>
</table>
<%
}
else
{
// User not logged in, prompt user to log in, and jump
response.setHeader("refresh","2;URL=login.jsp") ;
%>
You haven't logged in yet, please log in first!! <br>
After two seconds automatically jump to the login window!! <br>
If there is no jump, press <a href="login.jsp"> Here, </a> !!!!!!!!! <br>
<%
}
%>
</center>
</body>
</html>
The above is the entire content of this article, I hope to help you with your study.