This article shares a message management system based on Mvc+dao, including additions and deletions, which query, there are all inquiries and by keyword fuzzy query function, the specific contents are as follows
Notedao.java
Package Cn.mldn.lxh.note.dao;
Import java.util.*;
Import cn.mldn.lxh.note.vo.*;
Public interface Notedao
{
//Add operation public
void Insert (note) throws Exception;
Modify operation public
void Update (note) throws Exception;
Delete operation public
void Delete (int id) throws Exception;
Query by ID, mainly for updates using public note
Querybyid (int id) throws Exception;
Query all public
List Queryall () throws Exception;
Fuzzy query 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 {//Add operations public void Insert (note) throws Exception {Stri
ng sql = "INSERT into the 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 ("An error occurred in the operation!!!
") ;
finally {dbc.close (); }///modify operation public void update 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 (); An error occurred in the catch (Exception e) {throw new Exception ("Operation")!!!
") ;
finally {dbc.close ();
}//delete operation 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 (); An error occurred in the catch (Exception e) {throw new Exception ("Operation")!!!
") ;
finally {dbc.close ();
}///by ID query, mainly for update using public note Querybyid (int id) throws Exception {note = null;
String sql = "Select id,title,author,content from 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 (); An error occurred in the catch (Exception e) {throw new Exception ("Operation")!!!
") ; finally {DBC.CLose ();
} return note;
//Query All 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 ()) {The note Note = The 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 ("An error occurred in the operation!!!
") ;
finally {dbc.close ();
return all;
///Fuzzy query public List querybylike (String cond) throws Exception{List all = new ArrayList (); String sql = "Select Id,title,author,content from WHERE is 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 ()) {The note Note = The 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 ("An error occurred in the operation!!!
") ;
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 respon
SE) throws ioexception,servletexception {this.dopost (request,response);
} public void DoPost (HttpServletRequest request,httpservletresponse response) throws Ioexception,servletexception {
Request.setcharacterencoding ("GB2312");
String Path = "errors.jsp";
Receives the parameter value to be manipulated String status = Request.getparameter ("status");
if (status!=null) {//parameter has content, then select the appropriate method//query all operations if ("SelectAll". Equals (status)) {
try {request.setattribute ("All", Daofactory.getnotedaoinstance (). Queryall ());
catch (Exception e) {} path = "list_notes.jsp"; }
Insert Action if ("Insert". Equals (status)) {//1, receive inserted information String title = Request.getparam
Eter ("title");
String author = request.getparameter ("author");
String content = request.getparameter ("content");
2, instantiate the Vo object note = The new note ();
Note.settitle (title);
Note.setauthor (author);
Note.setcontent (content);
3. Invoke DAO to complete the insert operation of the database Boolean flag = FALSE;
try {daofactory.getnotedaoinstance (). Insert (note);
Flag = true;
catch (Exception e) {} request.setattribute ("Flag", new Boolean (flag));
Path = "insert_do.jsp";
//By ID query operation, before you modify the data need to query out if ("Selectid". Equals (status)) {//receive parameter 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";
}//Update operation 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 = The 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"; ///Fuzzy query if ("Selectbylike". Equals (status)) {String keyword = request.getparameter ("keyword
") ;
try {request.setattribute ("All", Daofactory.getnotedaoinstance (). Querybylike (keyword));
catch (Exception e) {} path = "list_notes.jsp";
///delete operation if ("Delete". Equals (status)) {//receive parameter 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 {/= no parameters, illegal client request} reqUest.getrequestdispatcher (Path). Forward (Request,response);
}
}; /* <servlet> <servlet-name>note</servlet-name> <servlet-class>cn.mldn.lxh.note.servlet.no teservlet</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.M" Ldn.lxh.note.vo.* "%>
The above is the entire content of this article, I hope to help you learn.