java数据库操作问题(上一条数据、下一条数据)

java界面中两个文本框两个按钮(“上一条”、“下一条”),从数据库中获取数据,单击单击“下一条”按钮在两个文本框中显示结果集中的第一条记录的两个字段值,再次单击显示第二... java界面中两个文本框两个按钮(“上一条”、“下一条”),从数据库中获取数据,单击单击“下一条”按钮在两个文本框中显示结果集中的第一条记录的两个字段值,再次单击显示第二条记录的两个字段值,依次类推。而单击“上一条”按钮时,如果此时文本框显示的是第3条记录,则文本框显示第2条记录的两个字段值。依次类推。谢谢!
对不起,我没说清楚,我是在swing中做的,就是桌面程序!
public void actionPerformed(ActionEvent e){
//连接读取数据库代码,已有。关键是下面单机按钮代码
if(e.getSource()==btnNext){
//第一次单击按钮,下面只是显示了第一条的数据,再次单击按钮还是现实第一条数据。我希望再次单击"下一条"按钮显示第二条数据?以此类推一直显示到最后一条记录数据?
rs.next();
t1.setText(rs.getString("ID"));
t2.setText(rs.getString("NAME"));
}
if(e.getSource()==btnPre){
//怎么显示当前已经显示的数据的上一条数据?不断单击该按钮一直显示到第一条记录数据?
}
}
}

谢谢诶已经解决了,就是 结果集默认不滚动,需要让其滚动就可以了!
展开
 我来答
yunsyz
2013-04-18 · 超过32用户采纳过TA的回答
知道答主
回答量:86
采纳率:0%
帮助的人:50.3万
展开全部

给你个思路吧:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'list.jsp' starting page</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<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">
-->
<style>
.row0{
background: #AABBFF;
}
.row1{
background: #FFAABB;
}
</style>
</head>
<body>
<div><span style="color:red;">请用list.action访问该页面</span></div>
<table width="100%" border="1px solid black" cellpadding="0" cellspacing="0">
<caption>用户列表及操作</caption>
<thead>
<tr>
<th width="20%">ID</th>
<th width="20%">用户名</th>
<th width="20%">密码</th>
<th width="20%">是否可用</th>
<th width="20%">操作</th>
</tr>
</thead>
<tbody>
<s:iterator value="%{users}" status="s">
<tr class="row${s.index%2 }">
<td>${id }</td>
<td>${username }</td>
<td>${password }</td>
<td>${valid }</td>
<td>
<a href="delete.action?id=${id }&p.page=${p.page }" onclick="return confirm('确定删除?')">删除</a>
<a href="load.action?id=${id }&p.page=${p.page }">修改</a>
</td>
</tr>
</s:iterator>
</tbody>
</table>
<div>
<form action="list.action">
<s:if test="%{p.page != 1}">
<a href="list.action?p.page=1">首页</a>
</s:if>
<s:else>
<a style="color:#ccc;">首页</a>
</s:else>
<s:if test="%{p.page != 1}">
<a href="list.action?p.page=${p.page-1 }">上一页</a>
</s:if>
<s:if test="%{p.page != p.totalPage}">
<a href="list.action?p.page=${p.page+1 }">下一页</a>
</s:if>
<s:if test="%{p.page != p.totalPage}">
<a href="list.action?p.page=${p.totalPage }">末页</a>
</s:if>
<s:else>
<a style="color:#ccc;">末页</a>
</s:else>
<span>第${p.page }页/共${p.totalPage }页</span>
<span>跳<input id="page" name="p.page" style="width:30px;">页</span>
<input type="submit" value="go">
</form>
</div>
<div>
<input type="button" value="添加用户" onclick="window.location='add.jsp'"/>
</div>
</body>
</html>

