新手怎么以最简单的方式实现jsp+mysql分页
4个回答
展开全部
刚开始弄 ,目前分页是在前台jsp中实现的 与bean相比 这样比较容易一些
先把代码贴出来 正在研究怎么样用bean实现分页 等弄好了 再贴代码
下面说下分页理论 select * from message order by time desc limit begin,size
begin 数据记录从第几条开始 begin=(当前页数-1)*size
size 每页的记录数
下面是连接数据库的javabean;
1: package bean;
2: import java.sql.*;
3:
4: public class MyDb{
5: private Statement stmt;
6: private Connection conn=null;
7: private ResultSet rs=null;
8:
9: public void connection(String db_name,String user,String pwd) throws Exception{
10: Class.forName("com.mysql.jdbc.Driver");
11: String url="jdbc:mysql://localhost:3306/" + db_name;
12: conn=DriverManager.getConnection(url,user,pwd);
13: stmt=conn.createStatement();
14: }
15:
16: public ResultSet query(String sql) throws Exception{
17: rs=stmt.executeQuery(sql);
18: return rs;
19: }
20:
21: public ResultSet query(String sql,int begin,int size) throws Exception{
22: String s=" limit " + begin + "," +size;
23: sql=sql + s;
24: rs=stmt.executeQuery(sql);
25: return rs;
26: }
27:
28:
29: public int update(String sql) throws Exception{
30: int n=0;
31: n=stmt.executeUpdate(sql);
32: return n;
33: }
34:
35: public boolean delete(String sql) throws Exception{
36: 0ff">boolean flag=false;
37: flag=stmt.execute(sql);
38: return flag;
39: }
40: }
下面是jsp的代码;
1: <jsp:useBean id="db" class="bean.MyDb" scope="page" />
2: <%
3: int page_count; //总页数
4: int p; //当前面号
5: int size=3; //每页记录数
6: int result_count; //总记录数
7: int begin; //当前页第一条记录索引号
8: ResultSet rs=null;
9: String page_s=request.getParameter("page");
10: if(page_s==null) page_s="1";
11: db.connection("test","root","root");
12: String sql="select count(*) from message";
13: rs=db.query(sql);
14: rs.next();
15: result_count=rs.getInt(1);
16: page_count=(result_count + size - 1)/size;
17: p=Integer.parseInt(page_s);
18: out.println("总记录数为:" + result_count + " 每页记录数为:" + size + " 总页数为:" + page_count + " 当前是第" + p + "页");
19: sql="select * from message order by time desc";
20: begin=(p-1)*size;
21: rs=db.query(sql,begin,size);
22: while(rs.next()){
23: %>
24: <table>
25: <tr>
26: <td width="200"><%=rs.getString(1)%></td>
27: <td width="200"><%=rs.getString(2)%></td>
28: </tr>
29: <tr>
30: <td><%=rs.getString(3)%></td>
31: <td><%=rs.getString(4)%></td>
32: </tr>
33: <tr><td colspan="2"><%=rs.getString(5)%></td></tr>
34: <tr><td colspan="2"><hr /></td></tr>
35: </table>
36: <%
37: }
38: if(p>1) out.println("<a href=test.jsp?page=" + (p-1) + ">上一页</a>");
39: if(p<page_count) out.println("<a href=test.jsp?page=" + (p+1) + ">下一页</a>");
40: %>
先把代码贴出来 正在研究怎么样用bean实现分页 等弄好了 再贴代码
下面说下分页理论 select * from message order by time desc limit begin,size
begin 数据记录从第几条开始 begin=(当前页数-1)*size
size 每页的记录数
下面是连接数据库的javabean;
1: package bean;
2: import java.sql.*;
3:
4: public class MyDb{
5: private Statement stmt;
6: private Connection conn=null;
7: private ResultSet rs=null;
8:
9: public void connection(String db_name,String user,String pwd) throws Exception{
10: Class.forName("com.mysql.jdbc.Driver");
11: String url="jdbc:mysql://localhost:3306/" + db_name;
12: conn=DriverManager.getConnection(url,user,pwd);
13: stmt=conn.createStatement();
14: }
15:
16: public ResultSet query(String sql) throws Exception{
17: rs=stmt.executeQuery(sql);
18: return rs;
19: }
20:
21: public ResultSet query(String sql,int begin,int size) throws Exception{
22: String s=" limit " + begin + "," +size;
23: sql=sql + s;
24: rs=stmt.executeQuery(sql);
25: return rs;
26: }
27:
28:
29: public int update(String sql) throws Exception{
30: int n=0;
31: n=stmt.executeUpdate(sql);
32: return n;
33: }
34:
35: public boolean delete(String sql) throws Exception{
36: 0ff">boolean flag=false;
37: flag=stmt.execute(sql);
38: return flag;
39: }
40: }
下面是jsp的代码;
1: <jsp:useBean id="db" class="bean.MyDb" scope="page" />
2: <%
3: int page_count; //总页数
4: int p; //当前面号
5: int size=3; //每页记录数
6: int result_count; //总记录数
7: int begin; //当前页第一条记录索引号
8: ResultSet rs=null;
9: String page_s=request.getParameter("page");
10: if(page_s==null) page_s="1";
11: db.connection("test","root","root");
12: String sql="select count(*) from message";
13: rs=db.query(sql);
14: rs.next();
15: result_count=rs.getInt(1);
16: page_count=(result_count + size - 1)/size;
17: p=Integer.parseInt(page_s);
18: out.println("总记录数为:" + result_count + " 每页记录数为:" + size + " 总页数为:" + page_count + " 当前是第" + p + "页");
19: sql="select * from message order by time desc";
20: begin=(p-1)*size;
21: rs=db.query(sql,begin,size);
22: while(rs.next()){
23: %>
24: <table>
25: <tr>
26: <td width="200"><%=rs.getString(1)%></td>
27: <td width="200"><%=rs.getString(2)%></td>
28: </tr>
29: <tr>
30: <td><%=rs.getString(3)%></td>
31: <td><%=rs.getString(4)%></td>
32: </tr>
33: <tr><td colspan="2"><%=rs.getString(5)%></td></tr>
34: <tr><td colspan="2"><hr /></td></tr>
35: </table>
36: <%
37: }
38: if(p>1) out.println("<a href=test.jsp?page=" + (p-1) + ">上一页</a>");
39: if(p<page_count) out.println("<a href=test.jsp?page=" + (p+1) + ">下一页</a>");
40: %>
展开全部
mysql
select * from t_table limit 0,20
-----------------------------------
请自行百度 limit 参数用法 pos,pagesize
将pos ,pagesize带入sql 即可后台分页
前端
ajax 得到的list 进行展示即可
select * from t_table limit 0,20
-----------------------------------
请自行百度 limit 参数用法 pos,pagesize
将pos ,pagesize带入sql 即可后台分页
前端
ajax 得到的list 进行展示即可
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用限定纪录条数的查询语句
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一、DBConn.java代码
package com;
import java.sql.*;
public class DBConn {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("驱动加载成功!");
} catch (Exception ex) {
System.out.println("驱动加载失败!");
ex.printStackTrace();
}
}
public static Connection getConn() {
try {
String url = "jdbc:mysql://localhost:3306/test?"
+ "user=root&password=123456&useUnicode=true&characterEncoding=utf8"; //数据库名,密码
Connection conn = DriverManager.getConnection(url);
System.out.println("获取数据库连接成功!");
return conn;
} catch (Exception ex) {
System.out.println("获取数据库连接失败!");
ex.printStackTrace();
return null;
}
}
public static void close(Connection conn, Statement st, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
}
}
if (st != null) {
try {
st.close();
} catch (Exception ex) {
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception ex) {
}
}
}
}
二、test.jsp代码:
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="java.sql.*"%>
<jsp:directive.page import="com.DBConn"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'user_save.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<%
Statement st = null;
int rs = 0;
Connection conn = null;
try{
conn = DBConn.getConn();
st = conn.createStatement(); //建立一个Statement对象,用于执行SQL语
//SQL 语句
String sql="UPDATE user SET tel='"+tel+"',email='"+email+"',addr='"+addr+"';
rs = st.executeUpdate(sql); //返回查询结果
//这里插入你的代码
}catch(SQLException e){
out.println("执行SQL\"" + sql + "\"时发生异常:" + e.getMessage());
return;
}finally{
if(st != null) st.close();
if(conn != null) conn.close();
}
%>
</head>
<body>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |