数据库

位置:IT落伍者 >> 数据库 >> 浏览文章

jsp+oracle分页实现


发布日期:2021年05月10日
 
jsp+oracle分页实现

今天做了个基于jsp+oracle分页的实现对于初学者来说这是好的(看了后绝对可以自己实现动手试试把)但是对于有基础的只是温故下sql语句(没涉及到很好的分层)好了我们开始把它实现把

首先建立一个web项目(如图)

导入oracle驱动包到lib目录下开编写数据库连接类DBMamager

package compageutil;import javasqlConnection;import javasqlDriverManager;import javasqlPreparedStatement;import javasqlResultSet;import javasqlSQLException;public class DBManager {

private static Connection connection = null;

static

{

try {

ClassforName(oraclejdbcdriverOracleDriver

connection = DriverManagergetConnection(jdbc:oracle:thin:@::orcl************//自己oracle数据库的帐号密码

} catch (ClassNotFoundException e) {

// TODO Autogenerated catch block

eprintStackTrace()

} catch (SQLException e) {

// TODO Autogenerated catch block

eprintStackTrace()

}

}

protected static Connection getConnection()

{

return connection;

}

public int update(String sql)

{

//boolean flag = false;

int row = ;

Connection connection = DBManagergetConnection()

PreparedStatement statement = null;

try

{

statement = connectionprepareStatement(sql)

row= statementexecuteUpdate()

//

Systemoutprintln(sql)

}

catch (SQLException e)

{

eprintStackTrace()

}

return row;

}

public ResultSet find(String sql)

{

Connection connection = getConnection()

ResultSet result = null;

PreparedStatement statement = null;

try

{

Systemoutprintln(sql)

statement = connectionprepareStatement(sql)

result = statementexecuteQuery()

} catch (SQLException e)

{

eprintStackTrace()

}

return result;

}}

具体实现分页的代码如下(先看代码后面有注释别太心急慢慢看)

<%@page import=comsuncryptoproviderRSACipher%><%@ page language=java import=javautil* pageEncoding=utf%><%@ page import=compageutil*%><%@ page import=javasql* %><%String path = requestgetContextPath()String basePath = requestgetScheme()+://+requestgetServerName()+:+requestgetServerPort()+path+/;%><!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN><html> <head>

<base ;%=basePath%>>

<title>用户信息列表</title>

<meta httpequiv=pragma content=nocache>

<meta httpequiv=cachecontrol content=nocache>

<meta httpequiv=expires content=>

<meta httpequiv=keywords content=keywordkeywordkeyword>

<meta httpequiv=description content=This is my page>

<!

<link rel=stylesheet type=text/css >

> </head>

<body>

<table align=center width=px border= cellspacing= cellpadding= >

<tr align=center bgcolor=#E height=px>

<th>编号</th>

<th>用户帐号</th>

<th>用户姓名</th>

<th>用户密码</th>

<th>用户信息</th>

</tr>

<%

int i;

int page_size=; //分页单位

int all_pages; //总页数

int pages; //接受的页码变量

int cur_page=; //当前页

int start_page; //本页记录开始

int count_row; //总记录数

int end_page;//本页记录结束

String sql_row=select count(id) as count_row from page;

DBManager dbManager=new DBManager()

ResultSet count_rs=dbManagerfind(sql_row)

count_rsnext()

count_row=count_rsgetInt(count_row

all_pages=(int)Mathceil((count_row+page_size)/page_size)//计算总页数

//判断参数pages是否为空

if(requestgetParameter(pages)==null){

pages=;

}else{

pages= new Integer(requestgetParameter(pages))intValue()

}

//判断当前页

if(pages > all_pages || pages == ){

cur_page = ;

} else {

cur_page = pages;

}

start_page=(cur_page)*page_size; //本页开始的记录编号数(数据库中的第几条数据)

end_page=start_page+page_size;//本页显示的最后一条编号数

String sql=select * from(select rownum rnp* from(select * from page )p where rownum<= +end_page+)where rn>+start_page+;

ResultSet rsSet=dbManagerfind(sql)

int t_row=;

String color=#FFFFFF;

while(rsSetnext()){

if(t_row%==){

//让表格更加好看双数行数时显示不同颜色

color=#EDFFC;

}else{

color=#FFFFFF;

}

%>

<tr bgcolor=<%=color %》

<td><%=rsSetgetString() %></td>

<td><%=rsSetgetString() %></td>

<td><%=rsSetgetString() %></td>

<td><%=rsSetgetString() %></td>

<td><%=rsSetgetString() %></td>

</tr>

<%

t_row++;

}

%>

<tr>

<td colspan= align=right>

<%if(cur_page>){%>//不在第一页时显示上一页

<a t;%=cur_page%>>上一页</a>

<%

}

if(cur_page<all_pages){//不在最后一行时显示下一页

%>

<a t;%=cur_page+%>>下一页</a>

<a t;%=all_pages%>>末页</a>//显示最后一页

<%

}

%>

<% for (i=;i<=all_pages;i++) {%>// 循环显示每一页本页时不显示超链接(没有下划线)

<% if (i != pages) {%>

<a t;%= i %>><%= i %></a>

<% } else{%>

<%=i %>

<%} %>

<%}%>

共<%=all_pages %>页&nbsp;

</td>

</tr>

</table> </body></html>

好了分页已经完成了部署好tomcat运行网站吧!(如图)

第二页

第三页

第四页

注意

总页数的求取是all_pages=(int)Mathceil((count_row+page_size)/page_size)//计算总页数

sql语句是String sql=select * from(select rownum rnp* from(select * from page )p where rownum<= +end_page+)where rn>+start_page+;

例如select *

from

select rownum rnp*

from

(select *

from page order by id

)p where rownum<=

)where rn>;//要用伪列!!

最后附上我的sql代码

create table page(

id varchar) not null

username varchar) not null

password varchar) not null

info varchar) default 大家好很高兴认识你们!

constraints pk_id primary key(id))select * from page;delete page;drop table page;insert into page (idusernamepassword) values(黄凯insert into page (idusernamepasswordinfo) values(肖旺我是JJ林俊杰!insert into page (idusernamepassword) values(申俊杰qqqqinsert into page (idusernamepasswordinfo) values(杨小宇我班长!insert into page (idusernamepassword) values(许世群xxxxxxinsert into page (idusernamepasswordinfo) values(王东宝我宝爷!insert into page (idusernamepasswordinfo) values(adminadmin我管理员!insert into page (idusernamepasswordinfo) values(刘鹏我爱游戏!insert into page (idusernamepasswordinfo) values(刘永军liu我少夜哈哈!update page set info=我是少爷哈哈!! where id=;select rownump* from page p where rownum between and ;select count(id) as a from page;select count(id) as count_row from page;select *

from

select rownum rnp*

from

(select *

from page order by id

)p where rownum<=

)where rn>;select *

from

select rownum rnp*

from

(select *

from page )p where rownum<=

)where rn>

在这里我们的任务完成了在如果有什么问题可以联系我QQ:(一起交流)

同时我也希望其他人能提供给我些分层的意见

同时这里没有关闭数据库的连接在自己的测试中出现过一个打开游标数超过最大值的问题大家能说说是不是没关闭数据库连接的问题?我是重新启动下tomcat后就可以了的请指教!谢谢!

祝大家工作顺利学业有成!谢谢阅读!

上一篇:JXTA下的媒体数据传输

下一篇:解决hibernate+mysql出现的隔天连接超时问题