jsp+oracle实现数据库内容以表格形式在前台显示(包含分页)

时间:2023-03-08 15:46:50

jsp+oracle实现数据库内容以表格形式在前台显示(包含分页)

在文件夹内新增list_emp.jsp

代码如下:

 <%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.sql.*"%>
<html>
<head><title>java study</title></head>
<body>
<%!
//定义数据库驱动程序
public String DBDRIVER="oracle.jdbc.driver.OracleDriver";
//数据库连接地址
public String DBURL="jdbc:oracle:thin:@localhost:1521:yy";
public String DBUSER="scott";
public String DBPASS="tiger"; %>
<%
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
%>
<%
try{
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
int pageSize=3;
int pageNow=1;//默认显示第一页
int rowCount=0;//总笔数
int pageCount=0;
String s_pageNow=(String)request.getParameter("pageNow");
if(s_pageNow!=null){
pageNow = Integer.parseInt(s_pageNow);
}
String sql1 = "select count(*) from emp";
pstmt=conn.prepareStatement(sql1);
rs=pstmt.executeQuery();
if(rs.next()){
rowCount = rs.getInt(1);
}
if(rowCount%pageSize==0){
pageCount = rowCount/pageSize;
}else{
pageCount = rowCount/pageSize + 1;
}
String sql="SELECT empno,ename,job,hiredate,sal FROM (select a1.*,rownum rn from (select * from emp order by sal desc) a1 WHERE rownum<="+pageSize*pageNow+") where rn>="+((pageNow-1)*pageSize+1);
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
%>
<center>
<table border="1" width="80%">
<tr>
<td>雇员编号</td>
<td>雇员姓名</td>
<td>雇员工作</td>
<td>雇员日期</td>
<td>雇员工资</td>
</tr>
<%
while(rs.next()){
int empno=rs.getInt(1);
String ename=rs.getString(2);
String job=rs.getString(3);
java.util.Date date=rs.getDate(4);
float sal=rs.getFloat(5);
%>
<tr>
<td><%=empno%></td>
<td><%=ename%></td>
<td><%=job%></td>
<td><%=date%></td>
<td><%=sal%></td>
</tr>
<%
}
%>
</table>
<form Action= "" method= "post">
<%
for(int i=1;i<=pageCount;i++){
out.println("<a href=list_emp.jsp?pageNow="+i+">["+i+"]</a>");
}
if(pageNow != 1){
out.println( " <a href=list_emp.jsp?pageNow=1>首页</a> ");
out.println( " <a href=list_emp.jsp?pageNow="+(pageNow - 1)+">上一页</a> ");
}
if(pageNow != pageCount){
out.println( " <a href= list_emp.jsp?pageNow="+ (pageNow + 1)+"> 下一页</a>");
out.println( " <a href= list_emp.jsp?pageNow="+ pageCount+"> 最后一页</a>");
}
%>
</form>
</center>
<%
}catch(Exception e){
System.out.println(e);
}
finally{
try{
rs.close();
pstmt.close();
conn.close();
}catch(Exception e){
System.out.println(e);
}
}
%>
</body>
</html>

来源于 :http://blog.csdn.net/josephiney/article/details/6989841