激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

香港云服务器
服務(wù)器之家 - 編程語言 - Java教程 - java中ResultSet遍歷數(shù)據(jù)操作

java中ResultSet遍歷數(shù)據(jù)操作

2020-08-16 00:02liuyunshengsir Java教程

這篇文章主要介紹了java中ResultSet遍歷數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

1.查找數(shù)據(jù)庫中表的列名

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<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.查找數(shù)據(jù)庫中表中每條記錄的列值

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

補(bǔ)充知識(shí):Java:使用ResultSet.next()執(zhí)行含有rownum的SQL語句速度緩慢

在使用Oracle數(shù)據(jù)庫進(jìn)行分頁查詢時(shí),經(jīng)常會(huì)用到如下SQL:

select tm.* from (select rownum rm, t.* xmlrecord from testtable t) tm where tm.rm > ? and tm.rm <= ?

使用的java代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int startIdx = 0;
int endIdx = 10000;
String sql = "select tm.* from (select rownum rm, t.* xmlrecord from testtable t) tm where tm.rm > ? and tm.rm <= ?";
 
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);
 }
 }
}

當(dāng)使用以上代碼時(shí),會(huì)發(fā)現(xiàn)當(dāng)取完最后一條記錄后,再執(zhí)行rs.next()時(shí),本來希望返回false后跳出循環(huán),但rs.next()會(huì)執(zhí)行非常長的時(shí)間。解決的方法是不讓rs.next()來判斷下一條記錄不存在,而在代碼通過計(jì)數(shù)來實(shí)現(xiàn):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 > ? and tm.rm <= ?";
 
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;
  }
 }
 }
}

如果代碼中設(shè)置了fetchSize,并且fetchSize不能被count整除時(shí),在取最后一片數(shù)據(jù)時(shí),rs.next()也會(huì)執(zhí)行很長時(shí)間:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int startIdx = 0;
int endIdx = 10000;
String sql = "select tm.* from (select rownum rm, t.* xmlrecord from testtable t) tm where tm.rm > ? and tm.rm <= ?";
 
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);
 }
 }
}

以上代碼中,當(dāng)取得9900條數(shù)據(jù)后,再取下一個(gè)300條時(shí),rs.next()就會(huì)執(zhí)行很長時(shí)間,可能是由于取不到一個(gè)完整的300條記錄造成的。解決方法是將fetchSize設(shè)置成能被count整除的數(shù)字,比如:

ps.setFetchSize(500);

在以上兩種狀況下,為什么rs.next()會(huì)執(zhí)行很長時(shí)間,還不是很清楚,但可以通過上述方式解決。至于為什么會(huì)有這個(gè)問題,有知道原因的朋友,請(qǐng)不吝賜教。

以上這篇java中ResultSet遍歷數(shù)據(jù)操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/liuyunshengsir/article/details/50618494

延伸 · 閱讀

精彩推薦
2283
主站蜘蛛池模板: www.成人在线视频 | 在线播放亚洲 | 中文字幕在线播放不卡 | 黄色的视频在线观看 | 亚洲最新黄色网址 | 97香蕉超级碰碰久久免费软件 | 亚洲精品7777 | 亚洲五码在线观看视频 | 免费a级毛片永久免费 | 久久久国产精品免费观看 | 欧洲精品色 | 久久aⅴ国产欧美74aaa | 日韩在线视频免费播放 | 性欧美大战久久久久久久免费观看 | 操操影视 | 欧美大荫蒂xxx | 黄色大片网站在线观看 | 又黄又爽免费无遮挡在线观看 | 黄色片网站免费在线观看 | 精品在线视频观看 | 九九热视频这里只有精品 | 香蕉视频1024| 久久天| 国产1区2区3区中文字幕 | 色网在线视频 | 中国av免费观看 | 久久看免费视频 | 91精彩在线| 国产精品一区二区手机在线观看 | 一级毛片电影院 | 欧美视频不卡 | 午夜视频导航 | 免费国产视频在线观看 | 久久99国产精品久久99果冻传媒 | 91www成人久久 | 精品一区二区三区欧美 | 国产免费观看视频 | 色爱99 | 亚洲视频在线免费看 | 成人影片在线免费观看 | 天天干导航 |