如何把ResultSet中的指针放到指定的位置

xiaomeizhun 2007-08-09 04:11:00
如何把ResultSet中的指针放到指定的位置
int size=0;
try{
while(rs.next()){
size+=1;
}
}catch(SQLException e){
System.out.println(e.toString());
}
这段程序执行后,rs的指针是否已经在最后一行了。如何把指针再放回到初始位置呢?
...全文
903 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackson416 2007-08-09
  • 打赏
  • 举报
回复
楼主你倒粪啊!!!!!!!!!!!!!!!!!!!
xiaomeizhun 2007-08-09
  • 打赏
  • 举报
回复
结贴了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
xiaomeizhun 2007-08-09
  • 打赏
  • 举报
回复
问题解决了。就是因为我在连接数据库中的类写的有问题。
把原来的stmt=conn.createStatement();
改成stmt=conn.createStatement=(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
再把代码改为:就可以了
package aaa;
import java.sql.*;

public class a111{
public static void main(String[] args){

javaConnectToSql jcts=new javaConnectToSql();
jcts.Conn("com.microsoft.jdbc.sqlserver.SQLServerDriver","jdbc:microsoft:sqlserver://localhost:1433;databaseName=biyesheji","sa","");

ResultSet rs=null;
rs=jcts.QuerySql("select * from historyInfo where flag=0");
int size=0;
try{
while(rs.next()){
size+=1;
}

}catch(SQLException e){
System.out.println(e.toString());
}
System.out.println(size);//得到size=3;

try{
rs.beforeFirst();
if(size>0){
while(rs.next()){
System.out.println(rs.getString(1));
}
}
else{
while(rs.next()){
System.out.println(rs.getString(2));
}

}

}catch(SQLException e){
System.out.println(e.toString());
}
}
}

谢谢各位!!!!!!!!!!!
awusoft 2007-08-09
  • 打赏
  • 举报
回复
Statement stmt=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY)


主要是在创建statement的时候要指定产生的结果是否可以来回的读取
awusoft 2007-08-09
  • 打赏
  • 举报
回复
跟数据库驱动和获得Statement对象时指定的参数有关系

有些驱动不支持这个操作,尽量使用版本比较新的数据库驱动程序

Statement stmt=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY)
这里面的参数会有影响

static int TYPE_FORWARD_ONLY
The constant indicating the type for a ResultSet object whose cursor may move only forward.
static int TYPE_SCROLL_INSENSITIVE
The constant indicating the type for a ResultSet object that is scrollable but generally not sensitive to changes made by others.
static int TYPE_SCROLL_SENSITIVE
The constant indicating the type for a ResultSet object that is scrollable and generally sensitive to changes made by others.

xiaomeizhun 2007-08-09
  • 打赏
  • 举报
回复
哦,有道理,我试一下

testejbzdx 2007-08-09
  • 打赏
  • 举报
回复
跟数据库驱动和获得Statement对象时指定的参数有关系

有些驱动不支持这个操作,尽量使用版本比较新的数据库驱动程序

Statement stmt=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY)
这里面的参数会有影响

static int TYPE_FORWARD_ONLY
The constant indicating the type for a ResultSet object whose cursor may move only forward.
static int TYPE_SCROLL_INSENSITIVE
The constant indicating the type for a ResultSet object that is scrollable but generally not sensitive to changes made by others.
static int TYPE_SCROLL_SENSITIVE
The constant indicating the type for a ResultSet object that is scrollable and generally sensitive to changes made by others.
liu_zhaoqf 2007-08-09
  • 打赏
  • 举报
回复
在生成ResultSet之前也要将它设置成可滚动啊
xiaomeizhun 2007-08-09
  • 打赏
  • 举报
回复
高手,怎么办啊?很着急啊
xiaomeizhun 2007-08-09
  • 打赏
  • 举报
回复
可是我在用JCreator时可以引用出来啊
xiaomeizhun 2007-08-09
  • 打赏
  • 举报
回复
啊?那有什么别的办法么?
supermanyan10 2007-08-09
  • 打赏
  • 举报
回复
版本低
没有这样的方法的……
xiaomeizhun 2007-08-09
  • 打赏
  • 举报
回复
这跟版本有关系?
xiaomeizhun 2007-08-09
  • 打赏
  • 举报
回复
1.3.0
HenryHee 2007-08-09
  • 打赏
  • 举报
回复
你的jdk版本是?
xiaomeizhun 2007-08-09
  • 打赏
  • 举报
回复
这些方法的具体含义我知道,但是不知道怎么用?请教了
xiaomeizhun 2007-08-09
  • 打赏
  • 举报
回复
try{
if(rs.isLast())
System.out.println("aaaaaaaaaa");
else
System.out.println("nnnnnnnnnn");
}catch(SQLException e){
System.out.println(e.toString());
}
我试了一下,错误:unsupported method:ResultSet.isLast。每次我在用rs的一些方法时,比如afterLast() 、first() 之类的,都会出现类似的错误。能告诉我怎么用么?
rorey_008 2007-08-09
  • 打赏
  • 举报
回复
boolean absolute(int row) 将指针移动到此 ResultSet 对象的给定行编号
void afterLast() 将指针移动到此 ResultSet 对象的末尾,正好位于最后一行之后
void beforeFirst() 将指针移动到此 ResultSet 对象的开头,正好位于第一行之前
boolean first() 将指针移动到此 ResultSet 对象的第一行
xiaomeizhun 2007-08-09
  • 打赏
  • 举报
回复
具体怎么用呢?我想把rs的指针放回到原来的位置
HenryHee 2007-08-09
  • 打赏
  • 举报
回复
boolean absolute(int row) 将指针移动到此 ResultSet 对象的给定行编号
void afterLast() 将指针移动到此 ResultSet 对象的末尾,正好位于最后一行之后
void beforeFirst() 将指针移动到此 ResultSet 对象的开头,正好位于第一行之前
boolean first() 将指针移动到此 ResultSet 对象的第一行
加载更多回复(5)

62,623

社区成员

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

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