网上找了用JDbc实现数据分页的程序,运行了下发现有错误,就是转折行的数据会不见了,即假入每页显示三行的话,第二页不会显示第四行的数据,直接从第五行开始,麻烦大家给看下是程序哪里的错误public class PageTest {
static String url="jdbc:mysql://localhost:3306/testdb";
static String user="root";
static String pw="123456";
public static void operation(){
System.out.println("===================================");
System.out.println("====this is the split operation====");
System.out.println("please input the number of the page");
}
public static void main(String[] args) {
operation();
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection(url, user, pw);
conn.setAutoCommit(false);
//设置可滚动更新的结果集
st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
//查询用户Id和姓名
String sql="select user_id,user_name from user order by user_id";
rs=st.executeQuery(sql);
//获取结果集大小
System.out.println(rs.getFetchSize());
Scanner scanner=new Scanner(System.in);
int i=scanner.nextInt();//每页显示多少行
int j=0;
rs.next();
do {
//当前显示的行数是否达到了指定的行数
if(i==j){
j=0;
//显示下一页
System.out.println("show the next page,please input the 'P'");
//显示上一页
System.out.println("show the last page,please input the 'l'");
System.out.println("exit,please input the 'e'");
String a=scanner.next();
if(a.equals("l")){
int rowNum=rs.getRow()-2*(i+1);//获取上一页的起始下标
if(rowNum==0){//判断是否回到起始下标
rs.absolute(1);
System.out.println(rs.getInt(1)+"=="+rs.getString(2));
j=1;
}
else{
rs.absolute(rowNum);//定位上一页的位置
}
continue;
}
if(a.equals("p")){
continue;
}
else
break;
}
System.out.println(rs.getInt(1)+"=="+rs.getString(2));
j++;
} while (rs.next());
conn.commit();
} catch (Exception e) {
e.printStackTrace();
try {
conn.rollback();
} catch (Exception e2) {
e2.printStackTrace();
}
}finally{
try {
st.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
static String url="jdbc:mysql://localhost:3306/testdb";
static String user="root";
static String pw="123456";
public static void operation(){
System.out.println("===================================");
System.out.println("====this is the split operation====");
System.out.println("please input the number of the page");
}
public static void main(String[] args) {
operation();
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection(url, user, pw);
conn.setAutoCommit(false);
//设置可滚动更新的结果集
st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
//查询用户Id和姓名
String sql="select user_id,user_name from user order by user_id";
rs=st.executeQuery(sql);
//获取结果集大小
System.out.println(rs.getFetchSize());
Scanner scanner=new Scanner(System.in);
int i=scanner.nextInt();//每页显示多少行
int j=0;
rs.next();
do {
//当前显示的行数是否达到了指定的行数
if(i==j){
j=0;
//显示下一页
System.out.println("show the next page,please input the 'P'");
//显示上一页
System.out.println("show the last page,please input the 'l'");
System.out.println("exit,please input the 'e'");
String a=scanner.next();
if(a.equals("l")){
int rowNum=rs.getRow()-2*(i+1);//获取上一页的起始下标
if(rowNum==0){//判断是否回到起始下标
rs.absolute(1);
System.out.println(rs.getInt(1)+"=="+rs.getString(2));
j=1;
}
else{
rs.absolute(rowNum);//定位上一页的位置
}
continue;
}
if(a.equals("p")){
continue;
}
else
break;
}
System.out.println(rs.getInt(1)+"=="+rs.getString(2));
j++;
} while (rs.next());
conn.commit();
} catch (Exception e) {
e.printStackTrace();
try {
conn.rollback();
} catch (Exception e2) {
e2.printStackTrace();
}
}finally{
try {
st.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
解决方案 »
- Hibnerate中单向关联字段如果为空怎么处理
- 请教个问题
- 求在Java中实现双色球的多种方法!
- javamail 自己发送给自己收不到邮件
- 继续向大家请教学习JAVA遇到的问题!——有关构造函数
- 高手帮忙解决问题,有几个textfield,当鼠标放在某个的时候,点一个按钮就在那个textfield添加文字!!
- 我想问一下如何将class文件生成可执行文件exe
- JAVA中AES算法的密钥强度问题,使用过AES的来
- 哪位GG有精通EJB第一版和第二版(电子版)(最好是中文)???给我发一份吧!!!
- Applet显示的问题?
- 哪位前辈有支持api模糊搜索的,小弟不胜感激!!!鞠躬!!!
- java问题
continue;
}
跳掉一个
do {..
} while (rs.next());换成 while(rs.next) {
...
}试试看
这一步错了。
do...while..有可能是do的时候多了一次了
continue a;
}
else
break;
}
a:
System.out.println(rs.getInt(1)+"=="+rs.getString(2));
j++;
看这样好使么
不知道怎么改,写这种程序干嘛? 你的写法是一个长连接,数据库应用中忌讳之一。 查完了就关掉Mysql可以用limit 20,5实现分页
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@page import="java.sql.*"%>
<%
int pageNum,startNum,pageSize,total,totalPage;
pageSize=4;
String pageStr=request.getParameter("pageNum");
new com.mysql.jdbc.Driver();
Connection conn = DriverManager
.getConnection("jdbc:mysql://localhost/bbs?user=root&password=tiger");
Statement stmt = conn.createStatement();
ResultSet rs=stmt.executeQuery("select count(*) from emp");
rs.next();
total=rs.getInt(1);
totalPage=total%pageSize==0?total/pageSize:(total/pageSize+1);
if(pageStr==null||"".equals(pageStr)){
pageNum=1;
startNum=0;
}else{
pageNum=Integer.parseInt(pageStr);
if(pageNum<1){
pageNum=1;
}else if(pageNum>totalPage){
pageNum=totalPage;
}
startNum=(pageNum-1)*pageSize;
}
rs= stmt.executeQuery("select * from emp limit "+startNum+","+pageSize);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<center>
<h1>员工信息表</h1>
<table border="1">
<tr><td>员工编号</td><td>姓名</td><td>工作种类</td><td>上司编号</td><td>入职时间</td><td>薪资</td><td>分公司编号</td><td>部门编号</td></tr>
<%
while (rs.next()) {
%>
<tr><td><%=rs.getInt(1)%></td><td><%=rs.getString(2)%></td><td><%=rs.getString(3)%></td><td><%=rs.getInt(4)%></td><td><%=rs.getDate(5)%></td><td><%=rs.getInt(6)%></td><td><%=rs.getInt(7)%></td><td><%=rs.getInt(8)%></td></tr>
<%
}
rs.close();
stmt.close();
conn.close();
%>
</table>
共<%=totalPage %>页,这是第<%=pageNum %>页
<a href="mysqlfenye.jsp">第一页</a>
<a href="mysqlfenye.jsp?pageNum=<%=pageNum-1%>">上一页</a>
<a href="mysqlfenye.jsp?pageNum=<%=pageNum+1%>">下一页</a>
<a href="mysqlfenye.jsp?pageNum=<%=totalPage%>">最后一页</a>
</center>
</body>
</html>
直接写在jsp里的,你改下吧。
这才对嘛<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@page import="java.sql.*"%>
<%
int pageNum,startNum,pageSize,total,totalPage;
pageSize=4;
String pageStr=request.getParameter("pageNum");
new com.mysql.jdbc.Driver();
Connection conn = DriverManager
.getConnection("jdbc:mysql://localhost/bbs?user=root&password=tiger");
Statement stmt = conn.createStatement();
ResultSet rs=stmt.executeQuery("select count(*) from emp");
rs.next();
total=rs.getInt(1);
totalPage=total%pageSize==0?total/pageSize:(total/pageSize+1);
if(pageStr==null||"".equals(pageStr)){
pageNum=1;
startNum=0;
}else{
pageNum=Integer.parseInt(pageStr);
if(pageNum<1){
pageNum=1;
}else if(pageNum>totalPage){
pageNum=totalPage;
}
startNum=(pageNum-1)*pageSize;
}
rs= stmt.executeQuery("select * from emp limit "+startNum+","+pageSize);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<center>
<h1>员工信息表</h1>
<table border="1">
<tr><td>员工编号</td><td>姓名</td><td>工作种类</td><td>上司编号</td><td>入职时间</td><td>薪资</td><td>分公司编号</td><td>部门编号</td></tr>
<%
while (rs.next()) {
%>
<tr><td><%=rs.getInt(1)%></td><td><%=rs.getString(2)%></td><td><%=rs.getString(3)%></td><td><%=rs.getInt(4)%></td><td><%=rs.getDate(5)%></td><td><%=rs.getInt(6)%></td><td><%=rs.getInt(7)%></td><td><%=rs.getInt(8)%></td></tr>
<%
}
rs.close();
stmt.close();
conn.close();
%>
</table>
共<%=totalPage %>页,这是第<%=pageNum %>页
<a href="mysqlfenye.jsp">第一页</a>
<a href="mysqlfenye.jsp?pageNum=<%=pageNum-1%>">上一页</a>
<a href="mysqlfenye.jsp?pageNum=<%=pageNum+1%>">下一页</a>
<a href="mysqlfenye.jsp?pageNum=<%=totalPage%>">最后一页</a>
</center>
</body>
</html>