JSP pagination problem. Only the first page. Data can not be refreshed. I was just learning JAVA hope someone help me. Thank you!

Category: Web Develop
 
aaaabbbbcc1123
2010-10-23 03:17:29

Sponsored Links
I try to do with JSP Web page code is displayed on the page according to the books. But only display the first page. While you can point to the next page and previous page, but there is no data on the page updated. Not sure why. Code is as follows: I hope who can tell me where I should change somewhere. Thanks. This is conn.java
code. CLASS files with JAVAC compiled a directory on web-inf/class
package student;
import java.sql. *;
Public class conn {
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc: odbc: student";
Connection connect = ; null;
ResultSet rs = null;
public conn () {
try {
Class.forName (sDBDriver);
}
catch (java.lang.ClassNotFoundException e) {
System.err.println (e.getMessage ());
}
}
public ResultSet executeQuery (String sql) {
try {
connect = DriverManager.getConnection (sConnStr);
Statement stmt = connect.createStatement ();
rs = stmt.executeQuery (sql);
}
catch (SQLException ex) {
System.err.println (ex.getMessage ());
}
return rs;
}
public ; int executeUpdate (String sql) {
int result = 0;
try {
connect = DriverManager.getConnection (sConnStr);
Statement ; stmt = connect.createStatement ();
result = stmt.executeUpdate (sql);

}
catch (SQLException ex) {
System.err.println (ex.getMessage ());
}
return result;
}
}
Here Pagination pagev3.java file
<% @ page contentType = "text / html; charset = GBK"%>
<% @ page language = "java" import = "java.sql. *"%>
<% @ page import = "java.lang.Math. *"%>
<jsp:useBean id="conn" scope="page" class="student.conn"/>
<%!
ResultSet ; rs = null;
ResultSet rsTmp = null;
String sql = "";
int PageSize = 3;
int Page = 1;
int totalPage = 1;
String str = "";
public String ShowOnePage (ResultSet rs, int Page, int PageSize) {
str = "";
/ / first record pointer to the appropriate location
try {
rs.absolute ((Page-1) * PageSize + 1);
;} catch (SQLException e) {
}
for (int iPage = 1; iPage <= PageSize; ; iPage + +) {
str + = RsToGbook (rs);
try {
if ( ! rs.next ()) break;
} catch (Exception e) {}
}
return str;
}

/ / display a single line recording sub-module
public String RsToGbook (ResultSet rs) {
String tt = "" ;
try {
tt + = "<TR>";
tt + = "<TD> ; "+ rs.getString (" name ") +" </TD> ";
tt + =" <TD> "+ rs.getString (" sex ") +" </TD> ";
tt + = ; "<TD>" + rs.getString ("score") + "</TD>";

tt + = "</TR>";
} catch (SQLException e) {}
return ; tt;
}
%>
<%
sql = "select * from table1";
try {
rs = conn.executeQuery (sql);
} catch ( Exception e) {
out.println ("Access Database Error!");
}
%>
<html>
<head>
<title> tabbed browsing database skills </title>
</head>
<body bgcolor="#FFFFFF">
<h2 ALIGN="CENTER"> JSP in the pagination control, Version 3 </ h2>
<hr>
<center>
<table border>
<TR bgcolor=lightblue>

<TH> Student Name </TH>
<TH> sex </TH>
<TH> score </TH>
</TR>
<%
rsTmp = conn.executeQuery ("select count (*) as mycount from table1");
rsTmp.next ();
int totalrecord = ; rsTmp.getInt ("mycount");
if (totalrecord% PageSize == 0) totalPage = totalrecord / PageSize; / / If the current page is an integer multiple of
else totalPage = (int) Math.floor (totalrecord / PageSize) + 1; / / If the last also spare a
if (totalPage == 0) totalPage = 1;
rsTmp.close ();
try ; {
if (request.getParameter ("Page") == null | | request.getParameter ("Page"). equals (""))
; Page = 1;
else
Page = Integer.parseInt (request.getParameter ("Page"));
} catch (java.lang.NumberFormatException e) {/ / capture the user directly from the browser address bar enter Page = sdfsdfsdf caused by abnormal
Page = 1;
}
if (Page <1) Page = 1;
if (Page> totalPage) Page = totalPage;
out.println (ShowOnePage (rs, Page, PageSize));
%>
</table>
<form ; Action = "pagev3.jsp" Method = "GET">
<%
if (Page! = 1) {
out.println ("<A HREF=pagev3.jsp?Page=1> First Page </A> �");
out.println ("<A HREF=pagev3.jsp?Page=" + (Page-1) + "> on a </A> � ");
}
if (Page! = totalPage) {
out.println ("<A HREF=pagev3.jsp?Page=" + (Page+1) + "> Next </A> � ");

out.println (" <A HREF = pagev3.jsp? Page = "+ totalPage +"> Last page </A> � ");
}
rs. close ();
%>
<p> Jump to page: <input TYPE = "TEXT" Name = "Page" SIZE = "3" ;> Pages: <font COLOR="Red"> <% = Page%> / <% = totalPage%> </font>
</p>
</form>
</center>
<hr>
</body>
</html>
use the ACCESS database. You can display the first page but the data can not be refreshed. I do not know what wrong? Which experts hope will do us a favor. Tell wrong place should be how to change

