首页 > 代码库 > 欣欣的留言板项目====超级触动的dbUtil实现留言板

欣欣的留言板项目====超级触动的dbUtil实现留言板

留言板管理系统

我的完成效果图:

技术分享

提交后:

技术分享

   我的留言板基本架构如图:

技术分享

创建留言板数据库:

技术分享

刚开始我的前台主页中写留言信息表单:

<body>
<h1>留言板</h1>
<form action="提交后的页面地址"  method="post" >
      留言者:<input type="text" name="author" />
      留言的内容:<input type="text" name="content" rows="100 "cols="120"/>
    <input type="submit" value=http://www.mamicode.com/"提交信息"/>
</form>
</body>

开始建立后台Java类中层次
我先在我的实体层里写了一个实体类实现增加的实体类如图:

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;
    }

}

写我Dao层里的BaseDao工具类

public class BaseDao{

     public static final String driver="com.mysql.jdbc.Driver";
     public static final String url="jdbc:mysql://localhost:3306/数据库名字";
     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();
     }
    //执行增删改
    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.executeUpdate(); 
          return count;
     }
  //获取结果集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]);
      }
          rs=ps.executeQuery();
          return rs;
     }
    public int delete(int id) throws Exception{
         return 0;
     }
}

在Dao层里写一个实现类:
  

public interface ImessageBorad{
//所有我接口里的方法:
     public boolean addMessage(String message,String author,Date paseTime) throws Exception;
//分页的集合方法
     public List<Object[]> boradList(int pageIndex,int pageSize) throws Exception;
//分页的总数
    public int messageCount() throws Exception;
}

在我的实现包里写一个实现类:
用我的dbutil的时候,加入一个dbutil的jar包,链接数据库也需要jar包,中间的是文件上传的Jar包:

技术分享

我的实现类的引用:

技术分享

开始写我的类:

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 就是调用我的Dao层里的方法和实现

所以直接写我的servlet调用我的service里的方法:

 

//先解决乱码问题:
 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", "添加成功");
           request.getRequestDispatcher("/index.jsp").forward(request, response);
         }else {
            request.setAttribute("success", "添加失败");
            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();
        }
    }

  } 


在我的前台页面中form表单:

<h1>留言板:</h1>
    <br>
    <%
        List<Object[]> list=(List<Object[]>)request.getAttribute("list");
        
        for(Object[] object:list){
            
            %>
            <p style="backcolor:red">作者:<%=object[2] %>  <%=object[0] %>楼</p>
                 <lable>内容</lable>
                 <label><%=object[1] %></label>
                 <p> <%=object[3] %></p>
            <%
            }
            
    %>
    
    <br>
    <a href="http://www.mamicode.com/MessageServlet?ca=ww&&pageIndex=${pageIndex}">下一页</a>
    <a href="http://www.mamicode.com/MessageServlet?ca=ws&&pageIndex=${pageIndex}">上一页</a>
  
<form action="MessageServlet?action=da" method="post">
 
        请输入你的姓名:<br><input type="text" name="name"><br>

        留言内容:<br><textarea rows="15" cols="20" name="message"></textarea><br><br>       
        <input type="submit" value="http://www.mamicode.com/提交信息"><br>
    </form>
     <label style="color:red">${success }</label>
 

                                                                                                                                                      我还在这里  奋斗着   你在哪里  在奋斗吗  地狱的镰刀

 

 

 

 

 

 

 

 

欣欣的留言板项目====超级触动的dbUtil实现留言板