且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jsp开发教程之 仿MOP论坛 三(帖子列表-上)

更新时间:2022-09-27 09:44:28

 
习惯用eclipse来开发,先新建立一个web项目 取名为axbbs
jsp开发教程之 仿MOP论坛 三(帖子列表-上)
准备两个jar包,一个是mysql for java 的驱动程序,一个是jspsmartupload的上传组件
复制到项目中 WebRoot\WEB-INF\lib目录下
先在src目录下新建一个 com.axbbs.common工具包,再新建一个数据库工具类,用来创建数据库连接.
DbUtil.java 设计如下
 
package com.axbbs.common; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
/** 
* @程序编写 阿汐 
* @版本 1.0 
* @说明 仿wc的jsp版本小论坛 
* @本模块使用于数据库连接公用类 
* @并且功能并不需要太多,因此本版本没有采用传统意义上的MVC开发模式,而是针对 
* @vo,bo类进行了精简,用po类来共用vo类,用servlet类来共用bo类,很奇怪的开发模式 
* @日期 2008.09.01 

*/
 
public class DbUtil { 

    //数据库连接 
    public static Connection getConnection() { 

  try { 
      //数据库驱动驱动 
      Class.forName("org.gjt.mm.mysql.Driver").newInstance(); 

      //创建连接 
      String url = "jdbc:mysql://localhost/axbbs?user=root&password=root"; 

      //设置连接 
      Connection con = DriverManager.getConnection(url); 

      return con; 

  } catch (Exception e) { 
      System.out.println("数据库连接失败!"); 
      e.printStackTrace(); 
      return null
  } 
    } 

    // 执行sql语句 
    public int execSQL(String sql) { 
  Connection con = getConnection(); 
  Statement st = null
  int n = 0; 
  try { 
      st = con.createStatement(); 
  } catch (SQLException e) { 
      System.out.println("error!"); 
      e.printStackTrace(); 
  } 
  try { 
      n = st.executeUpdate(sql); 
  } catch (SQLException e) { 
      System.out.println("error!"); 
      e.printStackTrace(); 
  } finally { 
      if (st != null
    try { 
        st.close(); 
    } catch (SQLException e) { 
        System.out.println("error!"); 
        e.printStackTrace(); 
    } 
      if (con != null
    try { 
        con.close(); 
    } catch (SQLException e) { 
        System.out.println("error!"); 
        e.printStackTrace(); 
    } 
  } 

  return n; 
    } 

    public ResultSet getResultSet(String sql) { 
  Connection con = getConnection(); 
  Statement st = null
  ResultSet rs = null
  try { 
      st = con.createStatement(); 
  } catch (SQLException e) { 
      System.out.println("数据库异常"); 
      e.printStackTrace(); 
  } 
  try { 
      rs = st.executeQuery(sql); 
  } catch (SQLException e) { 
      System.out.println("数据库异常"); 
      e.printStackTrace(); 
  } 

  return rs; 
    } 

    public void closeResultSet(ResultSet rs) { 
  try { 
      if (rs != null) { 
    Statement st = rs.getStatement(); 
    Connection con = st.getConnection(); 
    if (rs != null) { 
        rs.close(); 
    } 
    if (st != null) { 
        st.close(); 
    } 
    if (con != null) { 
        con.close(); 
    } 
      } 

  } catch (Exception e) { 
      System.out.println("数据库连接失败"); 
      e.printStackTrace(); 
  } 
    } 

然后在src目录新建一个包 com.axbbs.Dao :) 习惯从dao层设计 (Data Access Object 数据访问对象)
在com.axbbs.Dao 目录下新建一个Dao类 GetPostListDAO.java
这个类其实做了两件事情,一是得到帖子的列表,二是得到主贴的信息(偷懒)

package com.axbbs.Dao; 

import java.sql.Connection; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.ArrayList; 

import com.axbbs.common.DbUtil; 
import com.axbbs.po.PostPo; 


/** 
* @程序编写 阿汐 
* @版本 1.0 
* @说明 仿wc的jsp版本小论坛 
* @本模块是用于获取帖子列表信息和帖子内容信息的DAO类 
  * @并且功能并不需要太多,因此本版本没有采用传统意义上的MVC开发模式,而是针对 
* @vo,bo类进行了精简,用po类来共用vo类,用servlet类来共用bo类,很奇怪的开发模式 
* @日期 2008.09.01 

*/
 
public class GetPostListDAO { 

    /** 
  * @param args 
  */
 
    //创建数据工具包对象 
    private DbUtil dbUtil = new DbUtil(); 

    //创建数据库连接 
  
    private Connection conn = dbUtil.getConnection(); 
    public static void main(String[] args) { 
  //主方法测试方 
  //ArrayList l = new GetPostListDAO().getPost("1"); 
  //System.out.print(((PostPo) l.get(0)).getPostUserName()); 
    } 
    //帖子帖子列表信息 
    public ArrayList ViewList() { 
  ArrayList viewList = new ArrayList(); 
  Statement stmt = null
  ResultSet rs = null
  String sql = "select * from MainPost"
  try { 
      stmt = conn.createStatement(); 
      rs = stmt.executeQuery(sql); 
      while (rs.next()) { 
    PostPo pp = new PostPo(); 
    pp.setPostId(rs.getInt("id")); 
    pp.setPostTitle(rs.getString("Post_Title")); 
    pp.setPostContent(rs.getString("Post_Content")); 
    pp.setPostUserName(rs.getString("Post_UserName")); 
    pp.setPosttime(rs.getString("Post_Time")); 
    pp.setPostReply(rs.getInt("Post_Reply")); 
    pp.setPostHits(rs.getInt("Post_Hits")); 
    viewList.add(pp); 
      } 
  } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
  } 

   
  return viewList; 
   
    } 
    //获得帖子信息 
    public ArrayList getPost(String id) { 
  ArrayList viewList = new ArrayList(); 
  Statement stmt = null
  ResultSet rs = null
  String sql = "select * from MainPost where id='"+id+"'"
  try { 
      stmt = conn.createStatement(); 
      //执行查询获得主贴信息 
      rs = stmt.executeQuery(sql); 
      while (rs.next()) { 
    PostPo pp = new PostPo(); 
    pp.setPostId(rs.getInt("id")); 
    pp.setPostTitle(rs.getString("Post_Title")); 
    pp.setPostContent(rs.getString("Post_Content")); 
    pp.setPostUserName(rs.getString("Post_UserName")); 
    pp.setPosttime(rs.getString("Post_Time")); 
    pp.setPostReply(rs.getInt("Post_Reply")); 
    pp.setPostHits(rs.getInt("Post_Hits")); 
    viewList.add(pp); 
      } 
  } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
  } 

  return viewList; 
   
    } 
  
