BBS分页问题

jnkaixin2009 2009-03-26 08:53:02
一、BBS分页问题:
1)想实现分页功能
2)以下是有问题的平板式BBS分页程序( ShowArticleFlat.jsp)。

<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ page import = "java.sql.*"%>

<%
int pageSize = 3;
String strPageNo = request.getParameter("pageNo");
int pageNo;
if(strPageNo == null || strPageNo.equals("")){
pageNo = 1;

}else{

pageNo = Integer.parseInt(strPageNo.trim());
}

int startPos = (pageNo - 1) * pageSize;


Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/bbs?user=root&password=root";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from article where pid = 0 order by pdate desc limit" + startPos + "," + pageSize);


%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body>
<a href="Post.jsp">发表新帖</a>
<table border = "1">
<%
while(rs.next()){
%>

<tr>
<td>
<%=rs.getString("title")%>
</td>

</tr>

<%
}
rs.close();
stmt.close();
conn.close();
%>

</table>


</body>

</html>
3)以下是运行后报的错误。
HTTP Status 500 -
________________________________________
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Exception in JSP: /ShowArticleFlat.jsp:24

21: String url = "jdbc:mysql://localhost/bbs?user=root&password=root";
22: Connection conn = DriverManager.getConnection(url);
23: Statement stmt = conn.createStatement();
24: ResultSet rs = stmt.executeQuery("select * from article where pid = 0 order by pdate desc limit" + startPos + "," + pageSize);
25:
26:
27: %>


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:451)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:355)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
root cause
javax.servlet.ServletException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'limit0,3' at line 1
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:841)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:774)
org.apache.jsp.ShowArticleFlat_jsp._jspService(ShowArticleFlat_jsp.java:114)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
root cause
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'limit0,3' at line 1
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1026)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2548)
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2477)
com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1422)
org.apache.jsp.ShowArticleFlat_jsp._jspService(ShowArticleFlat_jsp.java:66)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.27 logs.
________________________________________
Apache Tomcat/5.5.27
4)显示的是sql语句有错,可是把sql语句放在dos窗口中运行时,并没有显示错误,可以从数据库中取出相应的内容。



找不出哪里的问题,帮帮忙呗,谢谢了
...全文
159 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
nature_he 2009-03-26
  • 打赏
  • 举报
回复
MS楼主在看SXT的视频
hoojo 2009-03-26
  • 打赏
  • 举报
回复
PageContextImpl.java:841
你分页的 实现类 有错误
你仔细看看 841 行
也可能是sql 写错了
下面是一个分页方法:

public List getAllstation(int pageSize,int currentpage){ //currentpage 当前页
List list = new ArrayList();
try {
String sql = "select top " + pageSize + " a.*,b.dname from station a,dept b where sid not in (select top " + (currentpage-1)*pageSize + " sid from station where isshow = 1 order by sid asc) and a.did = b.did and a.isshow = 1 order by sid asc";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
Station sta = new Station();
sta.setSid(rs.getInt("sid"));
sta.setDname(rs.getString("dname"));
sta.setSmoney(rs.getDouble("smoney"));
sta.setSname(rs.getString("sname"));
list.add(sta);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
DBHelper.closeRs(rs);
DBHelper.closePs(ps);
}
return list;
}


public int getTotalsPage() {
if (totals % pageSize == 0) {
this.totalsPage = totals / pageSize;
} else {
this.totalsPage = totals / pageSize + 1;
}
return totalsPage;
}
其中 totalsPage 总页数 totals 总数据行 (数据库中有多少行)
pageSize 每页显示多少行

兄弟,希望对你分页有所帮助
当你点下一页时 还有调用这个方法 不过要加 1 哦
^_*
liuchao1981 2009-03-26
  • 打赏
  • 举报
回复
楼上的逻辑很right。
楼主可以根据这个逻辑仔细查看你的代码。
SunnyPea 2009-03-26
  • 打赏
  • 举报
回复
楼上的逻辑很right。
楼主可以根据这个逻辑仔细查看你的代码。
ch370335226 2009-03-26
  • 打赏
  • 举报
回复
关于你的代码 我只能无语


JSP中分页效果实现步骤:
1、 在dao中定义按页和按每页记录条数参数查询数据的方法。
public List searchStudents(int pageNum, int Size){
List all = new ArrayList();
String sql = "select * from student where id in (select top "+(pageNum*Size)+" id from student) and id not in(select top "+((pageNum-1)*Size)+" id from student)";
try{
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
int id = 0;
String name = "";
int age = 0;
while(rs.next()){
id = rs.getInt(1);
name = rs.getString(2);
age = rs.getInt(3);
Student student = new Student(id, name, age);
all.add(student);
}
rs.close();
return all;
} catch(Exception ex){
System.out.println("数据库执行:"+ex.getMessage());
return null;
}
}

