package oldgoods;
import java.sql.*;
//该类只针对Oracl
public class CentPage {
/**
* 分页方法
* @param fieldsName:要显示的字段-----------注意不能为*
* @param tableNames:表名
* @param fromPage:当前面
* @param toPage:下一页
* @param pageSize:一页显示的条数
* @param orderString:排序的方式----------------不可不写
* @param conn:连接
* @return PrepareStatement
* */
public PreparedStatement pageCount(String fieldsName, String tableNames, int fromPage,
int toPage,int pageSize, String orderString, Connection conn) {
StringBuffer sb = new StringBuffer();
sb.append("Select ");
sb.append(fieldsName);
sb.append(" from (");
sb.append("Select ");
sb.append(fieldsName);
sb.append(", rownum as myrow From (");
sb.append("select ");
sb.append(fieldsName);
sb.append(", rownum ");
sb.append(" from ");
sb.append(tableNames);
sb.append(" order by rownum ");
sb.append(orderString);
sb.append(") t Where rownum <= ");
sb.append(pageSize*toPage);
sb.append(") Where myrow > ");
sb.append(pageSize*fromPage);
sb.append(" order by myrow desc");
try {
System.out.println(sb.toString());
return conn.prepareStatement(sb.toString(),ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
} catch (SQLException ex) {
ex.printStackTrace();
return null;
}
}
/**
* 重载pageCount
* 区别:追加了条件
* @param strCondition:条件
*/
public PreparedStatement pageCount(String fieldsName, String tableNames, int fromPage,
int toPage,int pageSize, String orderString, Connection conn,String strCondition) {
StringBuffer sb = new StringBuffer();
sb.append("Select ");
sb.append(fieldsName);
sb.append(" from (");
sb.append("Select ");
sb.append(fieldsName);
sb.append(", rownum as myrow From (");
sb.append("select ");
sb.append(fieldsName);
sb.append(", rownum ");
sb.append(" from ");
sb.append(tableNames);
sb.append(strCondition);
sb.append(" order by rownum ");
sb.append(orderString);
sb.append(") t Where rownum <= ");
sb.append(pageSize*toPage);
sb.append(") Where myrow > ");
sb.append(pageSize*fromPage);
sb.append(" order by myrow desc");
try {
System.out.println(sb.toString());
return conn.prepareStatement(sb.toString(),ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
} catch (SQLException ex) {
ex.printStackTrace();
return null;
}
}
}