📄 pagedao.java
字号:
package page;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
/**
* <p>Title: 分页标签</p>
*
* <p>Description: 数据访问类</p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: ACCP</p>
*
* @author HuWenJun
* @version 1.0
*/
public class PageDAO {
private Connection con = null;
private Statement stmt = null;
private ResultSet rs = null;
private String sql = "";
private static PageDAO me = new PageDAO();
private PageDAO() {
}
public static PageDAO newInstance() {
return me;
}
/**
* 获得指定页要显示的数据
* @param tableName 表名
* @param column 列名
* @param primaryKey 主键字段
* @param rowsPerPage 每页显示多少行
* @param curPage 当前是第几页
* @param columnCount 列数
* @return Vector 数据
*/
public Vector getData(String tableName, String columnNames,
String primaryKey,
int rowsPerPage, int curPage) {
Vector data = new Vector();
try {
DataBaseConn cBean = DataBaseConn.newInstance();
con = cBean.getConnDB();
stmt = con.createStatement();
sql = "select top " + rowsPerPage + " * from " +
tableName + " where " + primaryKey +
" not in (select top " + (curPage - 1) * rowsPerPage +
" " + primaryKey + " from " + tableName +
" order by " + primaryKey + " desc) order by " +
primaryKey + " desc";
rs = stmt.executeQuery(sql);
String[] columns = columnNames.split(",");
while (rs.next()) {
//根据列数动态创建数组
Object[] obj = new Object[columns.length];
for (int j = 0; j < columns.length; j++) {
//从结果集中取相应字段的值存入数组
obj[j] = rs.getString(columns[j]);
}
data.add(obj);
}
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
return data;
}
/**
* 获得总记录数
* @param tableName 表名
* @return int 记录数
*/
public int getAvailableCount(String tableName) {
int count = 0;
try {
DataBaseConn cBean = DataBaseConn.newInstance();
con = cBean.getConnDB();
stmt = con.createStatement();
String strSql = "select count(*) as maxRow from " + tableName;
rs = stmt.executeQuery(strSql);
if (rs.next()) {
count = rs.getInt("maxRow");
}
rs.close();
stmt.close();
con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return count;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -