이 기사에는 추가, 삭제, 수정 및 쿼리가 포함 된 MVC+DAO를 기반으로하는 메시지 관리 시스템이 공유되며 쿼리에는 키워드의 모든 쿼리 및 퍼지 쿼리가 있습니다. 특정 내용은 다음과 같습니다
Notao.java
패키지 cn.mldn.lxh.note.dao; java.util.*; cn.mldn.lxh.note.vo.*; public interc // 작업 수정 공개 공공 보이드 업데이트 (참고 참고) 예외가 발생합니다. // 삭제 작업 공개 void delete (int id)는 예외를 던집니다. // 주로 공개 노트 업데이트 QueryById (int id)를 업데이트하기 위해 ID의 쿼리는 예외를 발생시킵니다. // 쿼리 모든 공개 목록 QueryAll ()는 예외를 던집니다. // 퍼지 쿼리 공개 목록 QueryBylike (String Cond)는 예외를 던집니다. };
Notaoimpl.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.*; 공개 클래스에 대한 공개 class undaoimpl은 undao {// 작업 추가 공개 void 인서트 (참고 참고) 예외 {string sql = "삽입 (id, title, aworder, content) 값 (note_sect.nextVal, ?, ?,?)"; 준비된 상태 PSTMT = NULL; DatabaseConnection DBC = NULL; dbc = 새로운 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 (예외 e) {// system.out.println (e); 새로운 예외를 던지십시오 ( "작업에서 오류가 발생했습니다 !!!"); } 마침내 {dbc.close (); }} // 작업 수정 공개 void update (참고 참고) Exception {String sql = "업데이트 참고 설정 속성 =?, author =?, content =? where id =?" ; 준비된 상태 PSTMT = NULL; DatabaseConnection DBC = NULL; dbc = 새로운 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 (예외 e) {새 예외 던지기 ( "작업 중에 오류가 발생했습니다!!!"); } 마침내 {dbc.close (); }} // 삭제 작업 공개 void delete (int id)는 예외를 던지려면 {string sql = "id =?"참고에서 삭제합니다. ; 준비된 상태 PSTMT = NULL; DatabaseConnection DBC = NULL; dbc = 새로운 DatabaseConnection (); try {pstmt = dbc.getConnection (). preparestatement (sql); pstmt.setint (1, id); pstmt.executeupdate (); pstmt.close (); } catch (예외 e) {Throw new Exception ( "작업에서 오류가 발생했습니다 !!!"); } 마침내 {dbc.close (); }}} // ID의 쿼리, 주로 업데이트에 대한 쿼리 공개 노트 QueryById (int id)는 예외 {note note = null; 문자열 sql = "id, 제목, 저자, id =?" ; 준비된 상태 PSTMT = NULL; DatabaseConnection DBC = NULL; dbc = 새로운 DatabaseConnection (); try {pstmt = dbc.getConnection (). preparestatement (sql); pstmt.setint (1, id); resultet rs = pstmt.executeQuery (); if (rs.next ()) {note = new Note (); note.setId (rs.getint (1)); 주석 (Rs.GetString (2)); 참고 .setAuthor (rs.getString (3)); 참고 .SetContent (Rs.GetString (4)); } rs.close (); pstmt.close (); } catch (예외 e) {새 예외 던지기 ( "작업 중에 오류가 발생했습니다!!!"); } 마침내 {dbc.close (); } 리턴 참고; } // query 모든 공개 목록 QueryAll ()는 예외 {list all = new ArrayList (); 문자열 sql = "select id, title, author, content from note"; 준비된 상태 PSTMT = NULL; DatabaseConnection DBC = NULL; dbc = 새로운 DatabaseConnection (); try {pstmt = dbc.getConnection (). preparestatement (sql); resultet rs = pstmt.executeQuery (); while (rs.next ()) {note note = new Note (); note.setId (rs.getint (1)); 주석 (Rs.GetString (2)); 참고 .setAuthor (rs.getString (3)); 참고 .SetContent (Rs.GetString (4)); all.add (주); } rs.close (); pstmt.close (); } catch (예외 e) {System.out.println (e); 새로운 예외를 던지십시오 ( "작업에서 오류가 발생했습니다 !!!"); } 마침내 {dbc.close (); } 모두를 반환합니다. } // 퍼지 쿼리 공개 목록 QueryBylike (String Cond)는 예외 {list all = new ArrayList (); String SQL = "제목, 제목, 제목, 컨텐츠를 선택한 제목 또는 저자가 좋아 하는가? 또는 콘텐츠를 선택하십시오." ; 준비된 상태 PSTMT = NULL; DatabaseConnection DBC = NULL; dbc = 새로운 DatabaseConnection (); try {pstmt = dbc.getConnection (). preparestatement (sql); PSTMT.SetString (1, "%"+cond+"%"); PSTMT.SetString (2, "%"+cond+"%"); PSTMT.SetString (3, "%"+cond+"%"); resultet rs = pstmt.executeQuery (); while (rs.next ()) {note note = new Note (); note.setId (rs.getint (1)); 주석 (Rs.GetString (2)); 참고 .setAuthor (rs.getString (3)); 참고 .SetContent (Rs.GetString (4)); all.add (주); } rs.close (); pstmt.close (); } catch (예외 e) {System.out.println (e); 새로운 예외를 던지십시오 ( "수술 중에 오류가 발생했습니다!!"); } 마침내 {dbc.close (); } 모두를 반환합니다. }}; noteservlet.java
패키지 cn.mldn.lxh.note.servlet; java.io.*; javax.servlet.*; javax.servlet.http.* import; cn.mldn.lxh.note.factory import.*; cn.mldn.lxh.note.vo.*; Public Class NotesVlet은 httpservlet {public void doget (httpservletrequest 요청, httpservletreponse responsk)을 ioexception, servletexception {this.dopost (요청, 응답); } public void dopost (httpservletRequest 요청, httpservletResponse 응답)는 ioException, servleTeXception {request.SetchAracterEncoding ( "GB2312"); 문자열 path = "errors.jsp"; // 작동 할 매개 변수 값을 수신 문자열 상태 = request.getParameter ( "status"); if (status! = null) {// 매개 변수에는 내용이 있습니다. 그런 다음 적절한 메소드를 선택한 다음 // 모든 작업을 쿼리하면 ( "selectall".equals (status)) {try {request.setattribute ( "all", daofactory.getNotedAoinStance ()). QueryAll (); } catch (예외 e) {} path = "list_notes.jsp"; } // if ( "insert".equals (status)) {// 1. 삽입 된 정보 문자열 제목 = request.getParameter ( "title"); 문자열 author = request.getParameter ( "author"); 문자열 내용 = request.getParameter ( "Content"); // 2. VO 객체 참고를 인스턴스화합니다. 참고 = 새 note (); 주석 (제목); 참고 .setAuthor (저자); 참고 .SetContent (컨텐츠); // 3. DAO를 호출하여 데이터베이스 삽입 작업 부울 플래그 = false; try {daofactory.getnotedaoinstance (). insert (참고); flag = true; } catch (예외 e) {} request.setAttribute ( "flag", new boolean (flag)); path = "insert_do.jsp"; } // ID의 쿼리 작업. 수정하기 전에 먼저 데이터를 쿼리해야합니다. ( "selectId".equals (status)) {// 매개 변수를 받으십시오 int id = 0; try {id = integer.parseint (request.getParameter ( "id")); } catch (예외 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 (예외 e) {} 문자열 제목 = request.getParameter ( "title"); 문자열 author = request.getParameter ( "author"); 문자열 내용 = request.getParameter ( "Content"); 참고 사항 = 새 note (); note.setId (id); 주석 (제목); 참고 .setAuthor (저자); 참고 .SetContent (컨텐츠); 부울 플래그 = 거짓; try {daofactory.getnotedaoinstance (). update (참고); flag = true; } catch (예외 e) {} request.setAttribute ( "flag", new boolean (flag)); Path = "Update_do.jsp"; } // fuzzy query if ( "selectbylike".equals (status)) {문자열 keyword = request.getParameter ( "keyword"); try {request.setattribute ( "all", daofactory.getNotedaoinstance (). QueryBy Like (키워드)); } catch (예외 e) {} path = "list_notes.jsp"; } // If ( "delete".equals (status)) {// int id = 0; try {id = integer.parseint (request.getParameter ( "id")); } catch (예외 e) {} 부울 플래그 = false; try {daofactory.getnotedaoinstance (). delete (id); flag = true; } catch (예외 e) {} request.setAttribute ( "flag", new boolean (flag)); 경로 = "delete_do.jsp"; }} else {// 매개 변수가 없음을 의미합니다. 불법 고객 요청} request.getRequestDisPatcher (PATH) .forward (요청, 응답); }}; / * <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_mc/not list_notes.jsp
<%@ page contenttype = "text/html; charset = gb2312"%> <%@ page import = "java.util.*"%> <%@ page import = "cn.mldn.lxh.note.vo. <H1> 메시지 관리 예제 MVC + DAO 구현 </h1> <hr> <br> <% // 변환 요청을 인코딩합니다 .SetchAracterEncoding ( "GB2312"); if (session.getAttribute ( "Uname")! = null) {// 사용자가 %로 로그인됩니다. 아니오가 있으면 I의 값 int i = 0을 기반으로 컨텐츠 프롬프트가 없습니다. 문자열 키워드 = request.getParameter ( "키워드"); all = null을 나열하십시오. all = (list) request.getAttribute ( "all"); %> <form action = "note"method = "post"> 쿼리 내용을 입력하십시오 : <input type = "text"name = "keyword"> <input type = "hidden"name = "sections"value = "selectbylike"> <input type = "value ="query "> </form> </h3> <a href ="insert.jsp "> 새로운 메시지를 추가하십시오. <td> 메시지 id </td> <td> 제목 </td> <td> author </td> <td> 컨텐츠 </td> <td> delete </td> </tr> <% iterator iter = all.iterator (); while (iter.hasnext ()) {note note = (참고) iter.next (); i ++; // 루프 인쇄를 수행하여 모든 내용을 테이블 양식으로 인쇄합니다. // 데이터베이스에서 컨텐츠 제거 int id = note.getId (); 문자열 제목 = note.getTitle (); 문자열 awork = note.getAuthor (); 문자열 content = note.getContent (); // 키워드는 빨간색이므로 여기에서 쿼리 키워드를 받어야합니다. if (keyword! = null) {// 데이터는 reddited title = title.replaceall (키워드, "<font color =/"red/">"+keyword+"</font>"); author = author.replaceall (키워드, "<font color =/"red/">" +keyword +"</font>"); content = content.replaceall (키워드, "<font color =/"red/">" +키워드 +"</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> <%} // i 값이 변경되는지 여부를 결정합니다. 변경되면 콘텐츠가 있음을 의미합니다. 그렇지 않으면 (i == 0) {// 프롬프트를 만듭니다. ! ! </td> </tr> <%}%> </table> <%} else {// 사용자가 로그인하지 않고 사용자가 로그인하고 응답으로 점프합니다 .SetheAder ( "CROFFEM", "2; url = login.jsp"); %> 아직 로그인되지 않았습니다. 먼저 로그인하십시오! ! ! <br> 2 초 안에 자동으로 로그인 창으로 이동합니다! ! ! <br> 점프가 없으면 <a href = "login.jsp"> here </a>를 누릅니다! ! ! <br> < %} %> </center> </body> </html>위의 내용은이 기사에 관한 모든 것입니다. 모든 사람의 학습에 도움이되기를 바랍니다.