2、 在JSP页面中定义隐藏域,用来保存当前的页码。
3、 在点下一页或者上一页的时候,先改变隐藏域中的页码值,然后在提交表单到当前页面。
<script type="text/javascript">
function pageNext(op){
var num = document.form1.pageNum.value;
if(op==0){
num = parseInt(num) - 1;
} else {
num = parseInt(num) + 1;
}
document.form1.pageNum.value = num;
document.form1.submit();
}
</script>

4、 当前页面每次加载的时候需要重新获取客户端提交过来的页码,并及时更新隐藏域中的
值。
1-
<%
String Num = request.getParameter("pageNum");
int pageNum = 1;
if(Num != null){
pageNum = Integer.parseInt(Num);
}
%>
2-
<input name="pageNum" type="hidden" value="<%=pageNum%>">

ruoye_2008 2009-03-26
  • 打赏
  • 举报
回复
("select * from article where pid = 0 order by pdate desc limit" + startPos + "," + pageSize);

在limit后面加个空格试试,不然limit与后面的startPos的值会组成个字符串:

("select * from article where pid = 0 order by pdate desc limit " + startPos + "," + pageSize);

jnkaixin2009 2009-03-26
  • 打赏
  • 举报
回复
以上的代码还没有写完,只是简单的把一页3条信息给显示出来,问题出现在sql语句上。另外2楼和(7,8)楼的方法也很好。谢谢大家的帮助!!!
jnkaixin2009 2009-03-26
  • 打赏
  • 举报
回复
以上的代码还没有写完,只是简单的把一页3条信息给显示出来,问题出现在sql语句上。另外2楼和(7,8)楼的方法也很好。谢谢大家的帮助!!!
dami01 2009-03-26
  • 打赏
  • 举报
回复
url="jdbc:mysql://localhost:3306/bbs----" 端口号改过了吗? 是不是因为没写它啊
lyjilu2008 2009-03-26
  • 打赏
  • 举报
回复
<%
Buiness bui = new Buiness();
List<Product> arrProduct = bui.getProductInfo();//得到所以信息,总的信息
int curr = 0;//设置初始页数
String currStr = request.getParameter("curr");//得到当前在第几页
if (currStr == null)
curr = 1;
else
curr = Integer.parseInt(currStr);
int lengh = arrProduct.size();//得到有多少条信息
int size = 5;//每页显示行数
int pages = (lengh % size == 0) ? (lengh / size): (lengh / size + 1);//总页数
if(pages == 0)
pages = 1;
int begin = (curr - 1) * size;//开始页数
int end = 0;
if(curr != pages) //判断是否是最后一页
end = (curr) * size;
else
end = lengh;
%>


| 共<%=pages %>页 | 当前<%=curr %>页 |
<%
if( curr == 1){
%>
| 首页 | 上一页 |
<%
}else{
%>
<a href="product/showProduct.jsp?curr=<%=1%>"> 首页 </a>|
<a href="product/showProduct.jsp?curr=<%=curr-1 %>"> 上一页 </a>|
<%
}if(curr == pages){
%>
| 下一页 | 尾页 |
<%
}else{
%>
<a href="product/showProduct.jsp?curr=<%=curr+1 %>"> 下一页 </a>|
<a href="product/showProduct.jsp?curr=<%=pages%>">尾页 </a>|
<%
}
%>


这是以前在界面端做的分页!
lin_eric 2009-03-26
  • 打赏
  • 举报
回复
JSP中分页效果实现步骤:
1、 在dao中定义按页和按每页记录条数参数查询数据的方法。
public List searchStudents(int pageNum, int Size){
List all = new ArrayList();
String sql = "select * from student where id in (select top "+(pageNum*Size)+" id from student) and id not in(select top "+((pageNum-1)*Size)+" id from student)";
try{
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
int id = 0;
String name = "";
int age = 0;
while(rs.next()){
id = rs.getInt(1);
name = rs.getString(2);
age = rs.getInt(3);
Student student = new Student(id, name, age);
all.add(student);
}
rs.close();
return all;
} catch(Exception ex){
System.out.println("数据库执行:"+ex.getMessage());
return null;
}
}

81,107

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