这几天没事在家看了东西自己就学找写了个很简单的BBS,以前还没有这样认真写过想这样的东西,感觉初步做完的还不错,也很想自己能把后的一点点功能加上去......
article.jsp 页面代码:
<%@page pageEncoding="GB18030"%>
<%@page import="java.sql.*, java.util.*, com.mybbs.*" %>
<%!
private void tree(List<Article> articles, Connection conn, int id, int grade){
String sql = "select * from article where pid =" +id;
Statement stmt = BBSDB.createStmt(conn);
ResultSet rs = BBSDB.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("title"));
a.setIsleaf(rs.getInt("isleaf")==0 ? true:false);
a.setPdate(rs.getTimestamp("pdate"));
a.setGrade(grade);
articles.add(a);
if(!a.isIsleaf()){
tree(articles, conn, a.getId(), grade+1);
}
}
}catch(SQLException e){
e.printStackTrace();
}finally {
BBSDB.close(rs);
BBSDB.close(stmt);
//BBSDB.close(conn);
}
}
%>
<%
List<Article> articles = new ArrayList<Article>();
Connection conn = BBSDB.getConn();
tree(articles, conn, 0, 0);
BBSDB.close(conn);
%>
........
<%
for(Iterator<Article> it = articles.iterator(); it.hasNext(); ){
Article a = it.next();
String perStr = "";
for(int i=0;i<a.getGrade();i++){
perStr +="----";
}
%>
......
<%
}
%>
.......
还有写到一个BBSDB.java 文件,是用来做数据处理的。
package com.mybbs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class BBSDB {
public static Connection getConn(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs", "root", "root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static Statement createStmt(Connection conn){
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
public static ResultSet executeQuery(Statement stmt, String sql){
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public static void close(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
public static void close(Statement stmt){
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
}
public static void close(ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
}
}
Article.java 代码:
package com.mybbs;
import java.util.Date;
public class Article {
private int id;
private int pid;
private int rootid;
private String title;
private String cont;
private Date pdate;
private boolean isleaf;
private int grade;
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public int getRootid() {
return rootid;
}
public void setRootid(int rootid) {
this.rootid = rootid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCont() {
return cont;
}
public void setCont(String cont) {
this.cont = cont;
}
public Date getPdate() {
return pdate;
}
public void setPdate(Date pdate) {
this.pdate = pdate;
}
public boolean isIsleaf() {
return isleaf;
}
public void setIsleaf(boolean isleaf) {
this.isleaf = isleaf;
}
}
BBSSQL.sql
create table `bbs`.`article`(
`id` int not null auto_increment,
`pid` int,
`rootid` int,
`title` varchar(255),
`cont` text,
`pdate` datetime,
`isleaf` int,
primary key (`id`)
);
create unique index `PRIMARY` on `bbs`.`article`(`id`);
=====
只是一个很简单的BBS,还有几个页面的功能 没有写完成。明天继续......