Example usage for java.sql SQLWarning SQLWarning

List of usage examples for java.sql SQLWarning SQLWarning

Introduction

In this page you can find the example usage for java.sql SQLWarning SQLWarning.

Prototype

public SQLWarning(Throwable cause) 

Source Link

Document

Constructs a SQLWarning object with a given cause.

Usage

From source file:Main.java

private static void throwWarning() throws SQLException {
    SQLWarning rootWarning = new SQLWarning("Outter warning");
    SQLWarning containedWarning = new SQLWarning("Inner warning");
    rootWarning.setNextWarning(containedWarning);
    throw rootWarning;
}

From source file:org.apache.hive.jdbc.HiveConnection.java

@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
    // Per JDBC spec, if the connection is closed a SQLException should be thrown.
    if (isClosed) {
        throw new SQLException("Connection is closed");
    }/*  w w  w  . ja v  a 2s.c  o m*/
    // The auto-commit mode is always enabled for this connection. Per JDBC spec,
    // if setAutoCommit is called and the auto-commit mode is not changed, the call is a no-op.
    if (!autoCommit) {
        LOG.warn("Request to set autoCommit to false; Hive does not support autoCommit=false.");
        SQLWarning warning = new SQLWarning("Hive does not support autoCommit=false");
        if (warningChain == null)
            warningChain = warning;
        else
            warningChain.setNextWarning(warning);
    }
}

From source file:org.apache.hive.jdbc.HivePreparedStatement.java

/**
 * Executes the SQL statement./*from w w w .  j av a  2s .  com*/
 *
 * @param sql The sql, as a string, to execute
 * @return ResultSet
 * @throws SQLException if the prepared statement is closed or there is a database error.
 *                      caught Exceptions are thrown as SQLExceptions with the description
 *                      "08S01".
 */

protected ResultSet executeImmediate(String sql) throws SQLException {
    if (isClosed) {
        throw new SQLException("Can't execute after statement has been closed", "24000");
    }

    try {
        clearWarnings();
        resultSet = null;
        if (sql.contains("?")) {
            sql = updateSql(sql, parameters);
        }
        TExecuteStatementReq execReq = new TExecuteStatementReq(sessHandle, sql);
        execReq.setConfOverlay(sessConf);
        TExecuteStatementResp execResp = client.ExecuteStatement(execReq);
        if (execResp.getStatus().getStatusCode().equals(TStatusCode.STILL_EXECUTING_STATUS)) {
            warningChain = Utils.addWarning(warningChain, new SQLWarning("Query execuing asynchronously"));
        } else {
            Utils.verifySuccessWithInfo(execResp.getStatus());
        }

        stmtHandle = execResp.getOperationHandle();
    } catch (SQLException es) {
        throw es;
    } catch (Exception ex) {
        throw new SQLException(ex.toString(), "08S01", ex);
    }
    resultSet = new HiveQueryResultSet.Builder().setClient(client).setSessionHandle(sessHandle)
            .setStmtHandle(stmtHandle).setStatement(this).setMaxRows(maxRows).build();
    return resultSet;
}

From source file:org.executequery.databasemediators.spi.DefaultStatementExecutor.java

/** <p>Commits or rolls back the last executed
 *  SQL query or queries./*from   w w  w .  j a  va  2  s.  com*/
 *
 *  @param true to commit - false to roll back
 */
private SqlStatementResult commitLast(boolean commit) {

    statementResult.reset();
    statementResult.setUpdateCount(0);

    try {

        if (!conn.isClosed()) {

            if (conn.getAutoCommit()) {

                statementResult.setSqlWarning(new SQLWarning("Auto-Commit is set true"));
                return statementResult;
            }

            if (commit) {

                conn.commit();
                Log.info("Commit complete.");
                statementResult.setMessage("Commit complete.");
                closeMaxedConn();

            } else {

                conn.rollback();
                Log.info("Rollback complete.");
                statementResult.setMessage("Rollback complete.");
                closeMaxedConn();
            }

        } else {

            statementResult.setSqlException(new SQLException("Connection is closed"));
        }

    } catch (SQLException e) {
        handleException(e);
        statementResult.setSqlException(e);
    }
    return statementResult;

}

From source file:org.springframework.jdbc.core.JdbcTemplateTests.java

/**
 * Mock objects allow us to produce warnings at will
 *///from   ww  w.  ja v a  2s.co m
public void testFatalWarning() throws Exception {
    String sql = "SELECT forename from custmr";
    SQLWarning warnings = new SQLWarning("My warning");

    MockControl ctrlResultSet = MockControl.createControl(ResultSet.class);
    ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock();
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    MockControl ctrlStatement = MockControl.createControl(PreparedStatement.class);
    PreparedStatement mockStatement = (PreparedStatement) ctrlStatement.getMock();
    mockStatement.executeQuery(sql);
    ctrlStatement.setReturnValue(mockResultSet);
    mockStatement.getWarnings();
    ctrlStatement.setReturnValue(warnings);
    mockStatement.close();
    ctrlStatement.setVoidCallable();

    mockConnection.createStatement();
    ctrlConnection.setReturnValue(mockStatement);

    ctrlResultSet.replay();
    ctrlStatement.replay();
    replay();

    JdbcTemplate t = new JdbcTemplate(mockDataSource);
    t.setIgnoreWarnings(false);
    try {
        t.query(sql, new RowCallbackHandler() {
            public void processRow(ResultSet rs) throws SQLException {
                rs.getByte(1);
            }
        });
        fail("Should have thrown exception on warning");
    } catch (SQLWarningException ex) {
        // Pass
        assertTrue("Root cause of warning was correct", ex.getCause() == warnings);
    }

    ctrlResultSet.verify();
    ctrlStatement.verify();
}

From source file:org.springframework.jdbc.core.JdbcTemplateTests.java

public void testIgnoredWarning() throws Exception {
    String sql = "SELECT forename from custmr";
    SQLWarning warnings = new SQLWarning("My warning");

    MockControl ctrlResultSet = MockControl.createControl(ResultSet.class);
    ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock();
    mockResultSet.next();// w ww. j a  va  2s  .c om
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    MockControl ctrlStatement = MockControl.createControl(PreparedStatement.class);
    PreparedStatement mockStatement = (PreparedStatement) ctrlStatement.getMock();
    mockStatement.executeQuery(sql);
    ctrlStatement.setReturnValue(mockResultSet);
    if (debugEnabled) {
        mockStatement.getWarnings();
        ctrlStatement.setReturnValue(null);
    }
    mockStatement.close();
    ctrlStatement.setVoidCallable();

    mockConnection.createStatement();
    ctrlConnection.setReturnValue(mockStatement);

    ctrlResultSet.replay();
    ctrlStatement.replay();
    replay();

    // Too long: truncation
    JdbcTemplate template = new JdbcTemplate(mockDataSource);
    template.setIgnoreWarnings(true);
    template.query(sql, new RowCallbackHandler() {
        public void processRow(ResultSet rs) throws java.sql.SQLException {
            rs.getByte(1);
        }
    });

    ctrlResultSet.verify();
    ctrlStatement.verify();
}