此处一共涉及两个文件:
- dbaccess.jsp负责建立数据库连接,而且静态包好pase.jsp
dbaccess.jsp负责建立连接
1 <%@ page language="java" contentType="text/html; charset=GB2312" 2 pageEncoding="GB2312"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=GB2312"> 7 <title>dbaccess</title> 8 </head> 9 <body> 10 <% 11 try{ 12 Connection con; 13 Statement stmt; 14 Result rs; 15 16 //加载驱动器,线面的代码是加载MySQL驱动器 17 Class.forName("com.mysql.jdbc.Driver"); 18 //注册MySQL驱动器、 19 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); 20 21 //用适当的驱动器连接到数据库 22 String dbUrl = "jdbc:mysql://localhost:3306/DatabaseName?useUnicode=true&charaterEncoding=GB2312"; 23 String dbuser="username"; 24 String dbpwd = "password"; 25 26 //建立connection 27 con = java.sql.driverManager.getConnection(dbUrl,dbuser,dbpwd); 28 //Create sql 29 stmt = con.createStatement(); 30 31 %> 32 33 <%@ include file="pages.jsp" %> 34 35 <% 36 stmt.close(); 37 con.close(); 38 39 }catch(Exception e){ 40 out.println(e.getMessage()); 41 } 42 43 %> 44 </body> 45 </html>
2.pases.jsp负责分页显示数据库中表的数据;
Pages显示分页相关
1 <%@ page language="java" contentType="text/html; charset=GB2312 2 pageEncoding="GB2312%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=GB2312> 7 <title>pages</title> 8 </head> 9 <body> 10 <% 11 //分页变量的定义; 12 final int e=3; //每页显示的记录数 13 int totalPages=0; //页面总数 14 int currentPage=1; //当前page编号 15 int totalCount=0; //database中数据的所用数据 16 int p=0; //当前pase所显示的第一条记录的索引 17 18 //读取当前待显示的页面的编号 19 String tempStr=request.getParameter("CurrentPage"); 20 if (tempStr!=null && !tempStr.equals("")) 21 currentPage=Integer.parseInt(tempStr); 22 23 /*分页预备*/ 24 25 26 //计算总记录 27 rs=stmt.executeQuery("select count(*) from datatable;"); 28 if (rs.next()) 29 totalCount=rs.getInt(1); 30 31 //计算总的页数 32 totalPages=((totalCount%e==0)?(totalCount/e):(totalCount/e+1)); 33 if(totalPages==0) totalPages=1; 34 35 //修正当前page的编号,确保: 1<=currentPage<=totalPages; 36 if(currentPage>totalPages) 37 currentPage=totalPages; 38 else if(currentPag<1) 39 currentPage=1; 40 41 //计算当前page所显示的第一条记录索引 42 p=(currentPage-1)*e; 43 44 String sql="select XXX,XXX from datatable order by id limit " +p+",+e"; 45 rs=stmt.executeQuery(sql); 46 %> 47 48 <%-- 显示page标签-- %> 49 //yema 50 51 <% 52 for(int i=1;i<=totalPages;i++){ 53 if(i==currentPage) 54 %> 55 <%=i%> 56 57 <% }else{ %> 58 <a href="dbaccess.jsp?curentPage=<%=i%>"><%=i %></a> 59 <% } %> 60 <% } %> 61 62 共<%=totalPages%>页 ,共<%=totalCount%>条记录 63 64 65 <table border="1" width=400> 66 67 <tr> 68 <td bgcolor=""> </td> 69 <td bgcolor=""> </td> 70 <td bgcolor=""> </td> 71 <td bgcolor=""> </td> 72 </tr> 73 74 <% 75 while(rs.next()){ 76 String XXX = rs.getString(1); 77 String XXX = rs.getString(2); 78 String XXX = rs.getString(3); 79 float XXX = rs.getfloat(4); 80 81 %> 82 <tr> 83 <td><%=XXX %></td> 84 <td><%=XXX %></td> 85 <td><%=XXX %></td> 86 <td><%=XXX %></td> 87 </tr> 88 89 <% 90 } 91 %> 92 </table> 93 </body> 94 </html>