欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java留言管理系统中模糊查询实例分享

程序员文章站 2024-03-09 17:00:53
本文分享了一个基于mvc+dao的留言管理系统,包含增删改查,其中查询,有全部查询和按关键字进行模糊查询的功能,具体内容如下 notedao.java pa...

本文分享了一个基于mvc+dao的留言管理系统,包含增删改查,其中查询,有全部查询和按关键字进行模糊查询的功能,具体内容如下
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 ; 
  // 查询全部 
  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("操作中出现错误!!!") ; 
    } 
    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("操作中出现错误!!!") ; 
    } 
    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("操作中出现错误!!!") ; 
    } 
    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("操作中出现错误!!!") ; 
    } 
    finally 
    { 
      dbc.close() ; 
    } 
    return note ; 
  } 
  // 查询全部 
  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("操作中出现错误!!!") ; 
    } 
    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("操作中出现错误!!!") ; 
    } 
    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" ; 
    // 接收要操作的参数值 
    string status = request.getparameter("status") ; 
    if(status!=null) 
    { 
      // 参数有内容,之后选择合适的方法 
      // 查询全部操作 
      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、实例化vo对象 
        note note = new note() ; 
        note.settitle(title) ; 
        note.setauthor(author) ; 
        note.setcontent(content) ; 
        // 3、调用dao完成数据库的插入操作 
        boolean flag = false ; 
        try 
        { 
          daofactory.getnotedaoinstance().insert(note) ; 
          flag = true ; 
        } 
        catch (exception e) 
        {} 
        request.setattribute("flag",new boolean(flag)) ; 
        path = "insert_do.jsp" ; 
      } 
      // 按id查询操作,修改之前需要将数据先查询出来 
      if("selectid".equals(status)) 
      { 
        // 接收参数 
        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)) 
      { 
        // 接收参数 
        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 
    { 
      // 则表示无参数,非法的客户请求 
    } 
    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实现</h1> 
  <hr> 
  <br> 
  <% 
    // 编码转换 
    request.setcharacterencoding("gb2312") ; 
    if(session.getattribute("uname")!=null) 
    { 
      // 用户已登陆 
  %> 
  <% 
    // 如果有内容,则修改变量i,如果没有,则根据i的值进行无内容提示 
    int i = 0 ; 
    string keyword = request.getparameter("keyword") ; 
    list all = null ; 
    all = (list)request.getattribute("all") ; 
  %> 
<form action="note" method="post"> 
  请输入查询内容:<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>标题</td> 
    <td>作者</td> 
    <td>内容</td> 
    <td>删除</td> 
  </tr> 
  <% 
      iterator iter = all.iterator() ; 
      while(iter.hasnext()) 
      { 
        note note = (note)iter.next() ; 
        i++ ; 
        // 进行循环打印,打印出所有的内容,以表格形式 
        // 从数据库中取出内容 
        int id = note.getid() ; 
        string title = note.gettitle() ; 
        string author = note.getauthor() ; 
        string content = note.getcontent() ; 
         
        // 因为要关键字返红,所以此处需要接收查询关键字 
        // string keyword = request.getparameter("keyword") ; 
        if(keyword!=null) 
        { 
          // 需要将数据返红 
          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的值是否改变,如果改变,则表示有内容,反之,无内容 
      if(i==0) 
        { 
      // 进行提示 
  %> 
        <tr> 
          <td colspan="5">没有任何内容!!!</td> 
        </tr> 
  <% 
      } 
  %> 
</table> 
 
  <% 
    } 
    else 
    { 
      // 用户未登陆,提示用户登陆,并跳转 
      response.setheader("refresh","2;url=login.jsp") ; 
  %> 
      您还未登陆,请先登陆!!!<br> 
      两秒后自动跳转到登陆窗口!!!<br> 
      如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br> 
  <% 
    } 
  %> 
</center> 
</body> 
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助。