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

