この記事では、MVC+DAOに基づいたメッセージ管理システムを共有します。これには、追加、削除、変更、クエリが含まれ、クエリにはすべてのクエリとキーワードによるファジークエリがあります。特定のコンテンツは次のとおりです
Notedao.java
パッケージcn.mldn.lxh.note.dao; Java.util。*をインポートします。 cn.mldn.lxh.note.vo.*をインポートします。パブリックインターフェイスnotedao {//操作public void insert(note note)スロー例外を追加します。 //操作の変更public void update(note note)をスローする例外。 //削除操作public void delete(int id)スロー例をスローします。 // IDによるクエリ、主にパブリックノートquerybyid(int id)スローを更新するため。 //すべてのパブリックリストqueryall()を照会します。 //ファジークエリ公開リストquerybylike(string cond)スロー例外。 }; Notedaoimpl.java
パッケージcn.mldn.lxh.note.dao.impl; Java.sql。*をインポートします。 Java.util。*をインポートします。 cn.mldn.lxh.note.vo.*をインポートします。 cn.mldn.lxh.note.dao。*; cn.mldn.lxh.note.dbc。*; public class notedaoimpl実装notedao {// public public void insert(note note)throws exception {string sql = "inserting into into(id、title、auther、content)values(note_seave.nextval、?、?、?、?)"; predtatement pstmt = null; DatabaseConnection DBC = null; dbc = new DatabaseConnection(); try {pstmt = dbc.getConnection()。preperestatement(sql); pstmt.setString(1、note.getTitle()); pstmt.setString(2、note.getauthor()); pstmt.setString(3、note.getContent()); pstmt.executeupdate(); pstmt.close(); } catch(例外e){// system.out.println(e);新しい例外をスローします(「操作でエラーが発生しました!!!」); }最後に{dbc.close(); }} //操作の変更public void update(note note)スロー例外{string sql = "update note set title =?、著者=?、content =?where id =?" ; predtatement pstmt = null; DatabaseConnection DBC = null; dbc = new DatabaseConnection(); try {pstmt = dbc.getConnection()。preperestatement(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(例外e){新しい例外をスロー( "操作中にエラーが発生しました!!!"); }最後に{dbc.close(); }} //削除操作public void delete(int id)throws exception {string sql = "delete from note where id =?" ; predtatement pstmt = null; DatabaseConnection DBC = null; dbc = new DatabaseConnection(); try {pstmt = dbc.getConnection()。preperestatement(sql); pstmt.setint(1、id); pstmt.executeupdate(); pstmt.close(); } catch(例外e){新しい例外をスロー( "操作でエラーが発生しました!!!"); }最後に{dbc.close(); }} // idによるクエリ、主に更新の場合はパブリックノートquerybyid(int id)スロー例外{note note = null; string sql = "id、title、著者、content from note where id =?" ; predtatement pstmt = null; DatabaseConnection DBC = null; dbc = new DatabaseConnection(); try {pstmt = dbc.getConnection()。preperestatement(sql); pstmt.setint(1、id);結果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(例外e){新しい例外をスロー( "操作中にエラーが発生しました!!!"); }最後に{dbc.close(); } return note; } //すべてのpublic List queryall()exception {list All = new ArrayList(); string sql = "id、title、著者、noteのコンテンツを選択"; predtatement pstmt = null; DatabaseConnection DBC = null; dbc = new DatabaseConnection(); try {pstmt = dbc.getConnection()。preperestatement(sql);結果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(注); } rs.Close(); pstmt.close(); } catch(Exception e){System.out.println(e);新しい例外をスローします(「操作でエラーが発生しました!!!」); }最後に{dbc.close(); }すべてを返します。 } //ファジークエリパブリックリストquerybylike(string cond)throws exception {list all = new ArrayList(); string sql = "id、title、著者、note from note where where like?or著者のようなものを選択します。 ; predtatement pstmt = null; DatabaseConnection DBC = null; dbc = new DatabaseConnection(); try {pstmt = dbc.getConnection()。preperestatement(sql); pstmt.setString(1、 "%"+cond+"%"); pstmt.setString(2、 "%"+cond+"%"); pstmt.setString(3、 "%"+cond+"%");結果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(注); } rs.Close(); pstmt.close(); } catch(Exception e){System.out.println(e);新しい例外をスローします( "操作中にエラーが発生しました!!!"); }最後に{dbc.close(); }すべてを返します。 }}; Noteservlet.java
パッケージcn.mldn.lxh.note.servlet; java.io.*をインポートします。 javax.servlet。*; javax.servlet.http。*をインポートします。 cn.mldn.lxh.note.factory。*; cn.mldn.lxh.note.vo.*をインポートします。 public class noteservletは、httpservletを拡張します{public void doget(httpservletrequest request、httpservletresponse応答)throws ioexception、servletexception {this.dopost(request、response); } public void dopost(httpservletrequest request、httpservletResponse応答)IoException、servletexception {request.setcharacterencoding( "gb2312"); string path = "errors.jsp"; //操作するパラメーター値を受信する文字列ステータス= request.getParameter( "status"); if(status!= null){//パラメーターにコンテンツがあり、適切なメソッドを選択してください//すべての操作をクエリするif( "selectal" .equals(status)){try {request.setattribute( "all"、daofactory.getNotedaoinstance()。queryall()); } catch(例外e){} path = "list_notes.jsp"; } // insercement操作if( "insert" .equals(status)){// 1。挿入された情報文字列ittitle = request.getParameter( "title");文字列著者= request.getParameter( "author");文字列content = request.getParameter( "content"); //2。VOオブジェクトをインスタンス化しますnote = new note(); note.settitle(title); note.setauthor(著者); note.setContent(content); //3。DAOに電話して、データベース挿入操作Boolean Flag = falseを完了します。 try {daofactory.getNotedaoinstance()。挿入(注); flag = true; } catch(Exception e){} request.setattribute( "flag"、new boolean(flag)); path = "insert_do.jsp"; } // IDによるクエリ操作。変更する前に、最初にデータを照会する必要がありますif( "selectid" .equals(status)){// parameter int id = 0を受信します。 try {id = integer.parseint(request.getParameter( "id")); } catch(exception e){} try {request.setattribute( "note"、daofactory.getNotedaoinstance()。querybyid(id)); } catch(例外e){} path = "update.jsp"; } //操作を更新するif( "update" .equals(status)){int id = 0; try {id = integer.parseint(request.getParameter( "id")); } catch(exception e){}文字列タイトル= request.getParameter( "title");文字列著者= request.getParameter( "author");文字列content = request.getParameter( "content"); NOTE NOTE = new Note(); note.setId(id); note.settitle(title); note.setauthor(著者); note.setContent(content);ブールフラグ= 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(例外e){} path = "list_notes.jsp"; } // delete操作if( "delete" .equals(status)){// parameter int id = 0を受信します; try {id = integer.parseint(request.getParameter( "id")); } catch(例外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.lxh.note.servlet.noteservlet </servlet-class> </servlet> <servlet-name> note </servet-name> <url-pattern>/note/note_mvc/url> </url-tilltern> </url-tilltin list_notes.jsp
<%@ page contentType = "text/html; charset = gb2312"%> <%@ page import = "java.util。*"%> <%@ page import = "cn.mldn.lxh.note.vo。管理の例-MVC + DAO実装</h1> <hr> <br> <%//変換要求のエンコード.setcharacterencoding( "gb2312"); if(session.getAttribute( "uname")!= null){//ユーザーは%> <%//で記録されます。コンテンツがある場合は、変数iを変更します。 noがある場合、iの値に基づいたコンテンツプロンプトはありません。文字列キーワード= request.getParameter( "keyword");すべて= 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>タイトル</td> <td>著者</td> <td> content </td> <td> delete </td> </tr> <%iterator iter = all.iterator(); while(iter.hasnext()){note note =(note)iter.next(); i ++; //ループ印刷を実行して、すべてのコンテンツをテーブルフォームに印刷する//データベースからコンテンツを削除int id = note.getId();文字列タイトル= note.getTitle();文字列著者= note.getauthor();文字列コンテンツ= note.getContent(); //キーワードは赤いので、ここでクエリキーワードを受信する必要があります//文字列キーワード= request.getParameter( "keyword"); if(keyword!= null){//データはreddited tittite = title.replaceall(keyword、 "<font color =/" red/">"+keyword+"</font>");著者=著者.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 = <%= id%>&status = secretId"> <%= title%> </a> </td> < href = "note?id?id = <%= id%>&status = delete"> delete </a> </td> </tr> <%} // iの値が変わるかどうかを判断します。変更された場合、コンテンツがあることを意味します。それ以外の場合、(i == 0){// prompt%> <tr> <td colspan = "5">何もしない場合、コンテンツはありません。 ! ! </td> </tr> <%}%> </table> <%} else {//ユーザーはログインしていない、ユーザーにログインするように促し、respons.setheader( "refresh"、 "2; url = login.jsp"); %>まだログインしていないので、最初にログインしてください! ! ! <br> 2秒でログインウィンドウに自動的にジャンプします! ! ! <br>ジャンプがない場合は、<a href = "login.jsp">ここに押します</a>! ! ! <br> <%}%> </center> </body> </html>上記はこの記事に関するものです。すべての人の学習に役立つことを願っています。