Subject Re: Cursors
Author Roman Rokytskyy
> Can you describe a little bit more how I di this?
> Because I heared "CURRENT OF my_cursor" not at first time.
> Abd how can I set Cursornames?

Here's the part of unit test case I used to test positioned updates
(note, feature is available only in JayBird 1.0.1 that will be
released next week, however, you can download pre-release from the
files section of this group):

We assume that following table and constants are defined:

CREATE TABLE test_table(id INTEGER, str VARCHAR(10))

public static final String INSERT_INTO_TABLE_STATEMENT =
"INSERT INTO test_table VALUES(?, ?)";
public static final String CURSOR_NAME = "some_cursor";
public static final String UPDATE_TABLE_STATEMENT =
"UPDATE test_table SET str = ? WHERE CURRENT OF " + CURSOR_NAME;


/**
* Test if positioned updates work correctly.
*
* @throws java.lang.Exception if something went wrong.
*/
public void testPositionedUpdate() throws Exception {
int recordCount = 10;

PreparedStatement ps =
connection.prepareStatement(INSERT_INTO_TABLE_STATEMENT);

try {
for(int i = 0; i < recordCount; i++) {
ps.setInt(1, i);
ps.setInt(2, i);
ps.executeUpdate();
}
} finally {
ps.close();
}

connection.setAutoCommit(false);

ResultSet rs;
Statement select = connection.createStatement();
select.setCursorName(CURSOR_NAME);
try {
rs = select.executeQuery(
"SELECT id, str FROM test_table FOR UPDATE OF " + CURSOR_NAME);

assertTrue("Cursor name should be correct",
CURSOR_NAME.equals(rs.getCursorName()));

assertTrue("ResultSet.isBeforeFirst() should be true.",
rs.isBeforeFirst());

PreparedStatement update = connection.prepareStatement(
UPDATE_TABLE_STATEMENT);

try {
int counter = 0;

while (rs.next()) {

if (counter == 0) {
assertTrue("ResultSet.isFirst() should be true",
rs.isFirst());
} else
if (counter == recordCount - 1) {
try {
rs.isLast();
assertTrue("ResultSet.isLast() should be true",
false);
} catch(SQLException ex) {
// correct
}
}

counter++;

assertTrue("ResultSet.getRow() should be correct",
rs.getRow() == counter);

update.setInt(1, rs.getInt(1) + 1);
int updatedCount = update.executeUpdate();

assertTrue("Number of update rows should be 1, is " + updatedCount,
updatedCount == 1);
}

assertTrue("ResultSet.isAfterLast() should be true",
rs.isAfterLast());

} finally {
update.close();
}

} finally {
select.close();
}

try {
rs.close();
assertTrue(
"Result set should be closed after statemnet close", false);
} catch(SQLException ex) {
// everything is ok
}

connection.commit();

select = connection.createStatement();
try {
rs = select.executeQuery("SELECT id, str FROM test_table");

int counter = 0;

while (rs.next()) {

if (counter == 0) {
assertTrue("ResultSet.isFirst() should be true",
rs.isFirst());
} else
if (counter == recordCount - 1) {
assertTrue("ResultSet.isLast() should be true",
rs.isLast());
}

counter++;

int idValue = rs.getInt(1);
int strValue = rs.getInt(2);

assertTrue("Value of str column must be equal to id + 1, " +
"idValue = " + idValue + ", strValue = " + strValue,
strValue == (idValue + 1));
}
} catch(Exception e) {
e.printStackTrace();
throw e;
} finally {
select.close();
}
}


Best regards,
Roman Rokytskyy