下面列出了java.sql.ResultSet#clearWarnings ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
Print information about the SQL warnings for the ResultSet
to the given PrintWriter.
Walk the list of exceptions, if any.
@param out the place to write to
@param rs the ResultSet that may have warnings on it
*/
static public int /* GemStoneChange void */ ShowWarnings(PrintWriter out, ResultSet rs) {
try {
// GET RESULTSET WARNINGS
SQLWarning warning = null;
if (rs != null) {
return // GemStoneAddition
ShowWarnings(out, rs.getWarnings());
}
if (rs != null) {
rs.clearWarnings();
}
} catch (SQLException e) {
ShowSQLException(out, e);
}
return 0; // GemStoneAddition
}
static public void ShowWarnings(PrintStream out, ResultSet rs) {
try {
// GET RESULTSET WARNINGS
SQLWarning warning = null;
if (rs != null) {
ShowWarnings(out, rs.getWarnings());
}
if (rs != null) {
rs.clearWarnings();
}
} catch (SQLException e) {
ShowSQLException(out, e);
}
}
/**
Print information about the SQL warnings for the ResultSet
to the given PrintWriter.
Walk the list of exceptions, if any.
@param out the place to write to
@param rs the ResultSet that may have warnings on it
*/
static public int /* GemStoneChange void */ ShowWarnings(PrintWriter out, ResultSet rs) {
try {
// GET RESULTSET WARNINGS
SQLWarning warning = null;
if (rs != null) {
return // GemStoneAddition
ShowWarnings(out, rs.getWarnings());
}
if (rs != null) {
rs.clearWarnings();
}
} catch (SQLException e) {
ShowSQLException(out, e);
}
return 0; // GemStoneAddition
}
static public void ShowWarnings(PrintStream out, ResultSet rs) {
try {
// GET RESULTSET WARNINGS
SQLWarning warning = null;
if (rs != null) {
ShowWarnings(out, rs.getWarnings());
}
if (rs != null) {
rs.clearWarnings();
}
} catch (SQLException e) {
ShowSQLException(out, e);
}
}
/**
* Helper method available to all commands to dump any warnings
* associated with a ResultSet. The set of warnings is cleared
* after display.
*
* @param session The session to use for writing
* @param results The ResultSet that may, or may not, contain warnings.
*/
static public void printWarnings(Session session, ResultSet results) {
try {
SQLWarning w = results.getWarnings();
if (w != null) {
printWarnings(session, w);
results.clearWarnings();
}
}
catch (SQLException e) {
/* IGNORED */
}
}
/**
Print information about the SQL warnings for the ResultSet
to the given PrintWriter.
Walk the list of exceptions, if any.
@param out the place to write to
@param rs the ResultSet that may have warnings on it
*/
static public void ShowWarnings(PrintWriter out, ResultSet rs) {
try {
// GET RESULTSET WARNINGS
SQLWarning warning = null;
if (rs != null) {
ShowWarnings(out, rs.getWarnings());
}
if (rs != null) {
rs.clearWarnings();
}
} catch (SQLException e) {
ShowSQLException(out, e);
}
}
static public void ShowWarnings(PrintStream out, ResultSet rs) {
try {
// GET RESULTSET WARNINGS
SQLWarning warning = null;
if (rs != null) {
ShowWarnings(out, rs.getWarnings());
}
if (rs != null) {
rs.clearWarnings();
}
} catch (SQLException e) {
ShowSQLException(out, e);
}
}
@Override
public final void clearWarnings() throws SQLException {
Collection<SQLException> exceptions = new LinkedList<>();
for (ResultSet each : getResultSets()) {
try {
each.clearWarnings();
} catch (final SQLException ex) {
exceptions.add(ex);
}
}
throwSQLExceptionIfNecessary(exceptions);
}
@Test
public void assertClearWarnings() throws SQLException {
for (ResultSet each : resultSets.values()) {
assertNull(each.getWarnings());
each.clearWarnings();
assertNull(each.getWarnings());
}
}
private AbstractLobStreamingResultSetExtractor<Void> getResultSetExtractor(final boolean ex) {
AbstractLobStreamingResultSetExtractor<Void> lobRse = new AbstractLobStreamingResultSetExtractor<Void>() {
@Override
protected void streamData(ResultSet rs) throws SQLException, IOException {
if (ex) {
throw new IOException();
}
else {
rs.clearWarnings();
}
}
};
return lobRse;
}
private AbstractLobStreamingResultSetExtractor<Void> getResultSetExtractor(final boolean ex) {
AbstractLobStreamingResultSetExtractor<Void> lobRse = new AbstractLobStreamingResultSetExtractor<Void>() {
@Override
protected void streamData(ResultSet rs) throws SQLException, IOException {
if (ex) {
throw new IOException();
}
else {
rs.clearWarnings();
}
}
};
return lobRse;
}
private AbstractLobStreamingResultSetExtractor<Void> getResultSetExtractor(final boolean ex) {
AbstractLobStreamingResultSetExtractor<Void> lobRse = new AbstractLobStreamingResultSetExtractor<Void>() {
@Override
protected void streamData(ResultSet rs) throws SQLException, IOException {
if (ex) {
throw new IOException();
}
else {
rs.clearWarnings();
}
}
};
return lobRse;
}
/**
* Test that you get cursor operation conflict warning if updating
* a row which has been deleted from the table.
*/
public void testCursorOperationConflictWarning1()
throws SQLException
{
Statement s = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
s.setCursorName(getNextCursorName());
ResultSet rs = s.executeQuery("select * from t1");
rs.next();
createStatement().executeUpdate("delete from t1 where id=" +
rs.getString("ID"));
final int newValue = -3333;
final int oldValue = rs.getInt(2);
rs.updateInt(2, newValue);
rs.updateRow();
SQLWarning warn = rs.getWarnings();
assertWarning(warn, CURSOR_OPERATION_CONFLICT);
assertEquals("Did not expect the resultset to be updated", oldValue, rs.getInt(2));
assertTrue("Expected rs.rowDeleted() to be false", !rs.rowDeleted());
assertTrue("Expected rs.rowUpdated() to be false", !rs.rowUpdated());
rs.clearWarnings();
rs.deleteRow();
warn = rs.getWarnings();
assertWarning(warn, CURSOR_OPERATION_CONFLICT);
rs.relative(0);
assertTrue("Expected rs.rowUpdated() to be false", !rs.rowUpdated());
assertTrue("Expected rs.rowDeleted() to be false", !rs.rowDeleted());
assertEquals("Did not expect the resultset to be updated", oldValue, rs.getInt(2));
rs.close();
s.close();
}
@Test
public void assertClearWarnings() throws SQLException {
for (ResultSet each : resultSets.values()) {
assertNull(each.getWarnings());
each.clearWarnings();
assertNull(each.getWarnings());
}
}
/**
* Test that you get cursor operation conflict warning if updating
* a row which has been deleted from the table.
*/
public void testCursorOperationConflictWarning1()
throws SQLException
{
Statement s = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
s.setCursorName(getNextCursorName());
ResultSet rs = s.executeQuery("select * from t1");
rs.next();
createStatement().executeUpdate("delete from t1 where id=" +
rs.getString("ID"));
final int newValue = -3333;
final int oldValue = rs.getInt(2);
rs.updateInt(2, newValue);
rs.updateRow();
SQLWarning warn = rs.getWarnings();
assertWarning(warn, CURSOR_OPERATION_CONFLICT);
assertEquals("Did not expect the resultset to be updated", oldValue, rs.getInt(2));
assertTrue("Expected rs.rowDeleted() to be false", !rs.rowDeleted());
assertTrue("Expected rs.rowUpdated() to be false", !rs.rowUpdated());
rs.clearWarnings();
rs.deleteRow();
warn = rs.getWarnings();
assertWarning(warn, CURSOR_OPERATION_CONFLICT);
rs.relative(0);
assertTrue("Expected rs.rowUpdated() to be false", !rs.rowUpdated());
assertTrue("Expected rs.rowDeleted() to be false", !rs.rowDeleted());
assertEquals("Did not expect the resultset to be updated", oldValue, rs.getInt(2));
rs.close();
s.close();
}
private AbstractLobStreamingResultSetExtractor<Void> getResultSetExtractor(final boolean ex) {
AbstractLobStreamingResultSetExtractor<Void> lobRse = new AbstractLobStreamingResultSetExtractor<Void>() {
@Override
protected void streamData(ResultSet rs) throws SQLException, IOException {
if (ex) {
throw new IOException();
}
else {
rs.clearWarnings();
}
}
};
return lobRse;
}
/**
* Test that you get cursor operation conflict warning if updating
* a row which has been deleted from the table.
*/
public void testCursorOperationConflictWarning1()
throws SQLException
{
Statement s = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
s.setCursorName(getNextCursorName());
ResultSet rs = s.executeQuery("select * from t1");
rs.next();
createStatement().executeUpdate("delete from t1 where id=" +
rs.getString("ID"));
final int newValue = -3333;
final int oldValue = rs.getInt(2);
rs.updateInt(2, newValue);
rs.updateRow();
SQLWarning warn = rs.getWarnings();
assertWarning(warn, CURSOR_OPERATION_CONFLICT);
assertEquals("Did not expect the resultset to be updated", oldValue, rs.getInt(2));
assertTrue("Expected rs.rowDeleted() to be false", !rs.rowDeleted());
assertTrue("Expected rs.rowUpdated() to be false", !rs.rowUpdated());
rs.clearWarnings();
rs.deleteRow();
warn = rs.getWarnings();
assertWarning(warn, CURSOR_OPERATION_CONFLICT);
rs.relative(0);
assertTrue("Expected rs.rowUpdated() to be false", !rs.rowUpdated());
assertTrue("Expected rs.rowDeleted() to be false", !rs.rowDeleted());
assertEquals("Did not expect the resultset to be updated", oldValue, rs.getInt(2));
rs.close();
s.close();
}
/**
* <p>
* Check the results for the queries in testDataTruncation().
* </p>
*
* <p>
* The method expects a query that returns three rows with columns of a
* character string or binary string data type, where some of the values
* are cast to a narrower data type.
* </p>
*
* <p>
* Expect the following truncations to have taken place:
* </p>
*
* <ol>
* <li>Row 1, column 1: truncated from 3 to 2 bytes</li>
* <li>Row 3, column 1: truncated from 3 to 2 bytes</li>
* <li>Row 3, column 2: truncated from 4 to 2 bytes</li>
* </ol>
*/
private void checkDataTruncationResult(Statement s, String sql)
throws SQLException {
ResultSet rs = s.executeQuery(sql);
// First row should have one warning (column 1)
assertTrue(rs.next());
SQLWarning w = rs.getWarnings();
assertDataTruncation(w, -1, true, false, 3, 2);
w = w.getNextWarning();
assertNull(w);
rs.clearWarnings(); // workaround for DERBY-5765
// Second row should have no warnings
assertTrue(rs.next());
assertNull(rs.getWarnings());
// Third row should have two warnings (column 1 and 2)
assertTrue(rs.next());
w = rs.getWarnings();
assertDataTruncation(w, -1, true, false, 3, 2);
// Client driver doesn't support nested warnings
if (usingEmbedded()) {
w = w.getNextWarning();
assertDataTruncation(w, -1, true, false, 4, 2);
}
w = w.getNextWarning();
assertNull(w);
rs.clearWarnings(); // workaround for DERBY-5765
// No more rows
assertFalse(rs.next());
rs.close();
// There should be no warnings on the statement or the connection
assertNull(s.getWarnings());
assertNull(getConnection().getWarnings());
}