บทความนี้แชร์ระบบการจัดการข้อความตาม MVC+DAO ซึ่งรวมถึงการเพิ่มการลบการดัดแปลงและการสืบค้นและแบบสอบถามมีการสืบค้นและการสืบค้นฟัซซี่ทั้งหมดตามคำหลัก เนื้อหาเฉพาะมีดังนี้
notedao.java
แพ็คเกจ cn.mldn.lxh.note.dao; นำเข้า Java.util.*; นำเข้า cn.mldn.lxh.note.vo.*; อินเตอร์เฟสสาธารณะ notedao {// เพิ่มการใช้งานโมฆะสาธารณะแทรก (หมายเหตุ) โยนข้อยกเว้น; // แก้ไขการดำเนินการอัพเดตสาธารณะสาธารณะ (หมายเหตุ) โยนข้อยกเว้น; // ลบการดำเนินการเป็นโมฆะสาธารณะลบ (ID int) โยนข้อยกเว้น; // Query โดย ID ส่วนใหญ่สำหรับการอัปเดตโน้ตสาธารณะ queryById (ID int) โยนข้อยกเว้น; // สอบถามรายการสาธารณะทั้งหมด queryall () โยนข้อยกเว้น; // Fuzzy Query รายการสาธารณะ 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.*; คลาสสาธารณะ NoteDaoimpl ใช้งาน notedao {// เพิ่มการดำเนินการ public public void แทรก (หมายเหตุ) พ่นข้อยกเว้น {string sql = "แทรกลงในหมายเหตุ (id, ชื่อ, ผู้แต่ง, เนื้อหา) ค่า (note_ecome.nextval,?,?)"; PreparedStatement PSTMT = NULL; Databaseconnection dbc = null; dbc = databaseconnection ใหม่ (); ลอง {pstmt = dbc.getConnection (). การเตรียมการ (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); โยนข้อยกเว้นใหม่ ("เกิดข้อผิดพลาดในการดำเนินการ !!!"); } ในที่สุด {dbc.close (); }} // แก้ไขการอัพเดทการดำเนินการสาธารณะการดำเนินการ (หมายเหตุ) โยนข้อยกเว้น {String sql = "อัปเดตโน้ตชื่อชื่อ =?, uther =?, content =? id =?" - PreparedStatement PSTMT = NULL; Databaseconnection dbc = null; dbc = databaseconnection ใหม่ (); ลอง {pstmt = dbc.getConnection (). การเตรียมการ (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) {โยนข้อยกเว้นใหม่ ("เกิดข้อผิดพลาดระหว่างการดำเนินการ!!"); } ในที่สุด {dbc.close (); }} // ลบการดำเนินการสาธารณะโมฆะลบ (int id) พ่นข้อยกเว้น {string sql = "ลบออกจากหมายเหตุที่ id =?" - PreparedStatement PSTMT = NULL; Databaseconnection dbc = null; dbc = databaseconnection ใหม่ (); ลอง {pstmt = dbc.getConnection (). การเตรียมการ (SQL); pstmt.setint (1, id); pstmt.executeUpdate (); pstmt.close (); } catch (exception e) {โยนข้อยกเว้นใหม่ ("เกิดข้อผิดพลาดในการดำเนินการ !!!"); } ในที่สุด {dbc.close (); }} // Query โดย ID ส่วนใหญ่สำหรับการอัปเดตบันทึกสาธารณะ queryById (int id) โยนข้อยกเว้น {note note = null; String SQL = "SELECT ID ชื่อผู้แต่งเนื้อหาจากหมายเหตุที่ id =?" - PreparedStatement PSTMT = NULL; Databaseconnection dbc = null; dbc = databaseconnection ใหม่ (); ลอง {pstmt = dbc.getConnection (). การเตรียมการ (SQL); pstmt.setint (1, id); resultset rs = pstmt.executeQuery (); if (rs.next ()) {note = net 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) {โยนข้อยกเว้นใหม่ ("เกิดข้อผิดพลาดระหว่างการดำเนินการ!!"); } ในที่สุด {dbc.close (); } return note; } // Query รายการสาธารณะทั้งหมด queryall () พ่นข้อยกเว้น {list all = new ArrayList (); String SQL = "SELECT ID, TITLE, AUTHER, เนื้อหาจาก NOTE"; PreparedStatement PSTMT = NULL; Databaseconnection dbc = null; dbc = databaseconnection ใหม่ (); ลอง {pstmt = dbc.getConnection (). การเตรียมการ (SQL); resultset rs = pstmt.executeQuery (); ในขณะที่ (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 (); } ส่งคืนทั้งหมด; } // Fuzzy Query รายการสาธารณะ querybylike (สตริง cond) พ่นข้อยกเว้น {list all = new ArrayList (); String SQL = "เลือก ID, ชื่อเรื่อง, ผู้แต่ง, เนื้อหาจากโน้ตที่ชื่อชอบหรือผู้เขียนชอบหรือเนื้อหาเช่น?" - PreparedStatement PSTMT = NULL; Databaseconnection dbc = null; dbc = databaseconnection ใหม่ (); ลอง {pstmt = dbc.getConnection (). การเตรียมการ (SQL); pstmt.setstring (1, "%"+cond+"%"); pstmt.setstring (2, "%"+cond+"%"); pstmt.setstring (3, "%"+cond+"%"); resultset rs = pstmt.executeQuery (); ในขณะที่ (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 (); } ส่งคืนทั้งหมด; - noteervlet.java
แพ็คเกจ cn.mldn.lxh.note.servlet; นำเข้า Java.io.*; นำเข้า Javax.servlet.*; นำเข้า Javax.servlet.http.*; นำเข้า cn.mldn.lxh.note.factory.*; นำเข้า cn.mldn.lxh.note.vo.*; Noteservlet ระดับสาธารณะขยาย httpservlet {public void doget (คำขอ httpservletrequest, การตอบสนอง httpservletResponse) โยน ioexception, servletexception {this.dopost (คำขอ, การตอบสนอง); } โมฆะสาธารณะ dopost (คำขอ httpservletrequest, การตอบสนอง httpservletResponse) พ่น IOException, servletexception {request.Setcharacterencoding ("GB2312"); String Path = "Errors.jsp"; // รับค่าพารามิเตอร์เป็นสถานะสตริงที่ดำเนินการ = request.getParameter ("สถานะ"); if (สถานะ! = null) {// พารามิเตอร์มีเนื้อหาจากนั้นเลือกวิธีการที่เหมาะสม // query การดำเนินการทั้งหมดถ้า ("selectall" .equals (สถานะ)) {ลอง {request.setAttribute ("ทั้งหมด", daofactory.getNotEdaoInstance (). queryall (); } catch (exception e) {} path = "list_notes.jsp"; } // แทรกการดำเนินการถ้า ("แทรก" .equals (สถานะ)) {// 1. รับข้อมูลสตริงข้อมูลที่แทรก = request.getParameter ("title"); String Author = request.getParameter ("Author"); String Content = request.getParameter ("เนื้อหา"); // 2. อินสแตนซ์ออบเจ็กต์ Note Note = net note (); note.settitle (ชื่อ); note.setauthor (ผู้แต่ง); note.setContent (เนื้อหา); // 3. โทรหา DAO เพื่อให้การดำเนินการแทรกฐานข้อมูลการดำเนินการบูลีนธง = false; ลอง {daofactory.getNotEdaoInstance (). แทรก (หมายเหตุ); ธง = จริง; } catch (Exception E) {} request.setAttribute ("Flag", ใหม่บูลีน (Flag)); path = "insert_do.jsp"; } // การดำเนินการแบบสอบถามตาม ID ก่อนการแก้ไขคุณจะต้องสืบค้นข้อมูลก่อนถ้า ("เลือก" .Equals (สถานะ)) {// รับพารามิเตอร์ int ID = 0; ลอง {id = integer.parseint (request.getParameter ("id")); } catch (exception e) {} ลอง {request.setAttribute ("note", daofactory.getNotEdaoInstance (). queryById (id)); } catch (exception e) {} path = "update.jsp"; } // อัปเดตการดำเนินการถ้า ("อัปเดต" .equals (สถานะ)) {int id = 0; ลอง {id = integer.parseint (request.getParameter ("id")); } catch (exception e) {} title title = request.getParameter ("title"); String Author = request.getParameter ("Author"); String Content = request.getParameter ("เนื้อหา"); หมายเหตุหมายเหตุ = โน้ตใหม่ (); note.setId (id); note.settitle (ชื่อ); note.setauthor (ผู้แต่ง); note.setContent (เนื้อหา); ธงบูลีน = เท็จ; ลอง {daofactory.getNotEdaoInstance (). อัปเดต (หมายเหตุ); ธง = จริง; } catch (Exception E) {} request.setAttribute ("Flag", ใหม่บูลีน (Flag)); path = "update_do.jsp"; } // การสืบค้นฟัซซี่ถ้า ("selectbylike" .equals (สถานะ)) {สตริงคีย์เวิร์ด = request.getParameter ("คำหลัก"); ลอง {request.setAttribute ("All", daofactory.getNotEdaoInstance (). QueryBylike (คำหลัก)); } catch (exception e) {} path = "list_notes.jsp"; } // ลบการดำเนินการถ้า ("ลบ" .Equals (สถานะ)) {// รับพารามิเตอร์ int ID = 0; ลอง {id = integer.parseint (request.getParameter ("id")); } catch (exception e) {} boolean flag = false; ลอง {daofactory.getNotEdaoInstance (). ลบ (id); ธง = จริง; } catch (Exception E) {} request.setAttribute ("Flag", ใหม่บูลีน (Flag)); path = "delete_do.jsp"; }} else {// หมายความว่าไม่มีพารามิเตอร์คำขอลูกค้าที่ผิดกฎหมาย} request.getRequestDispatcher (เส้นทาง). forward (คำขอ, การตอบกลับ); - / * <servlet> <servlet-name> หมายเหตุ </servlet-name> <servlet-class> cn.mldn.lxh.note.servlet.noteservlet </servlet-class> </servlet> <servlet-mapping> <servlet-name> note </servlet-name> list_notes.jsp
<%@ page contentType = "text/html; charset = gb2312"%> <%@ หน้านำเข้า = "java.util.*"%> <%@ page import = "cn.mldn.lxh.note.vo.*"%> <html> <head> <head> <head> การจัดการตัวอย่าง MVC + DAO การใช้งาน </h1> <hr> <br> <% // การเข้ารหัสคำขอแปลงการแปลง Setcharacterencoding ("GB2312"); if (session.getAttribute ("uname")! = null) {// ผู้ใช้ถูกบันทึกใน %> < % // หากมีเนื้อหาให้แก้ไขตัวแปร i หากไม่มีไม่มีข้อความแจ้งเนื้อหาตามค่าของฉัน int i = 0; สตริงคีย์เวิร์ด = request.getParameter ("คำหลัก"); รายการทั้งหมด = null; all = (list) request.getAttribute ("all"); %> <form action = "note" method = "post"> โปรดป้อนเนื้อหาการสืบค้น: <อินพุต type = "text" name = "คำหลัก"> <อินพุตประเภท = "hidden" name = "สถานะ" value = "selectbylike"> <อินพุต type = "subment" value = "query"> </form> </h3> ID </td> <td> title </td> <td> ผู้แต่ง </td> <td> เนื้อหา </td> <td> ลบ </td> </tr> <% iterator iter = all.iterator (); ในขณะที่ (iter.hasnext ()) {note note = (note) iter.next (); i ++; // ดำเนินการพิมพ์ลูปเพื่อพิมพ์เนื้อหาทั้งหมดในรูปแบบตาราง // ลบเนื้อหาออกจากฐานข้อมูล int id = note.getId (); String title = note.getTitle (); String Author = note.getAuthor (); เนื้อหาสตริง = note.getContent (); // เนื่องจากคำหลักเป็นสีแดงคุณต้องรับคีย์เวิร์ดแบบสอบถามที่นี่ // คีย์เวิร์ดสตริง = request.getParameter ("คำหลัก"); if (keyword! = null) {// ข้อมูลจะต้องเป็น reddited title = title.replaceall (คำหลัก, "<font color =/" red/">"+คำหลัก+"</font>"); ผู้เขียน = uther.replaceall (คำหลัก, "<font color =/" red/">" +คำหลัก +"</font>"); content = content.replaceall (คำหลัก, "<font color =/" red/">" +คำหลัก +"</font>"); }%> <tr> <td> <%= id%> </td> <td> <a href = "หมายเหตุ? id = <%= id%> & status = selectId"> <%= title%> </a> </td> <td> <%= ผู้เขียน href = "หมายเหตุ? id = <%= id%> & status = ลบ"> ลบ </a> </td> </tr> <%} // กำหนดว่าค่าของฉันเปลี่ยนแปลงหรือไม่ หากมีการเปลี่ยนแปลงหมายความว่ามีเนื้อหา มิฉะนั้นจะไม่มีเนื้อหาถ้า (i == 0) {// สร้างพรอมต์ %> <tr> <td colspan = "5"> ไม่มีอะไร! - - </td> </tr> <%}%> </bable> <%} อื่น {// ผู้ใช้ไม่ได้เข้าสู่ระบบแจ้งให้ผู้ใช้เข้าสู่ระบบและข้ามไปที่การตอบสนอง %> คุณยังไม่ได้ลงชื่อเข้าใช้โปรดเข้าสู่ระบบก่อน! - - <br> มันจะข้ามไปที่หน้าต่างเข้าสู่ระบบโดยอัตโนมัติภายในสองวินาที! - - <br> หากไม่มีการกระโดดกด <a href = "login.jsp"> ที่นี่ </a>! - - <br> < %} %> </enter> </body> </html>ข้างต้นเป็นเรื่องเกี่ยวกับบทความนี้ฉันหวังว่ามันจะเป็นประโยชน์กับการเรียนรู้ของทุกคน