java中如何使用ResultSet实现遍历数据

发布时间:2020-11-09 15:55:59 作者:Leah
来源:亿速云 阅读:2646

这篇文章运用简单易懂的例子给大家介绍java中如何使用ResultSet实现遍历数据,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

1.查找数据库中表的列名

<pre name="code" class="html">String sql = "select *from tblmetadatainfo";
 ResultSet rs = MySqlHelper.executeQuery(sql, null);
 String str="";
 try {
  ResultSetMetaData rsmd = rs.getMetaData();
  for (int i = 1; i < rsmd.getColumnCount(); i++) {
  str+=rsmd.getColumnName(i)+",";
  }
  str=str.substring(0, str.length()-1);
 } catch (SQLException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }

2.查找数据库中表中每条记录的列值

for(int i=1;i<rs.getMetaData().getColumnCount();i++){
   str+=rs.getString(i)+",";
  }

补充知识:Java:使用ResultSet.next()执行含有rownum的SQL语句速度缓慢

在使用Oracle数据库进行分页查询时,经常会用到如下SQL:

select tm.* from (select rownum rm, t.* xmlrecord from testtable t) tm where tm.rm > &#63; and tm.rm <= &#63;

使用的java代码如下:

int startIdx = 0;
int endIdx = 10000; 
String sql = "select tm.* from (select rownum rm, t.* xmlrecord from testtable t) tm where tm.rm > &#63; and tm.rm <= &#63;";
 
try (Connection conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql);) {
 
 ps.setInt(1, startIdx);
 ps.setInt(2, endIdx);
 
 try (ResultSet rs = ps.executeQuery();) {
 while (rs.next()) {
  String id = rs.getString(2);
 
  System.out.println("id="+id);
 }
 }
}

当使用以上代码时,会发现当取完最后一条记录后,再执行rs.next()时,本来希望返回false后跳出循环,但rs.next()会执行非常长的时间。解决的方法是不让rs.next()来判断下一条记录不存在,而在代码通过计数来实现:

int startIdx = 0;
int endIdx = 10000;
int i = 0;
int count = endIdx - startIdx;
String sql = "select tm.* from (select rownum rm, t.* xmlrecord from testtable t) tm where tm.rm > &#63; and tm.rm <= &#63;";
 
try (Connection conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql);) {
 
 ps.setInt(1, startIdx);
 ps.setInt(2, endIdx);
 
 try (ResultSet rs = ps.executeQuery();) {
  while (rs.next()) {
  i++;
  String id = rs.getString(2); 
  System.out.println("id="+id);
   if(i == count) {
   break;
  }
 }
 }
}

如果代码中设置了fetchSize,并且fetchSize不能被count整除时,在取最后一片数据时,rs.next()也会执行很长时间:

int startIdx = 0;
int endIdx = 10000; 
String sql = "select tm.* from (select rownum rm, t.* xmlrecord from testtable t) tm where tm.rm > &#63; and tm.rm <= &#63;";
 
try (Connection conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql);) {
 ps.setFetchSize(300);
 ps.setInt(1, startIdx);
 ps.setInt(2, endIdx);
 
 try (ResultSet rs = ps.executeQuery();) {
 
 while (rs.next()) {
  String id = rs.getString(2);
 
  System.out.println("id="+id);
 }
 }
}

以上代码中,当取得9900条数据后,再取下一个300条时,rs.next()就会执行很长时间,可能是由于取不到一个完整的300条记录造成的。解决方法是将fetchSize设置成能被count整除的数字,比如:

ps.setFetchSize(500);

在以上两种状况下,为什么rs.next()会执行很长时间,还不是很清楚,但可以通过上述方式解决。至于为什么会有这个问题,有知道原因的朋友,请不吝赐教。

关于java中如何使用ResultSet实现遍历数据就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. 怎么在Java5中遍历HashMap数据
  2. Java中如何实现Map集合遍历

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java resultset 遍历

上一篇:Spring Jpa如何配置一个多数据源工程

下一篇:深入浅析Redis数据库中处理高并发的原理

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》