java留言管理系统中模糊查询实例分享
本文分享了一个基于MVC+DAO的留言管理系统,包含增删改查,其中查询,有全部查询和按关键字进行模糊查询的功能,具体内容如下
NoteDAO.Java
packagecn.mldn.lxh.note.dao;
importjava.util.*;
importcn.mldn.lxh.note.vo.*;
publicinterfaceNoteDAO
{
//增加操作
publicvoidinsert(Notenote)throwsException;
//修改操作
publicvoidupdate(Notenote)throwsException;
//删除操作
publicvoiddelete(intid)throwsException;
//按ID查询,主要为更新使用
publicNotequeryById(intid)throwsException;
//查询全部
publicListqueryAll()throwsException;
//模糊查询
publicListqueryByLike(Stringcond)throwsException;
};
NoteDAOImpl.java
packagecn.mldn.lxh.note.dao.impl;
importjava.sql.*;
importjava.util.*;
importcn.mldn.lxh.note.vo.*;
importcn.mldn.lxh.note.dao.*;
importcn.mldn.lxh.note.dbc.*;
publicclassNoteDAOImplimplementsNoteDAO
{
//增加操作
publicvoidinsert(Notenote)throwsException
{
Stringsql="INSERTINTOnote(id,title,author,content)VALUES(note_sequ.nextVal,?,?,?)";
PreparedStatementpstmt=null;
DataBaseConnectiondbc=null;
dbc=newDataBaseConnection();
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(Exceptione)
{
//System.out.println(e);
thrownewException("操作中出现错误!!!");
}
finally
{
dbc.close();
}
}
//修改操作
publicvoidupdate(Notenote)throwsException
{
Stringsql="UPDATEnoteSETtitle=?,author=?,content=?WHEREid=?";
PreparedStatementpstmt=null;
DataBaseConnectiondbc=null;
dbc=newDataBaseConnection();
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(Exceptione)
{
thrownewException("操作中出现错误!!!");
}
finally
{
dbc.close();
}
}
//删除操作
publicvoiddelete(intid)throwsException
{
Stringsql="DELETEFROMnoteWHEREid=?";
PreparedStatementpstmt=null;
DataBaseConnectiondbc=null;
dbc=newDataBaseConnection();
try
{
pstmt=dbc.getConnection().prepareStatement(sql);
pstmt.setInt(1,id);
pstmt.executeUpdate();
pstmt.close();
}
catch(Exceptione)
{
thrownewException("操作中出现错误!!!");
}
finally
{
dbc.close();
}
}
//按ID查询,主要为更新使用
publicNotequeryById(intid)throwsException
{
Notenote=null;
Stringsql="SELECTid,title,author,contentFROMnoteWHEREid=?";
PreparedStatementpstmt=null;
DataBaseConnectiondbc=null;
dbc=newDataBaseConnection();
try
{
pstmt=dbc.getConnection().prepareStatement(sql);
pstmt.setInt(1,id);
ResultSetrs=pstmt.executeQuery();
if(rs.next())
{
note=newNote();
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(Exceptione)
{
thrownewException("操作中出现错误!!!");
}
finally
{
dbc.close();
}
returnnote;
}
//查询全部
publicListqueryAll()throwsException
{
Listall=newArrayList();
Stringsql="SELECTid,title,author,contentFROMnote";
PreparedStatementpstmt=null;
DataBaseConnectiondbc=null;
dbc=newDataBaseConnection();
try
{
pstmt=dbc.getConnection().prepareStatement(sql);
ResultSetrs=pstmt.executeQuery();
while(rs.next())
{
Notenote=newNote();
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(Exceptione)
{
System.out.println(e);
thrownewException("操作中出现错误!!!");
}
finally
{
dbc.close();
}
returnall;
}
//模糊查询
publicListqueryByLike(Stringcond)throwsException
{
Listall=newArrayList();
Stringsql="SELECTid,title,author,contentFROMnoteWHEREtitleLIKE?orAUTHORLIKE?orCONTENTLIKE?";
PreparedStatementpstmt=null;
DataBaseConnectiondbc=null;
dbc=newDataBaseConnection();
try
{
pstmt=dbc.getConnection().prepareStatement(sql);
pstmt.setString(1,"%"+cond+"%");
pstmt.setString(2,"%"+cond+"%");
pstmt.setString(3,"%"+cond+"%");
ResultSetrs=pstmt.executeQuery();
while(rs.next())
{
Notenote=newNote();
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(Exceptione)
{
System.out.println(e);
thrownewException("操作中出现错误!!!");
}
finally
{
dbc.close();
}
returnall;
}
};
NoteServlet.java
packagecn.mldn.lxh.note.servlet;
importjava.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
importcn.mldn.lxh.note.factory.*;
importcn.mldn.lxh.note.vo.*;
publicclassNoteServletextendsHttpServlet
{
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException,ServletException
{
this.doPost(request,response);
}
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException,ServletException
{
request.setCharacterEncoding("GB2312");
Stringpath="errors.jsp";
//接收要操作的参数值
Stringstatus=request.getParameter("status");
if(status!=null)
{
//参数有内容,之后选择合适的方法
//查询全部操作
if("selectall".equals(status))
{
try
{
request.setAttribute("all",DAOFactory.getNoteDAOInstance().queryAll());
}
catch(Exceptione)
{
}
path="list_notes.jsp";
}
//插入操作
if("insert".equals(status))
{
//1、接收插入的信息
Stringtitle=request.getParameter("title");
Stringauthor=request.getParameter("author");
Stringcontent=request.getParameter("content");
//2、实例化VO对象
Notenote=newNote();
note.setTitle(title);
note.setAuthor(author);
note.setContent(content);
//3、调用DAO完成数据库的插入操作
booleanflag=false;
try
{
DAOFactory.getNoteDAOInstance().insert(note);
flag=true;
}
catch(Exceptione)
{}
request.setAttribute("flag",newBoolean(flag));
path="insert_do.jsp";
}
//按ID查询操作,修改之前需要将数据先查询出来
if("selectid".equals(status))
{
//接收参数
intid=0;
try
{
id=Integer.parseInt(request.getParameter("id"));
}
catch(Exceptione)
{}
try
{
request.setAttribute("note",DAOFactory.getNoteDAOInstance().queryById(id));
}
catch(Exceptione)
{
}
path="update.jsp";
}
//更新操作
if("update".equals(status))
{
intid=0;
try
{
id=Integer.parseInt(request.getParameter("id"));
}
catch(Exceptione)
{}
Stringtitle=request.getParameter("title");
Stringauthor=request.getParameter("author");
Stringcontent=request.getParameter("content");
Notenote=newNote();
note.setId(id);
note.setTitle(title);
note.setAuthor(author);
note.setContent(content);
booleanflag=false;
try
{
DAOFactory.getNoteDAOInstance().update(note);
flag=true;
}
catch(Exceptione)
{}
request.setAttribute("flag",newBoolean(flag));
path="update_do.jsp";
}
//模糊查询
if("selectbylike".equals(status))
{
Stringkeyword=request.getParameter("keyword");
try
{
request.setAttribute("all",DAOFactory.getNoteDAOInstance().queryByLike(keyword));
}
catch(Exceptione)
{
}
path="list_notes.jsp";
}
//删除操作
if("delete".equals(status))
{
//接收参数
intid=0;
try
{
id=Integer.parseInt(request.getParameter("id"));
}
catch(Exceptione)
{}
booleanflag=false;
try
{
DAOFactory.getNoteDAOInstance().delete(id);
flag=true;
}
catch(Exceptione)
{}
request.setAttribute("flag",newBoolean(flag));
path="delete_do.jsp";
}
}
else
{
//则表示无参数,非法的客户请求
}
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
<%@pagecontentType="text/html;charset=gb2312"%>
<%@pageimport="java.util.*"%>
<%@pageimport="cn.mldn.lxh.note.vo.*"%>
<html>
<head>
<title>MVC+DAO留言管理程序——登陆</title>
</head>
<body>
<center>
<h1>留言管理范例——MVC+DAO实现</h1>
<hr>
<br>
<%
//编码转换
request.setCharacterEncoding("GB2312");
if(session.getAttribute("uname")!=null)
{
//用户已登陆
%>
<%
//如果有内容,则修改变量i,如果没有,则根据i的值进行无内容提示
inti=0;
Stringkeyword=request.getParameter("keyword");
Listall=null;
all=(List)request.getAttribute("all");
%>
<formaction="Note"method="POST">
请输入查询内容:<inputtype="text"name="keyword">
<inputtype="hidden"name="status"value="selectbylike">
<inputtype="submit"value="查询">
</form>
</h3><ahref="insert.jsp">添加新留言</a></h3>
<tablewidth="80%"border="1">
<tr>
<td>留言ID</td>
<td>标题</td>
<td>作者</td>
<td>内容</td>
<td>删除</td>
</tr>
<%
Iteratoriter=all.iterator();
while(iter.hasNext())
{
Notenote=(Note)iter.next();
i++;
//进行循环打印,打印出所有的内容,以表格形式
//从数据库中取出内容
intid=note.getId();
Stringtitle=note.getTitle();
Stringauthor=note.getAuthor();
Stringcontent=note.getContent();
//因为要关键字返红,所以此处需要接收查询关键字
//Stringkeyword=request.getParameter("keyword");
if(keyword!=null)
{
//需要将数据返红
title=title.replaceAll(keyword,"<fontcolor=\"red\">"+keyword+"</font>")
;
author=author.replaceAll(keyword,"<fontcolor=\"red\">"+keyword
+"</font>");
content=content.replaceAll(keyword,"<fontcolor=\"red\">"+keyword
+"</font>");
}
%>
<tr>
<td><%=id%></td>
<td><ahref="Note?id=<%=id%>&status=selectid"><%=title%></a></td>
<td><%=author%></td>
<td><%=content%></td>
<td><ahref="Note?id=<%=id%>&status=delete">删除</a></td>
</tr>
<%
}
//判断i的值是否改变,如果改变,则表示有内容,反之,无内容
if(i==0)
{
//进行提示
%>
<tr>
<tdcolspan="5">没有任何内容!!!</td>
</tr>
<%
}
%>
</table>
<%
}
else
{
//用户未登陆,提示用户登陆,并跳转
response.setHeader("refresh","2;URL=login.jsp");
%>
您还未登陆,请先登陆!!!<br>
两秒后自动跳转到登陆窗口!!!<br>
如果没有跳转,请按<ahref="login.jsp">这里</a>!!!<br>
<%
}
%>
</center>
</body>
</html>
以上就是本文的全部内容,希望对大家的学习有所帮助。