package com.action;
import java.util.ArrayList;
import java.util.List;
import com.dao.UserDao;
import com.dao.UserDaoImpl;
import com.opensymphony.xwork2.ActionSupport;
import com.pojo.User;
import com.util.Pagination;
public class UserAction extends ActionSupport{
private UserDao dao = new UserDaoImpl();
private List<User> users = new ArrayList<User>();
private User user;
private int id;
private Pagination p = new Pagination();
public String list(){
System.out.println("======================list.action");
try {
System.out.println("list:"+p.getPage()+p.getPageSize());
dao.getMaxPage(p);
users = dao.list(p);
return "success";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "failed";
}
public String add(){
System.out.println("======================add.action");
try {
dao.add(user);
return "success";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "failed";
}
public String delete(){
System.out.println("======================delete.action");
try {
dao.delete(id);
return "success";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "failed";
}
public String load(){
System.out.println("======================load.action");
try {
user = dao.findUserById(id);
return "success";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "failed";
}
public String modify(){
System.out.println("======================modify.action");
try {
user.setId(id);//设置user的id为所要修改的id
dao.update(user);
return "success";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "failed";
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Pagination getP() {
return p;
}
public void setP(Pagination p) {
this.p = p;
}
}

package com.util;
/**
* 分页
* @author yun
*
*/
public class Pagination {
private int pageSize = 3;//每页显示几条数据
private int totalPage = Integer.MAX_VALUE;//共多少页
private int page = 1;//第几页
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
if(pageSize <= 0){
pageSize = 3;
}
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
if(totalPage <=0){
totalPage = 1;
}
this.totalPage = totalPage;
//下面的setPage(page)一定要有,因为totalPage是查询出来的,这会影响page的值。
//如:原来有12页数据,现在查询出来的只有2页,那么page大于2的页应该就不存在了
setPage(page);
}
public int getPage() {
return page;
}
public void setPage(int page) {
System.out.println("=========totalPage:"+totalPage);
if(page <= 0){
page = 1;
}
if(page > totalPage){
page = totalPage;
}
this.page = page;
}
}

package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.pojo.User;
import com.util.DBUtil;
import com.util.Pagination;
public class UserDaoImpl implements UserDao {
public void add(User u) throws Exception {
int id = searchMaxId();//获得id值,相当于id = seq.nextval()
System.out.println("获得id值=============="+id);
u.setId(id);
String sql = "insert into s_user(id,username,password,valid) " +
" values(?,?,?,?)";
Connection conn = DBUtil.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, u.getId());
pstmt.setString(2, u.getUsername());
pstmt.setString(3, u.getPassword());
pstmt.setString(4, u.getValid());
System.out.println("打印sql:"+sql+"\t参数:["+u.getId()+","+u.getUsername()+","+u.getPassword()+","+u.getValid()+"]");
pstmt.executeUpdate();
DBUtil.close(pstmt);
DBUtil.close(conn);
}
/**
* 查询记录中最大的id,如果没有就默认为1,有就+1返回
* @return
*/
private int searchMaxId() {
int id = 1;
String sql = "select max(id) mid from s_user";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
stmt = conn.createStatement();
System.out.println("打印sql:"+sql);
rs = stmt.executeQuery(sql);
if(rs.next()){
id = rs.getInt("mid")+1;//有结果,就+1
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return id;
}
public void delete(int id) throws Exception {
String sql = "delete from s_user where id = ?";
Connection conn = DBUtil.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
System.out.println("打印sql:"+sql+"\t参数:["+id+"]");
pstmt.executeUpdate();
DBUtil.close(pstmt);
DBUtil.close(conn);
}
public User findUserById(int id) throws Exception {
User u = null;
String sql = "select * from s_user where id = ?";
Connection conn = DBUtil.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
System.out.println("打印sql:"+sql+"\t参数:["+id+"]");
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
u = new User();
u.setId(rs.getInt("id"));
u.setUsername(rs.getString("username"));
u.setPassword(rs.getString("password"));
u.setValid(rs.getString("valid"));
}
DBUtil.close(rs);
DBUtil.close(pstmt);
DBUtil.close(conn);
return u;
}
public List<User> list() throws Exception {
List<User> list = new ArrayList<User>();
String sql = "select id,username,password,valid from s_user order by id";
Connection conn = DBUtil.getConnection();
Statement stmt = conn.createStatement();
System.out.println("打印sql:"+sql);
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
String valid = rs.getString("valid");
User u = new User();
u.setId(id);
u.setUsername(username);
u.setPassword(password);
u.setValid(valid);
list.add(u);
}
DBUtil.close(rs);
DBUtil.close(stmt);
DBUtil.close(conn);
return list;
}
public List<User> list(Pagination p) throws Exception {
List<User> list = new ArrayList<User>();
String sql = "select * from (select ini.*,rownum rn from " +
"(select id,username,password,valid from s_user order by id) ini " +
"where rownum <= ?) where rn >= ? ";
int begin = p.getPageSize()*(p.getPage()-1)+1;//10*(3-1)+1=21 1-10 11-20 21-30
int end = p.getPageSize()*p.getPage();//10*3=30
Connection conn = DBUtil.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, end);
pstmt.setInt(2, begin);
System.out.println("打印sql:"+sql+"\t参数:["+end+","+begin+"]");
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
String valid = rs.getString("valid");
User u = new User();
u.setId(id);
u.setUsername(username);
u.setPassword(password);
u.setValid(valid);
list.add(u);
}
DBUtil.close(rs);
DBUtil.close(pstmt);
DBUtil.close(conn);
return list;
}
public void update(User u) throws Exception {
String sql = "update s_user set username=?,password=?,valid=? where id=?";
Connection conn = DBUtil.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, u.getUsername());
pstmt.setString(2, u.getPassword());
pstmt.setString(3, u.getValid());
pstmt.setInt(4, u.getId());
System.out.println("打印sql:"+sql+"\t参数:["+u.getUsername()+","+u.getPassword()+","+u.getValid()+","+u.getId()+"]");
pstmt.executeUpdate();
DBUtil.close(pstmt);
DBUtil.close(conn);
}
/**
* 设置最大页
* @param p
* @throws SQLException
*/
public void getMaxPage(Pagination p) throws SQLException{
String sql = "select count(*) from s_user";
Connection conn = DBUtil.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()){
int s = rs.getInt(1);//一共有多少条数据
int n = p.getPageSize();//每页大小
int t = (s+n-1)/n;
p.setTotalPage(t);//共有多少页
}
DBUtil.close(rs);
DBUtil.close(stmt);
DBUtil.close(conn);
}
/**
* 测试UserDaoImpl
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
UserDaoImpl impl = new UserDaoImpl();
int size = impl.list().size();
System.out.println("size======"+size);//size======0
}
}

 

总体来说,这是一个翻页查询问题。

上一条,下一条如果是两个按钮的话,就要写javascript去异步发送请求了,请求时来带上参数,参数为当前的页数减一(上一条),或加一(下一条)。然后就是写查询语句了。

如:

public List<User> list(Pagination p) throws Exception {

  List<User> list = new ArrayList<User>();

  String sql = "select * from (select ini.*,rownum rn from " +

    "(select id,username,password,valid from s_user order by id) ini " +

    "where rownum <= ?) where rn >= ? ";

  int begin = p.getPageSize()*(p.getPage()-1)+1;//10*(3-1)+1=21  1-10 11-20 21-30

  int end = p.getPageSize()*p.getPage();//10*3=30

  Connection conn = DBUtil.getConnection();

  PreparedStatement pstmt = conn.prepareStatement(sql);

  pstmt.setInt(1, end);

  pstmt.setInt(2, begin);

  System.out.println("打印sql:"+sql+"\t参数:["+end+","+begin+"]");

  ResultSet rs = pstmt.executeQuery();

  while(rs.next()){

   int id = rs.getInt("id");

   String username = rs.getString("username");

   String password = rs.getString("password");

   String valid = rs.getString("valid");

   User u = new User();

   u.setId(id);

   u.setUsername(username);

   u.setPassword(password);

   u.setValid(valid);

   list.add(u);

  }

  DBUtil.close(rs);

  DBUtil.close(pstmt);

  DBUtil.close(conn);

  return list;

 }

 

 

 

 

 

 

 

 

 

 

 

同理,还是要去查询,然后得到两条记录,还是要传参数的。

sp_co
2013-04-19 · TA获得超过113个赞
知道答主
回答量:152
采纳率:0%
帮助的人:54.5万
展开全部
要么使用分页查询,要么你做缓存把几条数据存下来,点击一次取一次。
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
CaesireKin
2013-04-19
知道答主
回答量:28
采纳率:0%
帮助的人:16.1万
展开全部
传递参数控制查询的数据啊!!!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 2条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式