JavaWeb项目—BBS论坛
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaWeb项目—BBS论坛相关的知识,希望对你有一定的参考价值。
项目步骤:
1)将静态html文件转换为JSP文件(涉及到了编码的问题)
2)封装DB
1 package com.bjsxt.bbs; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 //封装DB,方便操作 10 public class DB { 11 public static Connection getConn() { 12 Connection conn = null; 13 try { 14 Class.forName("com.mysql.jdbc.Driver"); 15 conn = DriverManager.getConnection( 16 "jdbc:mysql://localhost:3306/bbs", "root", "123456"); 17 } catch (ClassNotFoundException e) { 18 e.printStackTrace(); 19 } catch (SQLException e) { 20 e.printStackTrace(); 21 } 22 return conn; 23 } 24 25 public static Statement createStmt(Connection conn) { 26 Statement stmt = null; 27 try { 28 stmt = conn.createStatement(); 29 } catch (SQLException e) { 30 e.printStackTrace(); 31 } 32 return stmt; 33 } 34 35 public static ResultSet executeQuery(Statement stmt, String sql) { 36 ResultSet rs = null; 37 try { 38 rs = stmt.executeQuery(sql); 39 } catch (SQLException e) { 40 e.printStackTrace(); 41 } 42 return rs; 43 } 44 45 public static void close(Connection conn) { 46 if (conn != null) { 47 try { 48 conn.close(); 49 } catch (SQLException e) { 50 e.printStackTrace(); 51 } 52 conn = null; 53 } 54 } 55 56 public static void close(Statement stmt) { 57 if (stmt != null) { 58 try { 59 stmt.close(); 60 } catch (SQLException e) { 61 e.printStackTrace(); 62 } 63 stmt = null; 64 } 65 } 66 67 public static void close(ResultSet rs) { 68 if (rs != null) { 69 try { 70 rs.close(); 71 } catch (SQLException e) { 72 e.printStackTrace(); 73 } 74 rs = null; 75 } 76 } 77 }
3)树状结构展现(一)
主要代码:
Article.java
1 package com.bjsxt.bbs; 2 3 import java.util.Date; 4 5 public class Article { 6 private int id; 7 private int pid; 8 private int rootId; 9 private String title; 10 private String cont; 11 private Date pDate; 12 private boolean isLeaf; 13 private int grade; 14 15 public int getGrade() { 16 return grade; 17 } 18 19 public void setGrade(int grade) { 20 this.grade = grade; 21 } 22 23 public int getId() { 24 return id; 25 } 26 27 public void setId(int id) { 28 this.id = id; 29 } 30 31 public int getPid() { 32 return pid; 33 } 34 35 public void setPid(int pid) { 36 this.pid = pid; 37 } 38 39 public int getRootId() { 40 return rootId; 41 } 42 43 public void setRootId(int rootId) { 44 this.rootId = rootId; 45 } 46 47 public String getTitle() { 48 return title; 49 } 50 51 public void setTitle(String title) { 52 this.title = title; 53 } 54 55 public String getCont() { 56 return cont; 57 } 58 59 public void setCont(String cont) { 60 this.cont = cont; 61 } 62 63 public Date getpDate() { 64 return pDate; 65 } 66 67 public void setpDate(Date pDate) { 68 this.pDate = pDate; 69 } 70 71 public boolean isLeaf() { 72 return isLeaf; 73 } 74 75 public void setLeaf(boolean isLeaf) { 76 this.isLeaf = isLeaf; 77 } 78 }
article.jsp文件
<%! private void tree(Set<Article> articles,Connection conn,int id,int grade){ String sql="select * from article where pid="+id; Statement stmt=DB.createStmt(conn); ResultSet rs=DB.executeQuery(stmt, sql); try{ while(rs.next()){ Article a=new Article(); a.setId(rs.getInt("id")); a.setPid(rs.getInt("pid")); a.setRootId(rs.getInt("rootid")); a.setTitle(rs.getString("titel")); a.setLeaf(rs.getInt("isleaf")==0?true:false); a.setpDate(rs.getTimestamp("pdate")); a.setGrade(grade+1); articles.add(a); if(!a.isLeaf()){ tree(articles, conn, id, grade+1); } }catch (SQLException e){ e.printStackTrace(); } } %> <% Set<Article> articles=new HashSet<Article>(); Connection conn=DB.getConn(); tree(articles,conn,0,0); DB.close(conn); %>
未完
以上是关于JavaWeb项目—BBS论坛的主要内容,如果未能解决你的问题,请参考以下文章