📄 replaydb.java
字号:
/**
* 数据库类:负责回贴表的增、删、改、查
*/
package bbs.db;
import java.sql.*;
import java.util.*;
import bbs.bean.*;
import com.db.*;
public class ReplayDB extends Databasec {
public ArrayList select(String tabName) throws Exception {
return null;
}
private ResultSet res;
private String sql = null;
private int currentPage = 1;
private int pageCount = 1;
private int rows = 10;
/**
* 向回贴表中插入数据
* @param obj BbsReply
* @param username String
* @return int
* @throws Exception
*/
public int insert(BbsReply obj,String username) throws Exception{
try{
this.open();
sql = "select UID from bbsUsers where UName='"+username+"'";
pst = con.prepareStatement(sql);
res = pst.executeQuery();
res.next();
int uid=res.getInt(1);
sql = "insert into bbsReply (RtID,RuID,Rcontents,Rtime,RIP) values(?,?,?,?,?)";
pst = con.prepareStatement(sql);
pst.setInt(1,obj.getRtid());
pst.setInt(2,uid);
pst.setString(3,obj.getRcontents());
pst.setString(4,obj.getRtime());
pst.setString(5,obj.getRip());
return pst.executeUpdate();
}
finally{
this.close();
}
}
/**
* 根据帖子编号查出该贴的所有回贴
* @param tid int
* @param page int
* @return ArrayList
* @throws Exception
*/
public ArrayList select(int tid,int page) throws Exception{
ArrayList list = new ArrayList();
try{
DBAccess db = new DBAccess();
this.open();
sql = "select count(*) from bbsReply";
pst = con.prepareStatement(sql);
res = pst.executeQuery();
res.next();
int counts = res.getInt(1);
this.pageCount = counts % rows == 0 ? counts / rows : counts / rows + 1;
if (page > pageCount) {
page = pageCount;
}
if (page < 1) {
page = 1;
}
int pp = (page - 1) * rows;
sql = "select top "+rows+" * from bbsReply where RID not in(select top "+pp+" RID from bbsReply order by RID) and RtID="+tid+" order by RID";
pst = con.prepareStatement(sql);
res = pst.executeQuery();
while(res.next()){
BbsReply obj = new BbsReply();
obj.setRid(res.getInt("RID"));
obj.setRtid(res.getInt("RtID"));
obj.setUser(db.selectUser(res.getInt("RuID")));
obj.setRface(res.getInt("Rface"));
obj.setRcontents(res.getString("Rcontents"));
obj.setRtime(res.getString("Rtime"));
obj.setRip(res.getString("RIP"));
list.add(obj);
}
}
finally{
this.close();
}
return list;
}
public int getRows() {
return rows;
}
public int getPageCount() {
return pageCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setRows(int rows) {
this.rows = rows;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -