【Java开源代码栏目提醒】:网学会员为广大网友收集整理了,TopicHandle.java,希望对大家有所帮助!
package bbs;
import java.
sql.*;
import java.util.ArrayList;
public class TopicHandle {
public TopicHandle() {
}
/**
* 寰鏁版嵁搴撴彃鍏ユ柊璐?
* @param bbs Topic 瑕佹彃鍏ョ殑甯栧瓙瀵硅薄
* @throws Exception
*/
public static void insert(Topic bbs) throws Exception {
Connection cnn = null;
PreparedStatement ps = null;
try {
cnn = DBHandle.getConn(); //鑾峰彇杩炴帴鍙ユ焺
cnn.setAutoCommit(false); //璁剧疆浜嬪姟澶勭悊鏍囧織
ps = cnn.prepareStatement( //鍒涘缓璇彞鍙ユ焺
"insert into topic(parentid, forumid, username, title, content, postdate) values(?,?,?,?,?,?)");
//濉厖鍙傛暟
ps.setInt(1, bbs.getParentId());
ps.setInt(2, bbs.getForumId());
ps.setString(3, bbs.getUserName());
ps.setString(4, bbs.getTitle());
ps.setString(5, bbs.getContent());
//甯栧瓙鍙戣〃鏃堕棿涓哄綋鍓嶆椂闂?
ps.setTimestamp(6, new Timestamp(System.currentTimeMillis()));
ps.executeUpdate(); //鎵ц璇彞
if (bbs.getParentId() != 0) { //濡傛灉璇ュ笘瀛愭槸鍥炲笘
DBHandle.closeResource(ps, null, null); //閲婃斁璇彞鍙ユ焺
//灏嗚甯栧瓙鐨勭埗甯栧瓙鐨勫洖澶嶆鏁板鍔犱竴娆?
ps = DBHandle.getConn().prepareStatement( //閲嶆柊浣跨敤鏂扮殑SQL璇彞鐢熸垚璇彞鍙ユ焺
"update topic set replies=replies+1 where id=?");
ps.setInt(1, bbs.getParentId());
ps.executeUpdate();
}
cnn.commit(); //鎻愪氦浜嬪姟
}
catch (Exception es) {
cnn.rollback(); //鍥炴粴浜嬪姟
throw es;
}
finally {
cnn.setAutoCommit(true); //杩樺師浜嬪姟鏍囧織
DBHandle.closeResource(ps, null, cnn); //閲婃斁璇彞鍙ユ焺鍜岃繛鎺ュ彞鏌?
}
}
/**
* 鍒犻櫎ID鎴朠ARENT_ID涓虹粰瀹氬肩殑甯栧瓙
* @param id int
*/
public static void delete(int id) throws Exception {
Connection cnn = null;
PreparedStatement ps = null;
try {
cnn = DBHandle.getConn();
ps = cnn.prepareStatement(
"delete from topic where id=? or parentid=?");
ps.setInt(1, id);
ps.setInt(2, id);
ps.executeUpdate();
}
catch (Exception es) {
throw es;
}
finally {
DBHandle.closeResource(ps, null, cnn);
}
}
public static void update(Topic bbs) throws Exception {
Connection cnn = null;
PreparedStatement ps = null;
try {
cnn = DBHandle.getConn();
ps = cnn.prepareStatement(
"update topic set title=?, content=? where id=?");
ps.setString(1, bbs.getTitle());
ps.setString(2, bbs.getContent());
ps.setInt(3, bbs.getId());
ps.executeUpdate();
}
catch (Exception es) {
throw es;
}
finally {
DBHandle.closeResource(ps, null, cnn);
}
}
public static ArrayList select(int forum_id) throws Exception {
Connection cnn = null;
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList array = new ArrayList();
try {
cnn = DBHandle.getConn();
ps = cnn.prepareStatement(
"select id, username, title, postdate, replies from topic where parentid=0 and forumid=? order by postdate desc");
ps.setInt(1, forum_id);
rs = ps.executeQuery();
while (rs.next()) {
Topic bbs = new Topic();
bbs.setId(rs.getInt(1));
bbs.setUserName(rs.getString(2));
bbs.setTitle(rs.getString(3));
bbs.setPostDate(StringUtil.getTimestamp(rs.getTimestamp(4)));
bbs.setReplies(rs.getInt(5));
array.add(bbs);
}
}
catch (Exception es) {
throw es;
}
finally {
DBHandle.closeResource(ps, rs, cnn);
}
return array;
}
/**
* 鑾峰彇鎸囧畾璁ㄨ鍖虹殑甯栧瓙鍒楄〃
* @param forum_id int 璁ㄨ鍖篿d
* @param page Page 鍒嗛〉瀵硅薄
* @throws Exception
* @return ArrayList 杩斿洖甯栧瓙鍒楄〃
*/
public static ArrayList select(int forum_id, Page page) throws Exception {
Connection cnn = null;
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList array = new ArrayList();
try {
cnn = DBHandle.getConn();
ps = cnn.prepareStatement( //鑾峰彇鎸囧畾璁ㄨ鍖虹殑甯栧瓙鏁伴噺锛屼笉鍖呭惈鍥炲
"select count(*) from topic where parentid=0 and forumid=?");
ps.setInt(1, forum_id);
rs = ps.executeQuery();
if (rs.next()) {
page.setTotal(rs.getInt(1)); //灏嗗笘瀛愭暟閲忎繚瀛樺湪鍒嗛〉瀵硅薄涓?
}
DBHandle.closeResource(ps, rs, null); //鍏抽棴缁撴灉闆嗗拰璇彞鍙ユ焺
ps = cnn.prepareStatement( //鎸夌収鎸囧畾鐨勫垎椤靛尯闂磋幏鍙栧笘瀛愪俊鎭?
"select id, username, title, postdate, replies from topic where parentid=0 and forumid=? order by id desc limit ?,?");
ps.setInt(1, forum_id);
ps.setInt(2, page.getIndex());
ps.setInt(3, page.MAX_ROW);
rs = ps.executeQuery();
while (rs.next()) {
Topic
bbs = new Topic();
bbs.setId(rs.getInt(1));
bbs.setUserName(rs.getString(2));
bbs.setTitle(rs.getS