    public void closeResultSet(ResultSet rs) { 
  try { 
      if (rs != null) { 
    Statement st = rs.getStatement(); 
    Connection con = st.getConnection(); 
    if (rs != null) { 
        rs.close(); 
    } 
    if (st != null) { 
        st.close(); 
    } 
    if (con != null) { 
        con.close(); 
    } 
      } 

  } catch (Exception e) { 
      System.out.println("数据库连接关闭异常!"); 
      e.printStackTrace(); 
  } 
    } 


 
 
这个类用到了一个Po对象,所以同时需要建立一个Po对象类.
com.axbbs.po 下建立PostPo.java
 
package com.axbbs.po; 
/** 
* @程序编写 阿汐 
* @版本 1.0 
* @说明 仿wc的jsp版本小论坛 
* @本模块是帖子信息的PO类,帖子列表信息同样用此类,另外省去了VO类,为了精简 
* @并且功能并不需要太多,因此本版本没有采用传统意义上的MVC开发模式,而是针对 
* @vo,bo类进行了精简,用po类来共用vo类,用servlet类来共用bo类,很奇怪的开发模式 
* @日期 2008.09.01 

*/
 
public class PostPo { 
    //id 
    private int postId; 
    //帖子标题 
    private String postTitle; 
    //帖子内容 
    private String postContent; 
    //发帖人 
    private String postUserName; 
    //发帖时间 
    private String posttime; 
    //回复 
    private int postReply; 
    //点击 
    private int postHits; 
    /** 
  * @return postContent 
  */
 
    public String getPostContent() { 
  return postContent; 
    } 
    /** 
  * @param postContent 要设置的 postContent 
  */
 
    public void setPostContent(String postContent) { 
  this.postContent = postContent; 
    } 
    /** 
  * @return postHits 
  */
 
    public int getPostHits() { 
  return postHits; 
    } 
    /** 
  * @param postHits 要设置的 postHits 
  */
 
    public void setPostHits(int postHits) { 
  this.postHits = postHits; 
    } 
    /** 
  * @return postId 
  */
 
    public int getPostId() { 
  return postId; 
    } 
    /** 
  * @param postId 要设置的 postId 
  */
 
    public void setPostId(int postId) { 
  this.postId = postId; 
    } 
    /** 
  * @return postReply 
  */
 
    public int getPostReply() { 
  return postReply; 
    } 
    /** 
  * @param postReply 要设置的 postReply 
  */
 
    public void setPostReply(int postReply) { 
  this.postReply = postReply; 
    } 
    /** 
  * @return posttime 
  */
 
    public String getPosttime() { 
  return posttime; 
    } 
    /** 
  * @param posttime 要设置的 posttime 
  */
 
    public void setPosttime(String posttime) { 
  this.posttime = posttime; 
    } 
    /** 
  * @return postTitle 
  */
 
    public String getPostTitle() { 
  return postTitle; 
    } 
    /** 
  * @param postTitle 要设置的 postTitle 
  */
 
    public void setPostTitle(String postTitle) { 
  this.postTitle = postTitle; 
    } 
    /** 
  * @return postUserName 
  */
 
    public String getPostUserName() { 
  return postUserName; 
    } 
    /** 
  * @param postUserName 要设置的 postUserName 
  */
 
    public void setPostUserName(String postUserName) { 
  this.postUserName = postUserName; 
    } 
  

 
好了,关于帖子列表的DAO和PO都做完了,下一章我们开始进行在页面得到帖子列表信息的设计.



本文转自阿汐 51CTO博客,原文链接:http://blog.51cto.com/axiii/97506,如需转载请自行联系原作者