下面列出了java.sql.ResultSet#absolute ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Advance the cursor to the first required row of the <tt>ResultSet</tt>
*/
private void advance(final ResultSet rs, final RowSelection selection) throws SQLException {
final int firstRow = LimitHelper.getFirstRow( selection );
if ( firstRow != 0 ) {
if ( getFactory().getSessionFactoryOptions().isScrollableResultSetsEnabled() ) {
// we can go straight to the first required row
rs.absolute( firstRow );
}
else {
// we need to step through the rows one row at a time (slow)
for ( int m = 0; m < firstRow; m++ ) {
rs.next();
}
}
}
}
/**
* Advance the cursor to the first required row of the <tt>ResultSet</tt>
*/
protected void advance(final ResultSet rs, final RowSelection selection) throws SQLException {
final int firstRow = LimitHelper.getFirstRow( selection );
if ( firstRow != 0 ) {
if ( getFactory().getSettings().isScrollableResultSetsEnabled() ) {
// we can go straight to the first required row
rs.absolute( firstRow );
}
else {
// we need to step through the rows one row at a time (slow)
for ( int m = 0; m < firstRow; m++ ) {
rs.next();
}
}
}
}
/**
* DERBY-1481 - ResultSet.beforeFirst() gives protocol error on scrollable,
* updatable result sets that are downgraded to read-only
*
* Check that no exception is thrown when calling positioning methods on a
* result set that has been downgraded to read-only.
*
*/
public void testDowngradeToScrollReadOnly() throws SQLException {
Statement s = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = s.executeQuery("select * from t1 order by b");
// check that the ResultSet was downgraded
assertWarning(rs.getWarnings(),
QUERY_NOT_QUALIFIED_FOR_UPDATABLE_RESULTSET);
// call positioning methods
rs.next();
rs.next();
rs.previous();
rs.relative(1);
rs.absolute(3);
rs.relative(-1);
rs.first();
rs.last();
rs.beforeFirst();
rs.afterLast();
// close result set and statement
rs.close();
s.close();
}
public boolean absolute( ResultSet rs, int position ) throws HopDatabaseException {
try {
return rs.absolute( position );
} catch ( SQLException e ) {
throw new HopDatabaseException( "Unable to move resultset to position " + position, e );
}
}
/**
* Tests that the code path for LOB locator release works fine for result
* sets without LOBs.
*
* @throws SQLException if the test fails for some reason
*/
public void testNoLOBs()
throws SQLException {
// Test a forward only result set, with autocommit.
Statement stmt = createStatement();
ResultSet rs = stmt.executeQuery("select * from sys.systables");
while (rs.next()) {
// Do nothing, just iterate through.
}
rs.close();
// Basic test checking that the scrollable result code path works.
stmt = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
getConnection().setAutoCommit(false);
rs = stmt.executeQuery("select * from sys.systables");
rs.absolute(3);
while (rs.next()) {
// Do nothing, just iterate through.
}
// Just navigate randomly.
rs.previous();
rs.absolute(2);
rs.relative(2);
rs.afterLast();
rs.first();
rs.next();
rs.last();
rs.beforeFirst();
// Close the statement instead of the result set first.
stmt.close();
rs.close();
rollback();
}
protected void assertResultSetLength(ResultSet rset, int len) throws Exception {
int oldRowPos = rset.getRow();
rset.last();
assertEquals("Result set length", len, rset.getRow());
if (oldRowPos > 0) {
rset.absolute(oldRowPos);
} else {
rset.beforeFirst();
}
}
protected void assertResultSetLength(ResultSet rset, int len) throws Exception {
int oldRowPos = rset.getRow();
rset.last();
assertEquals("Result set length", len, rset.getRow());
if (oldRowPos > 0) {
rset.absolute(oldRowPos);
} else {
rset.beforeFirst();
}
}
/**
* Update a random sample of n records in the resultset
* @param rs result set to be updated
* @param rows map of rows, will also be updated
* @param updatedRows set of being updated (position in RS)
* @param k number of records to be updated
*/
private void updateRandomSampleOfNRecords(final ResultSet rs,
final Map rows,
final Set updatedRows,
final int k)
throws SQLException
{
List sampledKeys = createRandomSample(rows, k);
println("Sampled keys:" + sampledKeys);
ResultSetMetaData meta = rs.getMetaData();
for (Iterator i = sampledKeys.iterator(); i.hasNext();) {
Integer key = (Integer) i.next();
rs.absolute(key.intValue());
if (positioned) {
updatePositioned(rs, meta);
rs.relative(0); // If this call is not here, the old values are
// returned in rs.getXXX calls
} else {
updateRow(rs, meta);
}
// Update the rows table
rows.put(key, getRowString(rs));
// Update the updatedRows set
updatedRows.add(key);
}
}
/**
* Tests that the code path for LOB locator release works fine for result
* sets without LOBs.
*
* @throws SQLException if the test fails for some reason
*/
public void testNoLOBs()
throws SQLException {
// Test a forward only result set, with autocommit.
Statement stmt = createStatement();
ResultSet rs = stmt.executeQuery("select * from sys.systables");
while (rs.next()) {
// Do nothing, just iterate through.
}
rs.close();
// Basic test checking that the scrollable result code path works.
stmt = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
getConnection().setAutoCommit(false);
rs = stmt.executeQuery("select * from sys.systables");
rs.absolute(3);
while (rs.next()) {
// Do nothing, just iterate through.
}
// Just navigate randomly.
rs.previous();
rs.absolute(2);
rs.relative(2);
rs.afterLast();
rs.first();
rs.next();
rs.last();
rs.beforeFirst();
// Close the statement instead of the result set first.
stmt.close();
rs.close();
rollback();
}
/**
* Test that you can correctly run multiple updateXXX() + updateRow()
* combined with cancelRowUpdates().
*/
public void testMultiUpdateRow1()
throws SQLException
{
Statement s = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
s.setCursorName(getNextCursorName());
ResultSet rs = s.executeQuery("select * from t1");
rs.absolute(5);
final int oldCol2 = rs.getInt(2);
final int newCol2 = -2222;
final int oldCol3 = rs.getInt(3);
final int newCol3 = -3333;
rs.updateInt(2, newCol2);
assertEquals("Expected the resultset to be updated after updateInt",
newCol2, rs.getInt(2));
rs.cancelRowUpdates();
assertEquals("Expected updateXXX to have no effect after cancelRowUpdated",
oldCol2, rs.getInt(2));
rs.updateInt(2, newCol2);
assertEquals("Expected the resultset to be updated after updateInt",
newCol2, rs.getInt(2));
assertTrue("Expected rs.rowUpdated() to be false before updateRow",
!rs.rowUpdated());
rs.updateRow();
assertTrue("Expected rs.rowUpdated() to be true after updateRow",
rs.rowUpdated());
assertEquals("Expected the resultset detect the updates of previous " +
"updateRow", newCol2, rs.getInt(2));
rs.updateInt(3, newCol3);
assertEquals("Expected the resultset to be updated after updateInt",
newCol3, rs.getInt(3));
assertEquals("Expected the resultset detect the updates of previous " +
"updateRow", newCol2, rs.getInt(2));
rs.cancelRowUpdates();
assertEquals("Expected updateXXX to have no effect after " +
"cancelRowUpdated", oldCol3, rs.getInt(3));
assertEquals("Expected the resultset detect the updates of previous " +
"updateRow after cancelRowUpdated", newCol2, rs.getInt(2));
rs.updateInt(3, newCol3);
rs.updateRow();
assertEquals("Expected the resultset to be updated after updateInt",
newCol3, rs.getInt(3));
rs.cancelRowUpdates();
assertEquals("Expected the resultset detect the updates of previous" +
"updateRow after cancelRowUpdates", newCol2, rs.getInt(2));
assertEquals("Expected the resultset detect the updates of previous" +
"updateRow after cancelRowUpdates", newCol3, rs.getInt(3));
assertTrue("Expected rs.rowUpdated() to be true after " +
"updateRow and cancelRowUpdates", rs.rowUpdated());
rs.close();
s.close();
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertAbsolute() throws SQLException {
for (ResultSet each : resultSets) {
each.absolute(1);
}
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertAbsolute() throws SQLException {
for (ResultSet each : resultSets) {
each.absolute(1);
}
}
/**
* Test that you can correctly run multiple updateXXX() + updateRow()
* combined with cancelRowUpdates().
*/
public void testMultiUpdateRow1()
throws SQLException
{
Statement s = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
s.setCursorName(getNextCursorName());
ResultSet rs = s.executeQuery("select * from t1");
rs.absolute(5);
final int oldCol2 = rs.getInt(2);
final int newCol2 = -2222;
final int oldCol3 = rs.getInt(3);
final int newCol3 = -3333;
rs.updateInt(2, newCol2);
assertEquals("Expected the resultset to be updated after updateInt",
newCol2, rs.getInt(2));
rs.cancelRowUpdates();
assertEquals("Expected updateXXX to have no effect after cancelRowUpdated",
oldCol2, rs.getInt(2));
rs.updateInt(2, newCol2);
assertEquals("Expected the resultset to be updated after updateInt",
newCol2, rs.getInt(2));
assertTrue("Expected rs.rowUpdated() to be false before updateRow",
!rs.rowUpdated());
rs.updateRow();
assertTrue("Expected rs.rowUpdated() to be true after updateRow",
rs.rowUpdated());
assertEquals("Expected the resultset detect the updates of previous " +
"updateRow", newCol2, rs.getInt(2));
rs.updateInt(3, newCol3);
assertEquals("Expected the resultset to be updated after updateInt",
newCol3, rs.getInt(3));
assertEquals("Expected the resultset detect the updates of previous " +
"updateRow", newCol2, rs.getInt(2));
rs.cancelRowUpdates();
assertEquals("Expected updateXXX to have no effect after " +
"cancelRowUpdated", oldCol3, rs.getInt(3));
assertEquals("Expected the resultset detect the updates of previous " +
"updateRow after cancelRowUpdated", newCol2, rs.getInt(2));
rs.updateInt(3, newCol3);
rs.updateRow();
assertEquals("Expected the resultset to be updated after updateInt",
newCol3, rs.getInt(3));
rs.cancelRowUpdates();
assertEquals("Expected the resultset detect the updates of previous" +
"updateRow after cancelRowUpdates", newCol2, rs.getInt(2));
assertEquals("Expected the resultset detect the updates of previous" +
"updateRow after cancelRowUpdates", newCol3, rs.getInt(3));
assertTrue("Expected rs.rowUpdated() to be true after " +
"updateRow and cancelRowUpdates", rs.rowUpdated());
rs.close();
s.close();
}
/**
* Test that you can correctly run multiple updateNull() + updateRow()
* combined with cancelRowUpdates().
*/
public void testMultiUpdateRow2()
throws SQLException
{
Statement s = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
s.setCursorName(getNextCursorName());
ResultSet rs = s.executeQuery("select * from t1");
rs.absolute(5);
final int oldCol2 = rs.getInt(2);
final int oldCol3 = rs.getInt(3);
rs.updateNull(2);
assertEquals("Expected the resultset to be updated after updateNull",
0, rs.getInt(2));
assertTrue("Expected wasNull to be true after updateNull", rs.wasNull());
rs.cancelRowUpdates();
assertEquals("Expected updateXXX to have no effect after cancelRowUpdated",
oldCol2, rs.getInt(2));
rs.updateNull(2);
assertEquals("Expected the resultset to be updated after updateNull",
0, rs.getInt(2));
assertTrue("Expected wasNull to be true after updateNull", rs.wasNull());
assertTrue("Expected rs.rowUpdated() to be false before updateRow",
!rs.rowUpdated());
rs.updateRow();
assertTrue("Expected rs.rowUpdated() to be true after updateRow",
rs.rowUpdated());
assertEquals("Expected the resultset detect the updates of previous " +
"updateRow", 0, rs.getInt(2));
rs.updateNull(3);
assertEquals("Expected the resultset to be updated after updateNull",
0, rs.getInt(3));
assertTrue("Expected wasNull to be true after updateNull", rs.wasNull());
assertEquals("Expected the resultset detect the updates of previous " +
"updateRow", 0, rs.getInt(2));
rs.cancelRowUpdates();
assertEquals("Expected updateXXX to have no effect after " +
"cancelRowUpdated", oldCol3, rs.getInt(3));
assertEquals("Expected the resultset detect the updates of previous " +
"updateRow after cancelRowUpdated", 0, rs.getInt(2));
rs.updateNull(3);
rs.updateRow();
assertEquals("Expected the resultset to be updated after updateNull",
0, rs.getInt(3));
rs.cancelRowUpdates();
assertEquals("Expected the resultset detect the updates of previous" +
"updateRow after cancelRowUpdates", 0, rs.getInt(2));
assertEquals("Expected the resultset detect the updates of previous" +
"updateRow after cancelRowUpdates", 0, rs.getInt(3));
assertTrue("Expected rs.rowUpdated() to be true after " +
"updateRow and cancelRowUpdates", rs.rowUpdated());
rs.close();
s.close();
}
/**
* Test that you can correctly run multiple updateNull() + updateRow()
* combined with cancelRowUpdates().
*/
public void testMultiUpdateRow2()
throws SQLException
{
Statement s = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
s.setCursorName(getNextCursorName());
ResultSet rs = s.executeQuery("select * from t1");
rs.absolute(5);
final int oldCol2 = rs.getInt(2);
final int oldCol3 = rs.getInt(3);
rs.updateNull(2);
assertEquals("Expected the resultset to be updated after updateNull",
0, rs.getInt(2));
assertTrue("Expected wasNull to be true after updateNull", rs.wasNull());
rs.cancelRowUpdates();
assertEquals("Expected updateXXX to have no effect after cancelRowUpdated",
oldCol2, rs.getInt(2));
rs.updateNull(2);
assertEquals("Expected the resultset to be updated after updateNull",
0, rs.getInt(2));
assertTrue("Expected wasNull to be true after updateNull", rs.wasNull());
assertTrue("Expected rs.rowUpdated() to be false before updateRow",
!rs.rowUpdated());
rs.updateRow();
assertTrue("Expected rs.rowUpdated() to be true after updateRow",
rs.rowUpdated());
assertEquals("Expected the resultset detect the updates of previous " +
"updateRow", 0, rs.getInt(2));
rs.updateNull(3);
assertEquals("Expected the resultset to be updated after updateNull",
0, rs.getInt(3));
assertTrue("Expected wasNull to be true after updateNull", rs.wasNull());
assertEquals("Expected the resultset detect the updates of previous " +
"updateRow", 0, rs.getInt(2));
rs.cancelRowUpdates();
assertEquals("Expected updateXXX to have no effect after " +
"cancelRowUpdated", oldCol3, rs.getInt(3));
assertEquals("Expected the resultset detect the updates of previous " +
"updateRow after cancelRowUpdated", 0, rs.getInt(2));
rs.updateNull(3);
rs.updateRow();
assertEquals("Expected the resultset to be updated after updateNull",
0, rs.getInt(3));
rs.cancelRowUpdates();
assertEquals("Expected the resultset detect the updates of previous" +
"updateRow after cancelRowUpdates", 0, rs.getInt(2));
assertEquals("Expected the resultset detect the updates of previous" +
"updateRow after cancelRowUpdates", 0, rs.getInt(3));
assertTrue("Expected rs.rowUpdated() to be true after " +
"updateRow and cancelRowUpdates", rs.rowUpdated());
rs.close();
s.close();
}
/**
* Test that when doing an update immediately after
* a commit, the update fails, because the cursor has been
* postioned between the current row and the next row.
* If the cursor gets repositioned, it allows an update.
* @param positioned true to use positioned update, otherwise use
* ResultSet.updateRow()
* @param resultSetType type of result set (as in ResultSet.getType())
*/
private void testCursorStateAfterCommit(final boolean positioned,
final int resultSetType)
throws SQLException
{
final Statement s = createStatement(resultSetType,
ResultSet.CONCUR_UPDATABLE);
final String cursorName = getNextCursorName();
s.setCursorName(cursorName);
final ResultSet rs = s.executeQuery("select a from t1");
final int recordToUpdate = 5;
if (resultSetType==ResultSet.TYPE_FORWARD_ONLY) {
for (int i = 0; i < recordToUpdate; i++) {
rs.next();
}
} else {
rs.absolute(recordToUpdate);
}
commit();
PreparedStatement ps =
prepareStatement("update t1 set a=? where current of " +
cursorName);
// First: check that we get an exception on update without repositioning:
try {
if (positioned) {
ps.setInt(1, -1);
ps.executeUpdate();
fail("Expected exception to be thrown on positioned update " +
"since cursor is not positioned");
} else {
rs.updateInt(1, -1);
rs.updateRow();
fail("Expected exception to be thrown on updateRow() since " +
"cursor is not positioned");
}
} catch (SQLException e) {
assertSQLState("Unexpected SQLState when updating row after commit",
SQLStateConstants.INVALID_CURSOR_STATE_NO_SUBCLASS,
e);
}
// Check that we after a repositioning can update:
if (resultSetType==ResultSet.TYPE_FORWARD_ONLY) {
rs.next();
} else {
rs.relative(0);
}
if (positioned) {
ps.setInt(1, -1);
ps.executeUpdate();
} else {
rs.updateInt(1, -1);
rs.updateRow();
}
s.close();
ps.close();
}
public static int countRecords(ResultSet resultSet)
{
int rowCount = 0;
//Determine rs size
if (resultSet != null)
{
try
{
int currentIndex = resultSet.getRow();
//Go to the end and get that row number
if (resultSet.last())
{
rowCount = resultSet.getRow();
}
//Put the cursor back
if (currentIndex > 0)
{
resultSet.absolute(currentIndex);
}
else
{
resultSet.beforeFirst();
}
}
catch (SQLException ex)
{
//TODO: Decide whether if absolute() not be supported, try first() as a last resort??
try
{
resultSet.first();
}
catch (SQLException se)
{
//we won't try anymore.
}
}
}
return rowCount;
}
/**
* Tests a sequence of operations on a scrollable, updatable resultset.
*
* @throws SQLException if the test fails
*/
public void testScrollableUpdateWithLocators()
throws SQLException {
getConnection().setAutoCommit(false);
Statement stmt = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(
"select dBlob, dClob from LOBLOC_NO_NULLS");
rs.absolute(3);
Clob c1 = rs.getClob(2);
final int origLength = (int)c1.length();
final String origContent = c1.getSubString(1, origLength);
// Do a change
c1.setString(origLength, "FIRSTPASS");
rs.absolute(7);
rs.next();
// Move back to row 3
rs.absolute(3);
Clob c2 = rs.getClob(2);
assertEquals(origContent, c2.getSubString(1, (int)c2.length()));
rs.updateRow(); // Should be a no-op
rs.absolute(3);
// Expect this to fail if the restriction that LOB columns cannot be
// accessed more than once is enforced.
Clob c3 = rs.getClob(2);
assertEquals(origContent, c3.getSubString(1, (int)c3.length()));
rs.previous();
rs.next();
Clob c4 = rs.getClob(2);
final String newContent = "THIS IS THE NEW VALUE!";
c4.setString(1, newContent);
rs.updateClob(2, c4);
rs.updateRow();
c4.setString(1, "THIS IS NOT NOT NOT THE NEW VALUE!");
rs.updateRow();
rs.next();
rs.absolute(3);
Clob c5 = rs.getClob(2);
assertEquals(newContent, c5.getSubString(1, (int)c5.length()));
rollback();
assertInvalid(c1);
assertInvalid(c2);
assertInvalid(c3);
assertInvalid(c4);
assertInvalid(c5);
}
protected boolean updateScrollableRsTx(ResultSet rs, int tid, int totalRows) {
try {
boolean success;
if (totalRows == 0) {
return true;
}
if (totalRows == 1) {
rs.first();
return updateURSRowTx(rs, tid);
} else {
int firstHalf = totalRows / 2;
// second half rows to be updated
rs.absolute(-(totalRows - firstHalf));
success = updateURSRowTx(rs, tid);
if (!success)
return success;
// if any one operation failed,
// need to restart the tx as product automatically rollback the tx
while (rs.next()) {
success = updateURSRowTx(rs, tid);
if (!success)
return success;
}
// first half rows to be updated
rs.absolute(firstHalf);
success = updateURSRowTx(rs, tid);
if (!success)
return success;
while (rs.previous()) {
success = updateURSRowTx(rs, tid);
if (!success)
return success;
}
rs.last();
if (random.nextInt(100) == 0) {
if (rs.relative(-1))
success = deleteURSRowTx(rs);
if (!success)
return success;
}
}
} catch (SQLException se) {
SQLHelper.handleSQLException(se);
// should not get conflict for moving position
// as it acquires only update read lock
}
return true;
}
/**
* Position on the specified row of the specified ResultSet.
*
* @param rs The specified ResultSet.
* @param row The row # to move to.
* (Negative means from the end of the result set.)
*
* @return NULL.
*
* @exception SQLException thrown on error.
* (absolute() not supported pre-JDBC2.0)
*/
ijResult absolute(ResultSet rs, int row)
throws SQLException
{
checkScrollableCursor(rs, "ABSOLUTE");
// 0 is an *VALID* value for row
return new ijRowResult(rs, rs.absolute(row));
}