jsp+oracle实现数据库内容以表格形式在前台显示(包含分页)
在文件夹内新增list_emp.jsp
代码如下:
1 <%@ page contentType="text/html" pageEncoding="GBK"%> 2 <%@ page import="java.sql.*"%> 3 <html> 4 <head><title>java study</title></head> 5 <body> 6 <%! 7 //定义数据库驱动程序 8 public String DBDRIVER="oracle.jdbc.driver.OracleDriver"; 9 //数据库连接地址 10 public String DBURL="jdbc:oracle:thin:@localhost:1521:yy"; 11 public String DBUSER="scott"; 12 public String DBPASS="tiger"; 13 14 %> 15 <% 16 Connection conn=null; 17 PreparedStatement pstmt=null; 18 ResultSet rs=null; 19 %> 20 <% 21 try{ 22 Class.forName(DBDRIVER); 23 conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS); 24 int pageSize=3; 25 int pageNow=1;//默认显示第一页 26 int rowCount=0;//总笔数 27 int pageCount=0; 28 String s_pageNow=(String)request.getParameter("pageNow"); 29 if(s_pageNow!=null){ 30 pageNow = Integer.parseInt(s_pageNow); 31 } 32 String sql1 = "select count(*) from emp"; 33 pstmt=conn.prepareStatement(sql1); 34 rs=pstmt.executeQuery(); 35 if(rs.next()){ 36 rowCount = rs.getInt(1); 37 } 38 if(rowCount%pageSize==0){ 39 pageCount = rowCount/pageSize; 40 }else{ 41 pageCount = rowCount/pageSize + 1; 42 } 43 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); 44 pstmt=conn.prepareStatement(sql); 45 rs=pstmt.executeQuery(); 46 %> 47 <center> 48 <table border="1" width="80%"> 49 <tr> 50 <td>雇员编号</td> 51 <td>雇员姓名</td> 52 <td>雇员工作</td> 53 <td>雇员日期</td> 54 <td>雇员工资</td> 55 </tr> 56 <% 57 while(rs.next()){ 58 int empno=rs.getInt(1); 59 String ename=rs.getString(2); 60 String job=rs.getString(3); 61 java.util.Date date=rs.getDate(4); 62 float sal=rs.getFloat(5); 63 %> 64 <tr> 65 <td><%=empno%></td> 66 <td><%=ename%></td> 67 <td><%=job%></td> 68 <td><%=date%></td> 69 <td><%=sal%></td> 70 </tr> 71 <% 72 } 73 %> 74 </table> 75 <form Action= "" method= "post"> 76 <% 77 for(int i=1;i<=pageCount;i++){ 78 out.println("<a href=list_emp.jsp?pageNow="+i+">["+i+"]</a>"); 79 } 80 if(pageNow != 1){ 81 out.println( " <a href=list_emp.jsp?pageNow=1>首页</a> "); 82 out.println( " <a href=list_emp.jsp?pageNow="+(pageNow - 1)+">上一页</a> "); 83 } 84 if(pageNow != pageCount){ 85 out.println( " <a href= list_emp.jsp?pageNow="+ (pageNow + 1)+"> 下一页</a>"); 86 out.println( " <a href= list_emp.jsp?pageNow="+ pageCount+"> 最后一页</a>"); 87 } 88 %> 89 </form> 90 </center> 91 <% 92 }catch(Exception e){ 93 System.out.println(e); 94 } 95 finally{ 96 try{ 97 rs.close(); 98 pstmt.close(); 99 conn.close(); 100 }catch(Exception e){ 101 System.out.println(e); 102 } 103 } 104 %> 105 </body> 106 </html>
来源于 :http://blog.csdn.net/josephiney/article/details/6989841