java留言管理系統(tǒng)中模糊查詢實(shí)例分享
本文分享了一個(gè)基于MVC+DAO的留言管理系統(tǒng),包含增刪改查,其中查詢,有全部查詢和按關(guān)鍵字進(jìn)行模糊查詢的功能,具體內(nèi)容如下
NoteDAO.Java
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 ;
// 查詢?nèi)?
public List queryAll() throws Exception ;
// 模糊查詢
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
{
// 增加操作
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("操作中出現(xiàn)錯(cuò)誤?。?!") ;
}
finally
{
dbc.close() ;
}
}
// 修改操作
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("操作中出現(xiàn)錯(cuò)誤?。?!") ;
}
finally
{
dbc.close() ;
}
}
// 刪除操作
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("操作中出現(xiàn)錯(cuò)誤!?。?) ;
}
finally
{
dbc.close() ;
}
}
// 按ID查詢,主要為更新使用
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("操作中出現(xiàn)錯(cuò)誤?。?!") ;
}
finally
{
dbc.close() ;
}
return note ;
}
// 查詢?nèi)?
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("操作中出現(xiàn)錯(cuò)誤!??!") ;
}
finally
{
dbc.close() ;
}
return all ;
}
// 模糊查詢
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("操作中出現(xiàn)錯(cuò)誤?。。?) ;
}
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" ;
// 接收要操作的參數(shù)值
String status = request.getParameter("status") ;
if(status!=null)
{
// 參數(shù)有內(nèi)容,之后選擇合適的方法
// 查詢?nèi)坎僮?
if("selectall".equals(status))
{
try
{
request.setAttribute("all",DAOFactory.getNoteDAOInstance().queryAll()) ;
}
catch (Exception e)
{
}
path = "list_notes.jsp" ;
}
// 插入操作
if("insert".equals(status))
{
// 1、接收插入的信息
String title = request.getParameter("title") ;
String author = request.getParameter("author") ;
String content = request.getParameter("content") ;
// 2、實(shí)例化VO對(duì)象
Note note = new Note() ;
note.setTitle(title) ;
note.setAuthor(author) ;
note.setContent(content) ;
// 3、調(diào)用DAO完成數(shù)據(jù)庫(kù)的插入操作
boolean flag = false ;
try
{
DAOFactory.getNoteDAOInstance().insert(note) ;
flag = true ;
}
catch (Exception e)
{}
request.setAttribute("flag",new Boolean(flag)) ;
path = "insert_do.jsp" ;
}
// 按ID查詢操作,修改之前需要將數(shù)據(jù)先查詢出來(lái)
if("selectid".equals(status))
{
// 接收參數(shù)
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" ;
}
// 更新操作
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" ;
}
// 模糊查詢
if("selectbylike".equals(status))
{
String keyword = request.getParameter("keyword") ;
try
{
request.setAttribute("all",DAOFactory.getNoteDAOInstance().queryByLike(keyword)) ;
}
catch (Exception e)
{
}
path = "list_notes.jsp" ;
}
// 刪除操作
if("delete".equals(status))
{
// 接收參數(shù)
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
{
// 則表示無(wú)參數(shù),非法的客戶請(qǐng)求
}
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 留言管理程序——登陸</title>
</head>
<body>
<center>
<h1>留言管理范例 —— MVC + DAO實(shí)現(xiàn)</h1>
<hr>
<br>
<%
// 編碼轉(zhuǎn)換
request.setCharacterEncoding("GB2312") ;
if(session.getAttribute("uname")!=null)
{
// 用戶已登陸
%>
<%
// 如果有內(nèi)容,則修改變量i,如果沒(méi)有,則根據(jù)i的值進(jìn)行無(wú)內(nèi)容提示
int i = 0 ;
String keyword = request.getParameter("keyword") ;
List all = null ;
all = (List)request.getAttribute("all") ;
%>
<form action="Note" method="POST">
請(qǐng)輸入查詢內(nèi)容:<input type="text" name="keyword">
<input type="hidden" name="status" value="selectbylike">
<input type="submit" value="查詢">
</form>
</h3><a href="insert.jsp">添加新留言</a></h3>
<table width="80%" border="1">
<tr>
<td>留言ID</td>
<td>標(biāo)題</td>
<td>作者</td>
<td>內(nèi)容</td>
<td>刪除</td>
</tr>
<%
Iterator iter = all.iterator() ;
while(iter.hasNext())
{
Note note = (Note)iter.next() ;
i++ ;
// 進(jìn)行循環(huán)打印,打印出所有的內(nèi)容,以表格形式
// 從數(shù)據(jù)庫(kù)中取出內(nèi)容
int id = note.getId() ;
String title = note.getTitle() ;
String author = note.getAuthor() ;
String content = note.getContent() ;
// 因?yàn)橐P(guān)鍵字返紅,所以此處需要接收查詢關(guān)鍵字
// String keyword = request.getParameter("keyword") ;
if(keyword!=null)
{
// 需要將數(shù)據(jù)返紅
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">刪除</a></td>
</tr>
<%
}
// 判斷i的值是否改變,如果改變,則表示有內(nèi)容,反之,無(wú)內(nèi)容
if(i==0)
{
// 進(jìn)行提示
%>
<tr>
<td colspan="5">沒(méi)有任何內(nèi)容?。?!</td>
</tr>
<%
}
%>
</table>
<%
}
else
{
// 用戶未登陸,提示用戶登陸,并跳轉(zhuǎn)
response.setHeader("refresh","2;URL=login.jsp") ;
%>
您還未登陸,請(qǐng)先登陸?。?!<br>
兩秒后自動(dòng)跳轉(zhuǎn)到登陸窗口!?。?lt;br>
如果沒(méi)有跳轉(zhuǎn),請(qǐng)按<a href="login.jsp">這里</a>?。。?lt;br>
<%
}
%>
</center>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- 圖書(shū)管理系統(tǒng)java版
- Java+Mysql學(xué)生管理系統(tǒng)源碼
- 一個(gè)簡(jiǎn)陋的java圖書(shū)管理系統(tǒng)
- 簡(jiǎn)單實(shí)現(xiàn)Java版學(xué)生管理系統(tǒng)
- java學(xué)生信息管理系統(tǒng)設(shè)計(jì)
- java聯(lián)系人管理系統(tǒng)簡(jiǎn)單設(shè)計(jì)
- Java設(shè)計(jì)模塊系列之書(shū)店管理系統(tǒng)單機(jī)版(一)
- 相冊(cè)管理系統(tǒng)(Java表單+xml數(shù)據(jù)庫(kù)存儲(chǔ))
- java開(kāi)發(fā)就業(yè)信息管理系統(tǒng)
- Java+MySQL實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)源碼
相關(guān)文章
Java 最重要布局管理器GridBagLayout的使用方法
GridBagLayout是java里面最重要的布局管理器之一,可以做出很復(fù)雜的布局,可以說(shuō)GridBagLayout是必須要學(xué)好的的,需要的朋友可以了解下2012-12-12
Java?webservice的POST和GET請(qǐng)求調(diào)用方式
這篇文章主要介紹了Java?webservice的POST和GET請(qǐng)求調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
java實(shí)現(xiàn)的海盜算法優(yōu)化版
這篇文章主要介紹了java實(shí)現(xiàn)的海盜算法優(yōu)化版,結(jié)合實(shí)例形式分析了java海盜算法的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-07-07
基于swagger測(cè)試List類型參數(shù)過(guò)程詳解
這篇文章主要介紹了基于swagger測(cè)試List類型參數(shù)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Java8 HashMap的實(shí)現(xiàn)原理分析
Java8之后新增挺多新東西,接下來(lái)通過(guò)本文給大家介紹Java8 HashMap的實(shí)現(xiàn)原理分析,對(duì)java8 hashmap實(shí)現(xiàn)原理相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-03-03
spring boot搭建文件服務(wù)器解決同時(shí)上傳多個(gè)圖片和下載的問(wèn)題
這篇文章主要介紹了spring boot搭建文件服務(wù)器解決同時(shí)上傳多個(gè)圖片和下載的問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11

