java中ResultSet遍歷數(shù)據(jù)操作
1.查找數(shù)據(jù)庫(kù)中表的列名
<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ù)庫(kù)中表中每條記錄的列值
for(int i=1;i<rs.getMetaData().getColumnCount();i++){ str+=rs.getString(i)+","; }
補(bǔ)充知識(shí):Java:使用ResultSet.next()執(zhí)行含有rownum的SQL語(yǔ)句速度緩慢
在使用Oracle數(shù)據(jù)庫(kù)進(jìn)行分頁(yè)查詢時(shí),經(jīng)常會(huì)用到如下SQL:
select tm.* from (select rownum rm, t.* xmlrecord from testtable t) tm where tm.rm > ? and tm.rm <= ?
使用的java代碼如下:
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í),本來(lái)希望返回false后跳出循環(huán),但rs.next()會(huì)執(zhí)行非常長(zhǎng)的時(shí)間。解決的方法是不讓rs.next()來(lái)判斷下一條記錄不存在,而在代碼通過(guò)計(jì)數(shù)來(lái)實(shí)現(xiàn):
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í)行很長(zhǎng)時(shí)間:
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í)行很長(zhǎng)時(shí)間,可能是由于取不到一個(gè)完整的300條記錄造成的。解決方法是將fetchSize設(shè)置成能被count整除的數(shù)字,比如:
ps.setFetchSize(500);
在以上兩種狀況下,為什么rs.next()會(huì)執(zhí)行很長(zhǎng)時(shí)間,還不是很清楚,但可以通過(guò)上述方式解決。至于為什么會(huì)有這個(gè)問(wèn)題,有知道原因的朋友,請(qǐng)不吝賜教。
以上這篇java中ResultSet遍歷數(shù)據(jù)操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot項(xiàng)目整合Log4j2實(shí)現(xiàn)自定義日志打印失效問(wèn)題解決
這篇文章主要介紹了SpringBoot項(xiàng)目整合Log4j2實(shí)現(xiàn)自定義日志打印失效問(wèn)題解決,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01java面試常見(jiàn)問(wèn)題---ConcurrentHashMap
ConcurrentHashMap是由Segment數(shù)組結(jié)構(gòu)和HashEntry數(shù)組結(jié)構(gòu)組成。Segment的結(jié)構(gòu)和HashMap類(lèi)似,是一種數(shù)組和鏈表結(jié)構(gòu),今天給大家普及java面試常見(jiàn)問(wèn)題---ConcurrentHashMap知識(shí),一起看看吧2021-06-06springboot集成junit編寫(xiě)單元測(cè)試實(shí)戰(zhàn)
在做單元測(cè)試時(shí),代碼覆蓋率常常被拿來(lái)作為衡量測(cè)試好壞的指標(biāo),本文主要介紹了springboot集成junit編寫(xiě)單元測(cè)試實(shí)戰(zhàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02mybatis-4 mybatis與spring結(jié)合使用及原理解析
本文通過(guò)圖文并茂的形式給大家介紹了mybatis-4 mybatis與spring結(jié)合使用及原理解析,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-04-04Java page cache回寫(xiě)機(jī)制案例詳解
這篇文章主要介紹了Java page cache回寫(xiě)機(jī)制案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09解決spring cloud gateway 獲取body內(nèi)容并修改的問(wèn)題
這篇文章主要介紹了解決spring cloud gateway 獲取body內(nèi)容并修改的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12并發(fā)編程ConcurrentLinkedQueue示例詳解
這篇文章主要為大家介紹了并發(fā)編程ConcurrentLinkedQueue使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Java Calendar類(lèi)使用總結(jié)及使用實(shí)例
這篇文章主要介紹了Java Calendar類(lèi)使用總結(jié)及使用實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03