filteredRowSet.insertRow();
filteredRowSet.moveToCurrentRow();
}
public void testFilter_Insert() throws Exception {
FilteredRowSet filteredRowSet = newFilterRowSet();
rs = st.executeQuery("SELECT * FROM USER_INFO");
filteredRowSet.populate(rs);
Predicate range = new RangeOne();
filteredRowSet.setFilter(range);
/*
* Insert a row. when call updateXXX(), evaluate(Object value, int
* column) is called to check first.
*/
filteredRowSet.afterLast();
filteredRowSet.moveToInsertRow();
filteredRowSet.updateInt(1, 200);
try {
filteredRowSet.updateString("NAME", "test200");
fail("should throw SQLException");
} catch (SQLException e) {
filteredRowSet.updateString("NAME", "insert200");
}
filteredRowSet.insertRow();
filteredRowSet.moveToCurrentRow();
/*
* Although the new row is inserted, it is invalid through
* evaluate(RowSet rs). Therefore, the inserted row is not visible.
*/
filteredRowSet.beforeFirst();
int index = 0;
while (filteredRowSet.next()) {
index++;
assertEquals(index + 1, filteredRowSet.getInt(1));
}
assertEquals(3, index);
/*
* Remove filter. See the inserted row. Then set again, and commit to
* database.
*/
filteredRowSet.setFilter(null);
assertTrue(filteredRowSet.last());
assertEquals(200, filteredRowSet.getInt(1));
assertTrue(filteredRowSet.rowInserted());
filteredRowSet.setFilter(range);
filteredRowSet.acceptChanges(conn);
// check database: the inserted row isn't commited to database
rs = st.executeQuery("SELECT * FROM USER_INFO");
index = 0;
while (rs.next()) {
index++;
assertEquals(index, rs.getInt(1));
}
assertEquals(4, index);
/*
* Remove filter
*/
filteredRowSet.setFilter(null);
filteredRowSet.beforeFirst();
index = 0;
while (filteredRowSet.next()) {
index++;
if (index == 5) {
/*
* Though the new row isn't inserted into database, the inserted
* row lost it's status after acceptChanges().
*/
assertEquals(200, filteredRowSet.getInt(1));
assertFalse(filteredRowSet.rowInserted());
} else {
assertEquals(index, filteredRowSet.getInt(1));
}
}
assertEquals(5, index);
}