[Java] JDBC ResultSet/RowSet 取得查詢結果的總列數

常常需要得到查詢結果返回的總列數,
Java提供以下三種解法:



1.Using sql
使用SQL COUNT方法達到


// Get a record count with the SQL Statement
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) AS rowcount FROM 
emp");
rs.next();

// Get the rowcount column value.
int ResultCount = rs.getInt(rowcount) ;
rs.close() ;

2.Using JDBC Scrollable ResultSet:
需注意需將rs.last()一定要執行才有辦法透過rs.getRow()取到總列數,
如果要取重新retrieve資料集可使用rs.beforeFirst()回去!!
..............

sqlString = "SELECT * FROM emp";

// Create a scrollable ResultSet.
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sqlString);

// Point to the last row in resultset.
rs.last(); 

// Get the row position which is also the number of rows in the ResultSet.
int rowcount = rs.getRow(); 

System.out.println("Total rows for the query: "+rowcount);


// Reposition at the beginning of the ResultSet to take up rs.next() call.
rs.beforeFirst();
................

3.Using Oracle JDBC Cached RowSet

.........................
ResultSet rs = null;
........................

// Create and initialize Cached RowSet object.
OracleCachedRowSet ocrs = new OracleCachedRowSet(); 

// Create a string that has the SQL statement that gets all the records.
String sqlString = "SELECT empno FROM emp";


// Create a statement, resultset objects.
stmt = conn.createStatement();
rs = stmt.executeQuery(sqlString);

// Populate the Cached RowSet using the above Resultset.
ocrs.populate(rs);

// Point to the last row in Cached RowSet.
ocrs.last(); 

// Get the row position which is also the number of rows in the Cached
// RowSet.
int rowcount = ocrs.getRow();

System.out.println("Total rows for the query using Cached RowSet: "+
rowcount); 

// Close the Cached Rowset object.

if (ocrs != null) 
ocrs.close();


秘密花園OST: Oscar - 눈물자리(眼淚星座)


오스카 - 눈물자리

어둔 밤하늘 밝게 빛나는 저 별 외로운 나를 꼭 닮아서
아픈 사랑에 후회로 가득한 슬픈 눈물 자리 됐구나

다시 되돌릴 수만 있다면 서로를 모르던 때로 돌아가
그댈 떠나보냈던 어리석은 일 절대로 두번 다시는 하지 않을 수 있는데

* 흐르는 내 눈물 저 별에 담아서 그리운 내 마음 비춰줄 수 있다면
내 슬픔 가득찬 저 별을 그대가 보고 가엾은 나를 용서해 줄텐데

다시 내게 기회를 준다면 우리 사랑했던 때로 돌아가
그댈 아프게 했던 바보같은 일 절대로 두번 다시는 하지 않을 수 있는데

* 흐르는 내 눈물 저 별에 담아서 그리운 내 마음 비춰줄 수 있다면
내 슬픔 가득찬 저 별을 그대가 보고 가엾은 나를 용서해 줄텐데

우리의 사이가 별보다 멀어져 서로의 가슴에 닿을 수가 없나봐
얼 마나 울어야 또 얼마나 가득 채워야 그대의 두눈속에 담겨질까
그대는 내 맘 알까


在黑夜裡綻放著光芒的星星,孤單的就像是妳
在悲傷的愛情中,後悔已經成了 悲傷的眼淚星座

如果能夠時光倒流,就回到我們彼此不認識的時候吧
我絕對不會,再次愚蠢的 離開了妳

* 我流淌的淚水,裝載在那顆星星,如果它能夠照亮我思念妳的心
如果妳看 見了滿是我的悲傷的那顆星星,也許就能夠原諒這可悲的我

如果妳願意給我再一次的機會,就讓我們回到彼此相愛的時候吧
我絕對不會, 再次像個傻瓜般的傷害了妳

* 我流淌的淚水,裝載在那顆星星,如果它能夠照亮我思念妳的心
如果妳看 見了滿是我的悲傷的那顆星星,也許就能夠原諒這可悲的我

我們之間的距離,已經比星星更遙遠,大概再也無法觸及妳的心了吧
究竟還得 哭多久,又得用多少眼淚,妳才能夠看見
妳,知道我的心嗎?

 

Copyright © 嘴兒嘴的生活瑣事. Template created by Volverene from Templates Block
WP by Simply WP | Solitaire Online