JDBC ResultSet 可滾動的結果集
本文主要介紹:JDBC ResultSet 可滾動的結果集,很多時候我們的項目都會有多個數據源,比如一個項目既支持PostgreSQL又支持SQLServer或者mysql,你們對于數據庫分頁則存在兼容性問題,這是如果數據量不是很到的話,采用可滾動的結果集可以實現兼容性分頁
可滾動的結果集
1)com.microsoft.sqlserver.jdbc.SQLServerException: 只進結果集不支持請求的操作。
當type設置為:ResultSet.TYPE_FORWARD_ONLY(默認)時,游標是不能任意移動的,只能逐步向前,否則會報 SQLServerException
示例:
statement = con.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_XXX);
rs = statement.executeQuery();
rs.absolute(2);
System.out.println(rs.getString("virusid")+" "+rs.getString("featurecode")+" "+rs.getString("apppackage")+" "+rs.getInt("virustype")+" "); 2)com.microsoft.sqlserver.jdbc.SQLServerException: 結果集沒有當前行。
當type設置為:ResultSet.TYPE_SCROLL_INSENSITIVE 或者 ResultSet.TYPE_SCROLL_INSENSITIVE 時,游標可以移動,但是移動的位置是[1,count],記住并不是從0開始,否則會報錯。
既然可以移動,那么把移動的幾個方法解釋一下:
rs = statement.executeQuery(); 游標指向第一行前面的位置,這個位置是不能獲取數據,否則報錯:結果集沒有當前行
rs.next(); // 游標下移一個位置,如果所在位置有結果集那么返回true,否則返回false
rs.previous(); // 游標上移一個位置,如果所在位置有結果集那么返回true,否則返回false
rs.first(); // 游標指向第一行的位置
rs.last(); // 游標指向最后一行的位置
rs.beforeFirst(); // 游標指向第一行前面的位置 , 這個位置不能獲取數據
rs.afterLast(); // 游標指向最后一行后面的位置,這個位置不能獲取數據
rs.absolute(index); // 游標移動至index位置,index是[1,count]的任意數字,但是不能超出,否則報錯
rs.relative(index); // 游標從當前位置算移動index個位置,也就是相對移動,index可以是負數,但是計算結果同樣在[1,count]內
isAfterLast(); // 判斷游標是否在最后一行之后。
isBeforeFirst();// 判斷游標是否在第一行之前。
ifFirst() ; //判斷游標是否指向結果集的第一行。
isLast(); // 判斷游標是否指向結果集的最后一行。
getRow();// 得到當前游標所指向行的行號,行號從1開始,如果結果集沒有行,返回0。
示例:
statement = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_XXX);
rs = statement.executeQuery();
rs.absolute(1);
System.out.println(rs.getString("virusid")+" "+rs.getString("featurecode")+" "+rs.getString("apppackage")+" "+rs.getInt("virustype")+" "); 3)ResultSet.TYPE_SCROLL_INSENSITIVE 與 ResultSet.TYPE_SCROLL_INSENSITIVE 的差異
很多人說:前者是對數據庫中數據發生變化不敏感,后者是敏感的。可是我用SQLServer2005測試的時候,真沒看出來二者之間的差異,如果有人看到可以貼出代碼來,互相學習。。。。。
另外,開發過程中也很少使用,我想TYPE_SCROLL_INSENSITIVE 已經足夠了。
注意:
運行前腳本:
IF NOT EXISTS(SELECT * FROM sysobjects WHERE id = object_id('Tbl_VirusBaseInfo') AND type = 'U')
BEGIN
CREATE TABLE Tbl_VirusBaseInfo (
uidVirusID varchar(48) not null,
strFeatureCode varchar(48) null,
strAppPackage varchar(512) null,
iVirusType int null,
CONSTRAINT PK_VirusBaseInfo PRIMARY KEY (uidVirusID))
END
Gopublic static Connection getConnection(){
Connection con = null;
try {
String username = "sa"; // 用戶名
String password = "123"; // 密碼
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");// 加載驅動類
con = DriverManager.getConnection(
"jdbc:sqlserver://127.0.0.1:1433;DatabaseName=TestDB",username, password);// 獲取連接
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
} @Test
public void resultSetTest(){
Connection con = getConnection();
String sql = "SELECT uidvirusid,strfeaturecode,strapppackage,ivirustype FROM Tbl_VirusBaseInfo";
PreparedStatement statement = null;
ResultSet rs = null;
try {
statement = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = statement.executeQuery();
rs.absolute(3);
System.out.println(rs.getString("uidvirusid")+" "+rs.getString("strfeaturecode"));
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(rs != null){
rs.close();
}
if(statement != null){
statement.close();
}
if(con != null){
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}</strong></pre> <p></p>