下面列出了java.sql.ResultSet#updateObject ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Tests calling updateObject with a null value on all columns.
* @exception SQLException database access error. Causes test to
* fail with an error.
*/
public void testUpdateObjectWithNull()
throws SQLException
{
Statement s = createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = s.executeQuery(SELECT_STMT);
rs.next();
Object value = null;
for (int i = 1; i <= COLUMNS; i++) {
rs.updateObject(i, value);
assertNull("Expected rs.getObject(" + i + ") to be null",
rs.getObject(i));
assertTrue("Expected rs.wasNull() to return true",
rs.wasNull());
}
rs.updateRow();
rs.close();
checkColumnsAreNull();
s.close();
}
private void adjustTable(final Connection conn, final String table) throws Exception {
System.out.println("Adjusting table " + table);
Statement sta = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = sta.executeQuery("SELECT * FROM " + table + " WHERE DISPLAYVAL is not null");
while (rs.next()) {
final Object pk = rs.getObject("ATTRVALUE_ID");
final String i18n = rs.getString("DISPLAYVAL");
final String model = adjustValue(i18n);
if (model != null && !model.equals(i18n)) {
rs.updateObject("DISPLAYVAL", model);
rs.updateRow();
System.out.println("Adjusting object(" + pk + ") val: " + model);
}
}
sta.close();
conn.commit();
}
/**
* Tests calling updateObject with a null value on all columns.
* @exception SQLException database access error. Causes test to
* fail with an error.
*/
public void testUpdateObjectWithNull()
throws SQLException
{
Statement s = createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = s.executeQuery(SELECT_STMT);
rs.next();
Object value = null;
for (int i = 1; i <= COLUMNS; i++) {
rs.updateObject(i, value);
assertNull("Expected rs.getObject(" + i + ") to be null",
rs.getObject(i));
assertTrue("Expected rs.wasNull() to return true",
rs.wasNull());
}
rs.updateRow();
rs.close();
checkColumnsAreNull();
s.close();
}
/**
* Tests calling updateObject with a null value on all columns.
* @exception SQLException database access error. Causes test to
* fail with an error.
*/
public void testUpdateObjectWithNull()
throws SQLException
{
Statement s = createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = s.executeQuery(SELECT_STMT);
rs.next();
Object value = null;
for (int i = 1; i <= COLUMNS; i++) {
rs.updateObject(i, value);
assertNull("Expected rs.getObject(" + i + ") to be null",
rs.getObject(i));
assertTrue("Expected rs.wasNull() to return true",
rs.wasNull());
}
rs.updateRow();
rs.close();
checkColumnsAreNull();
s.close();
}
/**
* Calls updateObject on the given ResultSet with the given value for the
* position of this PlaceHolder.
*/
public void updateObject(Object value, ResultSet rs)
throws SQLException
{
if(value != null) {
if(isInQuery()) {
rs.updateObject(getIndex(), value);
}
} else {
updateNull(rs);
}
}
/**
* Verify fix for DERBY-5042, where updateBoolean() and updateObject()
* would fail on a BOOLEAN column when using the client driver.
*/
public void test_5042_updateBoolean() throws SQLException {
setAutoCommit(false);
Statement s = createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
s.execute("create table derby5042(b boolean, i int, c char(10))");
ResultSet rs = s.executeQuery("select * from derby5042");
// Test updateBoolean() on various column types
rs.moveToInsertRow();
rs.updateBoolean("B", true); // Used to fail with client driver
rs.updateBoolean("I", true);
rs.updateBoolean("C", true);
rs.insertRow();
// Test updateObject() with a java.lang.Boolean on various column types
rs.moveToInsertRow();
rs.updateObject("B", Boolean.FALSE); // Used to fail with client driver
rs.updateObject("I", Boolean.FALSE);
rs.updateObject("C", Boolean.FALSE);
rs.insertRow();
rs.close();
JDBC.assertFullResultSet(
s.executeQuery("select * from derby5042 order by 1,2,3"),
new String[][]{
{"false", "0", "false"},
{"true", "1", "true"}});
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertUpdateObjectForColumnIndex() throws SQLException {
for (ResultSet each : resultSets) {
each.updateObject(1, new Object());
}
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertUpdateObjectForColumnLabel() throws SQLException {
for (ResultSet each : resultSets) {
each.updateObject("label", new Object());
}
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertUpdateObjectForColumnIndexWithScaleOrLength() throws SQLException {
for (ResultSet each : resultSets) {
each.updateObject(1, new Object(), 1);
}
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertUpdateObjectForColumnLabelWithScaleOrLength() throws SQLException {
for (ResultSet each : resultSets) {
each.updateObject("label", new Object(), 1);
}
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertUpdateObjectForColumnIndex() throws SQLException {
for (ResultSet each : resultSets) {
each.updateObject(1, new Object());
}
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertUpdateObjectForColumnLabel() throws SQLException {
for (ResultSet each : resultSets) {
each.updateObject("label", new Object());
}
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertUpdateObjectForColumnIndexWithScaleOrLength() throws SQLException {
for (ResultSet each : resultSets) {
each.updateObject(1, new Object(), 1);
}
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertUpdateObjectForColumnLabelWithScaleOrLength() throws SQLException {
for (ResultSet each : resultSets) {
each.updateObject("label", new Object(), 1);
}
}
/**
Set this value into a ResultSet for a subsequent ResultSet.insertRow
or ResultSet.updateRow. This method will only be called for non-null values.
@exception SQLException thrown by the ResultSet object
@exception StandardException thrown by me accessing my value.
*/
public void setInto(ResultSet rs, int position) throws SQLException, StandardException {
rs.updateObject(position, getObject());
}
/**
Set this value into a ResultSet for a subsequent ResultSet.insertRow
or ResultSet.updateRow. This method will only be called for non-null values.
@exception SQLException thrown by the ResultSet object
@exception StandardException thrown by me accessing my value.
*/
public void setInto(ResultSet rs, int position) throws SQLException, StandardException {
rs.updateObject(position, getObject());
}