这是我的代码,为什么它不能显示我第一次上传的数据呢?它只从第二个开始
<%@ page contentType="text/html; charset=GBK" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="demo.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
#Layer1 {
position:absolute;
left:326px;
top:18px;
width:539px;
height:90px;
z-index:1;
}
#Layer2 {
position:absolute;
left:89px;
top:134px;
width:1044px;
height:25px;
z-index:2;
}
#Layer3 {
position:absolute;
left:215px;
top:179px;
width:758px;
height:696px;
z-index:3;
background-color: #CCFFFF;
}
#Layer4 {
position:absolute;
left:793px;
top:880px;
width:90px;
height:31px;
z-index:4;
}
.STYLE1 {
font-size: 24px;
font-weight: bold;
}
-->
</style>
</head><body>
<div id="Layer1">
<table width="539" height="76" border="1" bordercolor="#0000FF">
<tr>
<td bordercolor="#0033FF"><div align="center">
<%
int pagesize=1; //每页显示的数据数量
//下边两个变量是把动态生成的部分集中在一起放入页面
//这样做的好处是当修改页面时就可以直接用Dreamweaver来编辑了
StringBuffer userInfo=new StringBuffer();
StringBuffer pageInfo=new StringBuffer();ResultSet rs = null; //数据库查询结果集
Connection conn = null;
Statement stmt = null;
try {
conn = DBConnection.getConnection();
conn.setAutoCommit(true);
//建立Statement对象并设置指针可前后移动
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE ,
ResultSet.CONCUR_READ_ONLY);
}catch(Exception e) {
System.err.println("数据库连接错误: " + e.getMessage());
}
//通过Statement执行SQL语句来获得查询结果
try{//从表userInfo中取出数据
rs = stmt.executeQuery("select * from new");
}catch(SQLException ex){
System.err.println("数据库查询错误: "+ex);
}
//从用户的请求中获取当前页码
//获得需要显示的页,如果用户请求没有页码参数,则默认是第1页
String paramPage=request.getParameter("page");
int currentPage=1;
try{
currentPage=Integer.parseInt(paramPage);
}catch(Exception e){
currentPage=1;
}
//将要显示的当前页的数据记录放入userinfo中
rs.last(); //把指针置底
int totaluser=rs.getRow(); //获得结果数量
//计算出总页数
int pagecount=(int)Math.ceil((float)totaluser/(float)pagesize);
int i=1;
rs.absolute((currentPage-1) * pagesize + 1); //把指针放到要显示的第一个数据
//把结果放进变量userInfo
while(i<=pagesize && rs.next()){ //userInfo.append("<tr bgcolor=#ffffff>\n");
/*userInfo.append("<td align=center>"+rs.getString("id")+"</td>");
userInfo.append("<td align=center>"+rs.getString("username")+"</td>");
userInfo.append("<td>"+rs.getString("phone")+"</td>");
userInfo.append("<td>"+rs.getString("email")+"</td>");
userInfo.append("</tr>");*/
out.println("<h3>"+"标题:"+"</h3>");
out.print("<h1>"+rs.getString(1)+"</h1>");
%> </div></td>
</tr>
</table>
</div>
<div id="Layer2"><hr color="#6666FF" size="5" width="900" noshade="noshade"></div>
<div id="Layer3"> <%
out.println("<h3>"+"内容:"+"</h3>");
out.print("<h3>"+rs.getString(2)+"</h3>");
if(!rs.next()) //当到达最后一个记录时退出循环
break;
i++;
}
//关闭数据库连接
try{
rs.close(); //关闭结果集对象
stmt.close(); //关闭Statement对象
conn.close(); //关闭数据库连接对象
}catch(SQLException e){
System.err.println(e);
}
//将要显示页码信息放入pageInfo
//把显示翻页的字段放进变量pageInfo
pageInfo.append("第"+currentPage+"页 共"+pagecount+"页,共"+totaluser+"篇新闻");
if(currentPage>1) //在当前页大于1时有向前翻页的连接,否则没有
pageInfo.append(" <a href='news3.jsp?page="+(currentPage-1)+"'><< 上一页</a>");
else
pageInfo.append("<< 上一页");
if(currentPage<pagecount) //在当前页小于总页数时有向前翻页的连接,否则没有
pageInfo.append(" <a href='news3.jsp?page="+(currentPage+1)+"'>下一页 >></a>");
else
pageInfo.append("下一页 >>");
//显示结果%> </table>
<table width="80%" border="0" align="center">
<tr>
<td align=right><%=pageInfo%></td>
</tr>
</table>
</div>
<div class="STYLE1" id="Layer4"><a href="news1.jsp">返回</a></div>
</body>
</html>
<%@ page contentType="text/html; charset=GBK" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="demo.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
#Layer1 {
position:absolute;
left:326px;
top:18px;
width:539px;
height:90px;
z-index:1;
}
#Layer2 {
position:absolute;
left:89px;
top:134px;
width:1044px;
height:25px;
z-index:2;
}
#Layer3 {
position:absolute;
left:215px;
top:179px;
width:758px;
height:696px;
z-index:3;
background-color: #CCFFFF;
}
#Layer4 {
position:absolute;
left:793px;
top:880px;
width:90px;
height:31px;
z-index:4;
}
.STYLE1 {
font-size: 24px;
font-weight: bold;
}
-->
</style>
</head><body>
<div id="Layer1">
<table width="539" height="76" border="1" bordercolor="#0000FF">
<tr>
<td bordercolor="#0033FF"><div align="center">
<%
int pagesize=1; //每页显示的数据数量
//下边两个变量是把动态生成的部分集中在一起放入页面
//这样做的好处是当修改页面时就可以直接用Dreamweaver来编辑了
StringBuffer userInfo=new StringBuffer();
StringBuffer pageInfo=new StringBuffer();ResultSet rs = null; //数据库查询结果集
Connection conn = null;
Statement stmt = null;
try {
conn = DBConnection.getConnection();
conn.setAutoCommit(true);
//建立Statement对象并设置指针可前后移动
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE ,
ResultSet.CONCUR_READ_ONLY);
}catch(Exception e) {
System.err.println("数据库连接错误: " + e.getMessage());
}
//通过Statement执行SQL语句来获得查询结果
try{//从表userInfo中取出数据
rs = stmt.executeQuery("select * from new");
}catch(SQLException ex){
System.err.println("数据库查询错误: "+ex);
}
//从用户的请求中获取当前页码
//获得需要显示的页,如果用户请求没有页码参数,则默认是第1页
String paramPage=request.getParameter("page");
int currentPage=1;
try{
currentPage=Integer.parseInt(paramPage);
}catch(Exception e){
currentPage=1;
}
//将要显示的当前页的数据记录放入userinfo中
rs.last(); //把指针置底
int totaluser=rs.getRow(); //获得结果数量
//计算出总页数
int pagecount=(int)Math.ceil((float)totaluser/(float)pagesize);
int i=1;
rs.absolute((currentPage-1) * pagesize + 1); //把指针放到要显示的第一个数据
//把结果放进变量userInfo
while(i<=pagesize && rs.next()){ //userInfo.append("<tr bgcolor=#ffffff>\n");
/*userInfo.append("<td align=center>"+rs.getString("id")+"</td>");
userInfo.append("<td align=center>"+rs.getString("username")+"</td>");
userInfo.append("<td>"+rs.getString("phone")+"</td>");
userInfo.append("<td>"+rs.getString("email")+"</td>");
userInfo.append("</tr>");*/
out.println("<h3>"+"标题:"+"</h3>");
out.print("<h1>"+rs.getString(1)+"</h1>");
%> </div></td>
</tr>
</table>
</div>
<div id="Layer2"><hr color="#6666FF" size="5" width="900" noshade="noshade"></div>
<div id="Layer3"> <%
out.println("<h3>"+"内容:"+"</h3>");
out.print("<h3>"+rs.getString(2)+"</h3>");
if(!rs.next()) //当到达最后一个记录时退出循环
break;
i++;
}
//关闭数据库连接
try{
rs.close(); //关闭结果集对象
stmt.close(); //关闭Statement对象
conn.close(); //关闭数据库连接对象
}catch(SQLException e){
System.err.println(e);
}
//将要显示页码信息放入pageInfo
//把显示翻页的字段放进变量pageInfo
pageInfo.append("第"+currentPage+"页 共"+pagecount+"页,共"+totaluser+"篇新闻");
if(currentPage>1) //在当前页大于1时有向前翻页的连接,否则没有
pageInfo.append(" <a href='news3.jsp?page="+(currentPage-1)+"'><< 上一页</a>");
else
pageInfo.append("<< 上一页");
if(currentPage<pagecount) //在当前页小于总页数时有向前翻页的连接,否则没有
pageInfo.append(" <a href='news3.jsp?page="+(currentPage+1)+"'>下一页 >></a>");
else
pageInfo.append("下一页 >>");
//显示结果%> </table>
<table width="80%" border="0" align="center">
<tr>
<td align=right><%=pageInfo%></td>
</tr>
</table>
</div>
<div class="STYLE1" id="Layer4"><a href="news1.jsp">返回</a></div>
</body>
</html>
解决方案 »
- 初学问题
- 请教使用my97datepicker时的问题
- 为什么会出现 org.apache.jasper.JasperException: No data found
- sf+ejb, 在页面上删除一条记录后,数据库显示已经删除。 可是页面上还是一直显示存在?
- jsp生成excel文档
- 问一个提交的问题~大伙帮忙解决下~!!
- 我在JSP页面import了这个类,为何还出could not load的错?
- 关于用java处理多语言的问题
- 怎样才能做到当鼠标在网页上移动时,鼠标后面有一个字跟着一起,并且随着鼠标移动的快慢,字之间的距离也不一样??
- 关于邮箱验证的问题
- Jsp与Servlet的学习顺序??
- myfaces参考实现报错java.lang.ClassNotFoundException: com.sun.faces.config.ConfigureListener
以前遇到过。你的代码不全,要不然帮你测试一下。
问题可能出在这里:
....
.......
int i=1;
rs.absolute((currentPage-1) * pagesize + 1); //把指针放到要显示的第一个数据
//把结果放进变量userInfo
while(i <=pagesize && rs.next()){
.......
... 改成试试:
rs.absolute((currentPage-1) * pagesize + 1); //把指针放到要显示的第一个数据
//把结果放进变量userInfo
rs.relative(-1);
while(i <=pagesize && rs.next()){ 看我发的帖子《一张表中记录读取记录时游标定位的问题(rowSet.next())!~》我用的是rowset(行集)离线结果集
你的是滚动的结果集,游标使用上差不多。帖子地址:
http://topic.csdn.net/u/20080514/12/a2f82a03-7f80-4067-90c2-89a966a55816.html最后再强调一下,问问题时千万别贴出来太多的代码,忒多了帖子回复的人数与你代码的字数成很大的反比!~抓住问题的关键!~祝你好运!~
这个分页的实现方法很多,楼主如果搞通意思了,很随意的就可以做出来。。
首先开发一个页面控制的JavaBean(PageBean.java)package com.jspdev.ch13;
import java.util.Vector;
public class PageBean
{
public int curPage ; //当前是第几页
public int maxPage ; //一共有多少页
public int maxRowCount ; //一共有多少行
public int rowsPerPage=5 ;//每页多少行
public java.util.Vector data;//这个是本页中要显示的资料
public PageBean()
{
}
public void countMaxPage() { //根据总行数计算总页数
if (this.maxRowCount % this.rowsPerPage==0){
this.maxPage = this.maxRowCount/this.rowsPerPage;
}else{
this.maxPage = this.maxRowCount/this.rowsPerPage + 1;
}
}
public Vector getResult()
{
return this.data;
}
public PageBean(ContactBean contact)throws Exception
{
this.maxRowCount = contact.getAvailableCount(); //得到总行数
this.data = contact.getResult(); //得到要显示于本页的数据
this.countMaxPage();
}
} //计算总页数
然后在写一个具体业务相关的javabean (ContactBean.java)
package com.jspdev.ch13;
import java.util.*;
import com.jspdev.util.*;
import java.sql.*;
public class ContactBean
{
private Connection conn;
Vector v;
public ContactBean()throws Exception
{
conn=DatabaseConn.getConnection();
v=new Vector();
}
public int getAvailableCount()throws Exception
{
int ret=0;
Statement stmt=conn.createStatement();
String strSql="select count(*) from contact";
ResultSet rset=stmt.executeQuery(strSql);
while(rset.next())
{
ret=rset.getInt(1);
}
return ret;
}
public PageBean listData(String page)throws Exception
{
try
{
PageBean pageBean=new PageBean(this);
int pageNum=Integer.parseInt(page);
Statement stmt=conn.createStatement();
String strSql="select top "+pageNum*pageBean.rowsPerPage+" * from contact order by userName";
ResultSet rset=stmt.executeQuery(strSql);
int i=0;
while(rset.next())
{
if(i>(pageNum-1)*pageBean.rowsPerPage-1)
{
Object[] obj=new Object[6];
obj[0]=rset.getString("userName");
obj[1]=new Integer(rset.getInt("mobile"));
obj[2]=rset.getString("phone");
obj[3]=rset.getString("mail");
obj[4]=rset.getDate("lastcontact");
obj[5]=rset.getString("mem");
v.add(obj);
}
i++;
}
rset.close();
stmt.close(); pageBean.curPage=pageNum;
pageBean.data=v;
return pageBean;
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
public Vector getResult()throws Exception
{
return v;
}
}
下边,再来一个servlet,用于接收客户端请求,调用ContactBean中的listDate方法,并且获得PageBean对象,把他保存在Request中。(ContactServlet.java)
package com.jspdev.ch13;
import javax.servlet.http.*;
import java.io.*;
public class ContactServlet extends javax.servlet.http.HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws javax.servlet.ServletException,java.io.IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try
{
ContactBean contact=new ContactBean();
PageBean pageCtl = contact.listData((String)request.getParameter("jumpPage"));
request.setAttribute("pageCtl",pageCtl);
}
catch(Exception e)
{
e.printStackTrace();
} javax.servlet.RequestDispatcher dis=request.getRequestDispatcher("/viewcontact");
dis.forward(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws javax.servlet.ServletException,java.io.IOException
{
doGet(request,response);
}
}
最后高一个翻页的功能的页面(Pageman.jsp),这个相当于一个可重用组件,需要分页的地方都能用哦。◎~<script language="JavaScript"><!--
function Jumping(){
document.PageForm.submit();
return ;
}function gotoPage(pagenum){
document.PageForm.jumpPage.value = pagenum;
document.PageForm.submit();
return ;
}-->
</script>每页<%=pageCtl.rowsPerPage%>行
共<%=pageCtl.maxRowCount%>行
第<%=pageCtl.curPage%>页
共<%=pageCtl.maxPage%>页
<BR>
<%if(pageCtl.curPage==1){ out.print(" 首页 上一页"); }else{ %>
<A HREF="javascript:gotoPage(1)">首页</A>
<A HREF="javascript:gotoPage(<%=pageCtl.curPage-1%>)">上一页</A>
<%}%>
<%if(pageCtl.curPage==pageCtl.maxPage){ out.print("下一页 尾页"); }else{ %>
<A HREF="javascript:gotoPage(<%=pageCtl.curPage+1%>)">下一页</A>
<A HREF="javascript:gotoPage(<%=pageCtl.maxPage%>)">尾页</A>
<%}%>
转到第<SELECT name="jumpPage" onchange="Jumping()">
<% for(int i=1;i<=pageCtl.maxPage;i++) {
if (i== pageCtl.curPage){
%>
<OPTION selected value=<%=i%>><%=i%></OPTION>
<%}else{%>
<OPTION value=<%=i%>><%=i%></OPTION>
<%}}%>
</SELECT>页测试一下,使用分页的页面(contact.jsp)<jsp:useBean id="pageCtl" class="com.jspdev.ch13.PageBean" scope="request"/>
<table border=1>
<% java.util.Vector v=pageCtl.getResult();
java.util.Enumeration e=v.elements();
while(e.hasMoreElements())
{
Object[] obj=(Object[])e.nextElement();
%>
<tr>
<td align="center" width="95"><%= obj[0] %></td>
<td align="center" width="93"><%= obj[1]%></td>
<td align="center" width="71"><%= obj[2] %></td>
<td align="center" width="142"><%= obj[3] %></td>
<td align="center" width="142"><%= obj[4] %></td>
<td align="center" width="142"><%= obj[5] %></td>
</tr>
<% }%>
</table>
<%if(pageCtl.maxPage!=1){%>
<form name="PageForm" action="/ch13/servlet/contactservlet" method="post">
<%@ include file="/pageman.jsp"%>
</form>
<%}%>
rs.absolute((currentPage-1) * pagesize + 1); //把指针放到要显示的第一个数据
//把结果放进变量userInfo
while(i <=pagesize && rs.next()){ 你首先已经把指针放到要显示的第一个数据了,然后在while循环判断条件时又用了一个“rs.next()”,就把指针移动到第二个数据了
所以会不显示第一个。
如果while循环条件中一定要判断rs.next(),那么第一次循环之前就要把指针放到第一个数据之前,才能保证第一次判断rs.next()后指针在第一个数据上。