Sponsored Links

xiaok_temp2
2010-10-23 03:39:20
a simple example of the distinction:

<% @ page contentType = "text / html; charset = 8859_1 "%>

<%
/ / variable declaration
java.sql.Connection sqlCon; / / database connection object
java . sql.Statement sqlStmt; / / SQL statement object
java.sql.ResultSet sqlRst; / / result set object

java.lang.String strCon; ;/ / database connection string
java.lang.String strSQL; / / SQL statement

int intPageSize; / / a number of records displayed
int intRowCount; / / Total number of records
int intPageCount; / / total pages
int intPage; / / page number to be displayed
java.lang.String ; strPage;

int i;

/ / set a number of records displayed
intPageSize = 2;

/ / Get the page number to be displayed
strPage = request.getParameter ("page");
if (strPage == null) {/ / QueryString that no page in this parameter, At this point the first page data
intPage = 1;
}
else {/ / convert a string to integer
intPage = java.lang . Integer.parseInt (strPage);
if (intPage <1) intPage = 1;
}

/ / Load the JDBC driver
java.sql.DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver ());

/ / set the database connection string
strCon = "jdbc: oracle : thin: @ linux: 1521: ora4cweb ";

/ / connect to database
sqlCon = java.sql.DriverManager.getConnection (strCon," hzq "," hzq " ;);

/ / create a scrollable read-only SQL statement object
sqlStmt = sqlCon.createStatement (java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet . CONCUR_READ_ONLY);

/ / prepare the SQL statement
strSQL = "select name, age from test";

/ / execute SQL statement and get the result set
sqlRst = sqlStmt.executeQuery (strSQL);

/ / Get total number of records
sqlRst.last ();
intRowCount ; = sqlRst.getRow ();

/ / note count total number of pages
intPageCount = (intRowCount + intPageSize-1) / intPageSize;

/ / adjust the page to be displayed
if (intPage> intPageCount) intPage = intPageCount;
%>

<html> ;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
< ; title> JSP database operation routines - data pagination - JDBC 2.0 - Oracle </title>
</head>

<body>

<table border="1" cellspacing="0" cellpadding="0">
<tr>
<th> Name </th>
<th> Age </th>
</tr>

<%
if (intPageCount> 0) {
/ / the record pointer is positioned to be the first record displayed on the page
sqlRst.absolute ((intPage-1) * intPageSize + ; 1);

/ / display data
i = 0;
while (i <intPageSize &&! sqlRst.isAfterLast ()) {
%>
<tr>
<td> <% = sqlRst.getString (1)%> </td>
<td> <% = sqlRst.getString (2)%> </td>
</tr>
<%
sqlRst.next ();
i + +;
}
}
%>

</table>

first <% = intPage%> page Total <% = intPageCount%> page <% if (intPage <intPageCount){%> <a href = "jdbc20-oracle.jsp? page = <% = intPage +1%> ; "> Next </a> <%}%> <% if (intPage> 1) {%> <a href =" jdbc20-oracle.jsp? page = <% = intPage-1%> "> Previous </a> <%}%>

</body>
</html>

<%
/ / close the result set
sqlRst.close ();

/ / Close the SQL statement object
sqlStmt.close ();

/ / close the database
sqlCon.close ();
%>
taoshengyijiu1985
2010-10-23 04:14:12
look quite understand.

you first try.

the pagev3.java file
int Page = 1;
changed:
int Page = 1;
String p = request.getParamenter ("Page");
if (p! = null && p.length ()> 0 &&! p.equals ("null") ) {
Page = Integet.parseInt (p);
System.out.println (Page) ;/ / put this straight fight out to see, in the end is how much.
}
noopy911
2010-10-23 04:53:03
access database may be the reason, you can switch to sqlserver try
odin890607
2010-10-23 05:25:29
landlord why not search, forums about pagination too many examples, I also back a lot, and now numb.
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.