📄 frienddbaccess.java
字号:
/**
* 友情链接的增.删.改.查
*/
package bbs.db;
import java.sql.*;
import bbs.bean.BbsFriend;
import com.db.Databasec;
import com.db.face.*;
import java.util.ArrayList;
public class FriendDBAccess
extends Databasec {
private java.sql.ResultSet rst;
/**
* 插入链接
* @param friend BbsFriend
* @return int
* @throws Exception
*/
public int insert(BbsFriend friend) throws Exception {
this.open();
try {
String sql = "insert into bbsFriend values('"
+ friend.getFname() + "','"
+ friend.getFaddress() + "')";
pst = con.prepareStatement(sql);
return pst.executeUpdate();
}
finally {
this.close();
}
}
/**
* 删除链接 根据ID号码
* @param id String
* @return int
* @throws Exception
*/
public int delete(String id) throws Exception {
try {
this.open();
String sql = "delete from bbsFriend where FID=" + id;
pst = con.prepareStatement(sql);
return pst.executeUpdate();
}
finally {
this.close();
}
}
/**
* 更新链接
* @param friend BbsFriend
* @return int
* @throws Exception
*/
public int update(BbsFriend friend) throws Exception {
this.open();
try {
String sql = "UPDATE bbsFriend SET FName='"
+ friend.getFname() + "', FAddress='"
+ friend.getFaddress() + "' WHERE FID="
+ friend.getFid();
pst = con.prepareStatement(sql);
return pst.executeUpdate();
}
finally {
this.close();
}
}
/**
* 查询链接的方法:
* @param tabName String
* @return ArrayList
* @throws Exception
*/
public ArrayList select(String tabName) throws Exception {
return null;
}
public ArrayList select() throws Exception {
ArrayList list = new ArrayList();
try {
this.open();
String sql = "select * from bbsFriend";
pst = con.prepareStatement(sql);
rst = pst.executeQuery();
while (rst.next()) {
BbsFriend obj = new BbsFriend();
obj.setFid(rst.getInt("FID"));
obj.setFname(rst.getString("FName"));
obj.setFaddress(rst.getString("FAddress"));
list.add(obj);
}
}
finally {
this.close();
}
return list;
}
private int currentPage = 1;
private int pageCount = 1;
private int rows = 5;
public ArrayList select(int page) throws Exception {
ArrayList list = new ArrayList();
try {
this.open();
String sql = "select count(*) from bbsFriend";
pst = con.prepareStatement(sql);
rst = pst.executeQuery();
rst.next();
int counts = rst.getInt(1);
this.close();
this.pageCount = counts % rows == 0 ? counts / rows : counts / rows + 1;
if (page > pageCount) {
page = pageCount;
}
if (page < 1) {
page = 1;
}
this.open();
int pp = (page - 1) * rows;
sql = "select top " + rows + " * from bbsFriend "
+ "where fid not in("+ "select top " + pp + " fid from bbsFriend order by fid)"
+ " order by fid";
pst = con.prepareStatement(sql);
rst = pst.executeQuery();
while (rst.next()) {
BbsFriend obj = new BbsFriend();
obj.setFid(rst.getInt("FID"));
obj.setFname(rst.getString("FName"));
obj.setFaddress(rst.getString("FAddress"));
list.add(obj);
}
}
finally {
this.close();
}
return list;
}
public int getRows() {
return rows;
}
public int getCurrentPage() {
return currentPage;
}
public int getPageCount() {
return pageCount;
}
public void setRows(int rows) {
this.rows = rows;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -