oracle面试

:在ORACLE大数据量下的分页解决方法。一般用截取ID方法,还有是三层嵌套方法。下面的是网上的答案,不过没有看明白,那讲一下,弄细点,<%

int i=1;

int numPages=14;

String pages = request.getParameter(page) ;

int currentPage = 1;

currentPage=(pages==null)?(1):{Integer.parseInt(pages)}

sql = select count(*) from tables;

ResultSet rs = DBLink.executeQuery(sql) ;

while(rs.next()) i = rs.getInt(1) ;

int intPageCount=1;

intPageCount=(i%numPages==0)?(i/numPages)i/numPages+1);

int nextPage ;

int upPage;

nextPage = currentPage+1;

if (nextPage>=intPageCount) nextPage=intPageCount;

upPage = currentPage-1;

if (upPage<=1) upPage=1;

rs.close();

sql=select * from tables;

rs=DBLink.executeQuery(sql);

i=0;

while((i<numpages*(currentpage-1))&&rs.next()){i++;} <br="">%>

//输出内容

//输出翻页连接

合计:<%=currentPage%>/<%=intPageCount%>第一页href=List.jsp?page=<%=upPage%>>上一页

<%

for(int j=1;j<=intPageCount;j++){

if(currentPage!=j){

%>

>[<%=j%>]

<%

}else{

out.println(j);

}

}

%>

>下一页>最后页





为什么会有

<%

for(int j=1;j<=intPageCount;j++){

if(currentPage!=j){

%>

>[<%=j%>]

<%

}else{

out.println(j);

}

}

%>

这一段呢?他是做什么的呀,那个高手可以说说每一个代码的意思,不光是我问的这个,把上的都解释一下了

<%
int i=1;
int numPages=14;//每页的记录数
String pages = request.getParameter(page) ; // 取出当前页码参数
int currentPage = 1;
currentPage=(pages==null)?(1):{Integer.parseInt(pages)} //转换成数字
sql = select count(*) from tables;
ResultSet rs = DBLink.executeQuery(sql) ;
while(rs.next()) i = rs.getInt(1) ; //连接数据库,查询总记录数
int intPageCount=1;
intPageCount=(i%numPages==0)?(i/numPages)i/numPages+1);//计算出总页数
int nextPage ;
int upPage;
nextPage = currentPage+1;
if (nextPage>=intPageCount) nextPage=intPageCount;//下页的页码
upPage = currentPage-1;
if (upPage<=1) upPage=1;//上页的页码
rs.close();
sql=select * from tables;
rs=DBLink.executeQuery(sql);
i=0;
while((i %>
//输出内容
//输出翻页连接
合计:<%=currentPage%>/<%=intPageCount%>第一页 href=List.jsp?page=>上一页
<%
for(int j=1;j<=intPageCount;j++){
if(currentPage!=j){
%>
>[<%=j%>]//其实page作为一个页码的参数
<%
}else{
out.println(j);
}
}
%>
>下一页>最后页

为什么会有
<%
for(int j=1;j<=intPageCount;j++){
if(currentPage!=j){
%>
>[<%=j%>]
<%
}else{
out.println(j);
}
}
%>
这段存在是因为,这个页码除了显示上一页,下一页,还显示第x页的功能,看看javaeye论坛的分页那个样子。而且当前页是不做连接处理。。。

ok,还有什么问题?

上面的代码前面一段是计算页码、起始位置什么的,然后是一条一条的往下next移动到起始位置的,数据大了会很慢很慢,而且还混合了jsp页面上的代码,晕呀,纯粹误导读者。
[code="java"]
/**
这个比较简单,是Hibernate源码,可以吧一个普通sql通过ROW_NUM这个隐藏列转换为分页SQL,应该是最好的解决方案了。页面上可以自己搞,这个问题似乎和页面无关。
@param sql 原来的sql
@param hasOffset 如果为false,表示从第一条记录开始(第一页)
*/
public String getLimitString(String sql, boolean hasOffset) {

    sql = sql.trim();

//把锁去掉
boolean isForUpdate = false;
if ( sql.toLowerCase().endsWith(" for update") ) {
sql = sql.substring( 0, sql.length()-11 );
isForUpdate = true;
}

    StringBuffer pagingSelect = new StringBuffer( sql.length()+100 );

//开始折腾SQL了,是否有offset是不同的,看不明白可以去掉else里面的代码,只保留if里面的代码(连if也去掉,只要里面的)
if (hasOffset) {
pagingSelect.append("select * from ( select row_.*, rownum rownum_ from ( ");
}
else {
pagingSelect.append("select * from ( ");
}
pagingSelect.append(sql);
if (hasOffset) {//第一个?是起始行,第二个是终止行
pagingSelect.append(" ) row_ ) where rownum_ <= ? and rownum_ > ?");
}
else {
pagingSelect.append(" ) where rownum <= ?");
}
//折腾完了 再加上锁(如果以前有锁)
if ( isForUpdate ) {
pagingSelect.append( " for update" );
}

    return pagingSelect.toString();
}

[/code]

同意,纯粹误导读者。。。。

那段jsp代码很恶心。。。。 :lol:

忘了说,前面这段代码返值就是分页SQL了。
你给出的那些网上的代码把java和jsp混在一起,而且用next分页,实在是...无语了,千万别看了,我也不详细解释它了。反正根据你的题目,给出了最好的了。

<%
for(int j=1;j<=intPageCount;j++){
if(currentPage!=j){
%>
>[<%=j%>]
<%
}else{
out.println(j);
}
}
%>

就是把每一个页码都建一个连接到后台去查询该页内容。

就像 [u]1[/u] [u]2[/u] [u]3[/u] [u]4[/u] [u]5[/u]。。这样

至于页面上的,你可以用ecside、extrememComponent、displayTag、ext grid之类的表格组件,页面会很干净,springside1.0m3的文档中也介绍过一个分页标签,不错的。总之不要在页面嵌入<%%>,象我等老土偶尔也用用<%%>,但那是懒+土的结果。