jsp java在Servlet 中对数据库中数据进行分页显示,为了实现在Servlet 中对数据库的分页显示,需要编写Servlet 的Java 程序,Servlet 程序为HttpServlet 接口的子类,在Servlet 中重写doGet()方法。将Servlet 类程序放到WEB服务器的servlets目录下面,为调用Servlet,需要创建发送Servlet 请求的HTML 文档,在表单调用时需要使用对应Servlet 的get 方法。程序代码如下:
1.OutDatabase 类包含两个字段:整型的count 和pcount,该类还有三个方法:init()、doGet()和destroy(),代码如下:
1 | public class outDatabase extends HttpServlet |
2 | { |
3 | int count= 0 ; |
4 | int pcount= 1 ; |
5 | public void init() throws ServletException; |
6 | PRotected void doGet(HttpServletRequest request,HttpServletRespose response) |
7 | throws ServletException,IOException |
8 | public void destroy(); |
9 | } |
2.Init()和destroy()方法不执行任何操作,doGet()方法连接数据库,执行查找,对数据库中数据实行分页显示,outDatabase 类的全部代码如下:
01 | import javax.servlet.*; |
02 | import javax.servlet.http.*; |
03 | import java.io.*; |
04 | import java.sql.*; |
05 | public class outDatabase extends HttpServlet |
06 | { |
07 | int count= 0 ; |
08 | int pcount= 1 ; |
09 | public void init() throws ServletException |
10 | { |
11 | } |
12 | protected void doGet(HttpServletRequest request,HttpServletRespose response) |
13 | throws ServletException,IOException |
14 | { |
15 | int rcount= 0 ; |
16 | boolean hasmore= false ; |
17 | java.io.PrintWriter out= new java.io.PrintWriter(response.getOutputStream()); |
18 | out.print( "<html>" ); |
19 | out.print( "<head><title>数据分页显示</title></head>" ); |
20 | out.print( "<body>" ); |
21 | try |
22 | { |
23 | Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ); |
24 | } |
25 | catch (Exception e){} |
26 | try |
27 | { |
28 | Connection con=DriverManager.getConnection( "jdbc:odbc:myDSN" ); |
29 | String str= "select * from chater" ; |
30 | Statement sm=con.createStatement(); |
31 | ResultSet rs=sm.executeQuery(str); |
32 | ResultSetMetaData rmd=rs.getMetaData(); |
33 | int ccount=rmd.getColumnCount(); |
34 | out.print( "<center>" ); |
35 | out.print( "<table border=/"1/">" ); |
36 | out.print( "<tr>" ); |
37 | //输出表头 |
38 | for ( int i= 1 ;i<=ccount;i++) |
39 | { |
40 | out.print( "<th>" ); |
41 | out.print(rmd.getColumnLabel(i)); |
42 | out.print( "</th>" ); |
43 | } |
44 | out.print( "</tr>" ); |
45 | while (rs.next()) |
46 | { |
47 | rcount++; |
48 | if (rcount>count*pcount && rcount<=(count+ 1 )*pcount) |
49 | { |
50 | out.print( "<tr>" ); |
51 | for ( int i= 1 ;i<=ccount;i++) |
52 | { |
53 | out.print( "<td>" ); |
54 | out.print(rs.getString(i)); |
55 | out.print( "</td>" ); |
56 | } |
57 | out.print( "</tr>" ); |
58 | } |
59 | if (rcount>(count+ 1 )*pcount) |
60 | { |
61 | hasmore=rs.next(); |
62 | break ; |
63 | } |
64 | } |
65 | out.print( "</table>" ); |
66 | out.print( "</center>" ); |
67 | sm.close(); |
68 | con.close(); |
69 | } |
70 | catch (SQLException e) |
71 | { |
72 | out.print(e.getMessage()); |
73 | } |
74 | if (hasmore) |
75 | { |
76 | out.print( "<br>" ); |
77 | out.print( "center>" ); |
78 | out.print( "<form action=/"submit/" value=/"next/">" ); |
79 | out.print( "</form>" ); |
80 | out.print( "</center>" ); |
81 | } |
82 | else |
83 | { |
84 | out.print( "数据输出完成" ); |
85 | } |
86 | out.print( "</body></html>" ); |
87 | out.flush(); |
88 | count++; |
89 | } |
90 | public void destroy() |
91 | {} |
92 | } |
3.因为程序使用了JDBC 类、servlet 类和使用控制台输出,所以需要引入如下的包:
1 | import java.sql.*; |
2 | import java.io.*; |
3 | import javax.servlet.*; |
4 | import javax.servlet.http.* |
4.编译outDatabase.java,产生outDatabase.class 文件,将outDatabase.class 放到WEB服务器的servlets 目录下,本例采用Java Web Server 作为WEB 服务器,配置好WEB 服务器,添加outDatabase.class,指定名称为outdatabase。
5.编写调用Servlet 的HTML 文件。程序如下:
01 | < html > |
02 | < head > |
03 | < title >Servlet数据库分页显示实例</ title > |
04 | </ head > |
05 | < body > |
06 | < center > |
07 | < form action = "/servlet/outdatabase" method = "get" > |
08 | < input name = "action" type = "submit" value = "分页显示" > |
09 | </ form > |
10 | </ center > |
11 | </ body > |
12 | </ html > |
这是个比较简单的JSP数据分页一例,仅供参考。
天骄国际美易购随心所欲新闻热点
疑难解答