Xin's message board project ==== super-touched dbUtil implements the message board and message board dbutil

Source: Internet
Author: User

Xin's message board project ==== super-touched dbUtil implements the message board and message board dbutil

Message Board Management System

My completion:

After submission:

Basic Structure of my message board

Create a message board database:

At the beginning, I wrote a message form on the homepage:

<Body> 

Start to create a level in the background Java class
I first wrote an entity class in my entity layer to implement the added entity class.

public class MessageBorad {    private int id;    private String message;    private String author;    private Date pastTime;        public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getMessage() {        return message;    }    public void setMessage(String message) {        this.message = message;    }    public String getAuthor() {        return author;    }    public void setAuthor(String author) {        this.author = author;    }    public Date getPastTime() {        return pastTime;    }    public void setPastTime(Date pastTime) {        this.pastTime = pastTime;    }}

Write the BaseDao tool class in my Dao Layer

Public class BaseDao {public static final String driver = "com. mysql. jdbc. driver "; public static final String url =" jdbc: mysql: // localhost: 3306/Database Name "; public static final String username =" root "; public static final String password = "1234"; public Connection con; public PreparedStatement ps; public ResultSet rs; static {Class. forname (driver);} public Connection getConnection () throws Exception {if (con = null | con. isClosed () {con = DriverManager. getConnection (url, username, password);} return con;} public void closeResources () throws Exception {rs. close (); ps. close (); con. close () ;}// execute add, delete, and modify public int exeuteUpdate (String SQL, Object... objs) throws Exception {con = getConnection (); ps = con. prepareStatement (SQL); for (int I = 0; I <objs. length; I ++) {ps. setObject (I + 1, objs [I]);} int count?ps.exe cuteUpdate (); return count ;}// obtain the result set ResultSet public ResultSet executeQuery (String SQL, Object... objs) throws Exception {con = getConnection (); ps = con. prepareStatement (SQL); for (int I = 0; I <objs. length; I ++) {ps = setObject (I + 1, objs [I]);} rs1_ps.exe cuteQuery (); return rs;} public int delete (int id) throws Exception {return 0 ;}}

Write an implementation class in the Dao layer:

Public interface ImessageBorad {// all methods in my interface: public boolean addMessage (String message, String author, Date paseTime) throws Exception; // The pagination collection method public List <Object []> boradList (int pageIndex, int pageSize) throws Exception; // the total number of pages public int messageCount () throws Exception ;}

Write an implementation class in my implementation package:
When using dbutil, add a jar package of dbutil, and connect the database to the jar package. The Jar package uploaded by the file is in the middle:

Reference of my implementation class:

Start to write my class:

public class ImpMessageBorad implements ImessageBorad {      con=bd.getConection();      DbUtils ut=new DbUtil();      QueryRunner  qr=new QueryRunner();      BaseDao bd=new BaseBao();      ArrayListHandler al=new ArrayListHandler();      public boolean addMessage(String boradContent, String author, Date time) throws Exception {        int num=qr.update(con,"insert into message(message,author,pastTime) values (?,?,?)",boradContent,author,DateTime);       if(num>0){          flag=true;      }          ut.closeQuietly(con);  }       public List<Object[]> boradList(int pageIndex,int pageSize) throws Exception{       String sql="select * from message limit ?,?";       Object[] parameters={              (pageIndex)';              (pageSize);      };     return qr.query(con,sql,parameters,al);  }public int messageCount() throws Exception{   string sql="select count(1) as co from message";   int num=0;   ResultSet rs=bd.executeQuery(sql);   if(rs.next()){        num=rs.getInt("co");   }      return num;      }}

Service is to call methods and implementations in my Dao Layer

Therefore, write my servlet to call the methods in my service:

 

// Solve the garbled problem first: ImessageBoradService msg = new MessageBoradServiceImp (); request. setCharacterEncoding ("UTF-8"); response. setCharacterEncoding ("UTF-8"); if ("da ". equest. getParameter ("action") {String boradContent = request. getParameter ("name"); String author = request. getParameter ("message"); Date dt = new Date (); try {if (msg. addMessage (boradContent, author, dt) {request. setAttribute ("success", "added successfully"); request. getRequestDispatcher ("/index. jsp "). forward (request, response);} else {request. setAttribute ("success", "failed to add"); request. getRequestDispatcher ("/index. jsp "). forward (request, response) ;}} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} int pageSize = 2; int pageIndex = 0; if ("ww ". equals (request. getParameter ("ca") {String index = request. getParameter ("pageIndex"); System. out. println (index); if (index. equals ("0") {pageIndex = pageIndex + pageSize;} else {System. out. println (2); int in = Integer. parseInt (index); pageIndex = in + pageSize; System. out. println (3) ;}} if ("ws ". equals (request. getParameter ("ca") {String index = request. getParameter ("pageIndex"); System. out. println (index); if (index. equals ("0") {pageIndex = 0;} else {int in = Integer. parseInt (index); pageIndex = in-pageSize ;}} request. setAttribute ("pageIndex", pageIndex); try {request. setAttribute ("list", msg. boradList (pageIndex, pageSize); request. getRequestDispatcher ("/index. jsp "). forward (request, response);} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}


In my front-end page, form:

<H1> message board: 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.