Example usage for java.sql CallableStatement getWarnings

List of usage examples for java.sql CallableStatement getWarnings

Introduction

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

Prototype

SQLWarning getWarnings() throws SQLException;

Source Link

Document

Retrieves the first warning reported by calls on this Statement object.

Usage

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

public Object execute(CallableStatementCreator csc, CallableStatementCallback action) {
    if (logger.isDebugEnabled()) {
        String sql = getSql(csc);
        logger.debug("Calling stored procedure" + (sql != null ? " [" + sql + "]" : ""));
    }// www.  j  a v  a 2  s  .  c  om
    Connection con = DataSourceUtils.getConnection(getDataSource());
    CallableStatement cs = null;
    try {
        Connection conToUse = con;
        if (this.nativeJdbcExtractor != null
                && this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeCallableStatements()) {
            conToUse = this.nativeJdbcExtractor.getNativeConnection(con);
        }
        cs = csc.createCallableStatement(conToUse);
        DataSourceUtils.applyTransactionTimeout(cs, getDataSource());
        CallableStatement csToUse = cs;
        if (this.nativeJdbcExtractor != null) {
            csToUse = this.nativeJdbcExtractor.getNativeCallableStatement(cs);
        }
        Object result = action.doInCallableStatement(csToUse);
        SQLWarning warning = cs.getWarnings();
        throwExceptionOnWarningIfNotIgnoringWarnings(warning);
        return result;
    } catch (SQLException ex) {
        throw getExceptionTranslator().translate("executing CallableStatementCallback [" + csc + "]",
                getSql(csc), ex);
    } finally {
        if (csc instanceof ParameterDisposer) {
            ((ParameterDisposer) csc).cleanupParameters();
        }
        JdbcUtils.closeStatement(cs);
        DataSourceUtils.closeConnectionIfNecessary(con, getDataSource());
    }
}

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

public void testNativeJdbcExtractorInvoked() throws Exception {
    MockControl ctrlResultSet = MockControl.createControl(ResultSet.class);
    final ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock();
    mockResultSet.close();//from w w  w.j ava2  s.  c  o m
    ctrlResultSet.setVoidCallable(2);

    MockControl ctrlStatement = MockControl.createControl(Statement.class);
    final Statement mockStatement = (Statement) ctrlStatement.getMock();
    if (debugEnabled) {
        mockStatement.getWarnings();
        ctrlStatement.setReturnValue(null);
    }
    mockStatement.close();
    ctrlStatement.setVoidCallable();
    MockControl ctrlStatement2 = MockControl.createControl(Statement.class);
    final Statement mockStatement2 = (Statement) ctrlStatement2.getMock();
    mockStatement2.executeQuery("my query");
    ctrlStatement2.setReturnValue(mockResultSet, 1);

    MockControl ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class);
    final PreparedStatement mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock();
    if (debugEnabled) {
        mockPreparedStatement.getWarnings();
        ctrlPreparedStatement.setReturnValue(null);
    }
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();
    MockControl ctrlPreparedStatement2 = MockControl.createControl(PreparedStatement.class);
    final PreparedStatement mockPreparedStatement2 = (PreparedStatement) ctrlPreparedStatement2.getMock();
    mockPreparedStatement2.executeQuery();
    ctrlPreparedStatement2.setReturnValue(mockResultSet, 1);

    MockControl ctrlReturnResultSet = MockControl.createControl(ResultSet.class);
    final ResultSet mockReturnResultSet = (ResultSet) ctrlReturnResultSet.getMock();
    mockReturnResultSet.next();
    ctrlReturnResultSet.setReturnValue(false);
    mockReturnResultSet.close();
    ctrlReturnResultSet.setVoidCallable(2);

    MockControl ctrlCallableStatement = MockControl.createControl(CallableStatement.class);
    final CallableStatement mockCallableStatement = (CallableStatement) ctrlCallableStatement.getMock();
    if (debugEnabled) {
        mockCallableStatement.getWarnings();
        ctrlCallableStatement.setReturnValue(null);
    }
    mockCallableStatement.close();
    ctrlCallableStatement.setVoidCallable();
    MockControl ctrlCallableStatement2 = MockControl.createControl(CallableStatement.class);
    final CallableStatement mockCallableStatement2 = (CallableStatement) ctrlCallableStatement2.getMock();
    mockCallableStatement2.execute();
    ctrlCallableStatement2.setReturnValue(true);
    mockCallableStatement2.getUpdateCount();
    ctrlCallableStatement2.setReturnValue(-1);
    mockCallableStatement2.getResultSet();
    ctrlCallableStatement2.setReturnValue(mockReturnResultSet);
    mockCallableStatement2.getMoreResults();
    ctrlCallableStatement2.setReturnValue(false);
    mockCallableStatement2.getUpdateCount();
    ctrlCallableStatement2.setReturnValue(-1);

    ctrlResultSet.replay();
    ctrlStatement.replay();
    ctrlStatement2.replay();
    ctrlPreparedStatement.replay();
    ctrlPreparedStatement2.replay();
    ctrlReturnResultSet.replay();
    ;
    ctrlCallableStatement.replay();
    ctrlCallableStatement2.replay();

    mockConnection.createStatement();
    ctrlConnection.setReturnValue(mockStatement, 1);
    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource);
    template.setNativeJdbcExtractor(new NativeJdbcExtractor() {
        public boolean isNativeConnectionNecessaryForNativeStatements() {
            return false;
        }

        public boolean isNativeConnectionNecessaryForNativePreparedStatements() {
            return false;
        }

        public boolean isNativeConnectionNecessaryForNativeCallableStatements() {
            return false;
        }

        public Connection getNativeConnection(Connection con) {
            return con;
        }

        public Connection getNativeConnectionFromStatement(Statement stmt) throws SQLException {
            return stmt.getConnection();
        }

        public Statement getNativeStatement(Statement stmt) {
            assertTrue(stmt == mockStatement);
            return mockStatement2;
        }

        public PreparedStatement getNativePreparedStatement(PreparedStatement ps) {
            assertTrue(ps == mockPreparedStatement);
            return mockPreparedStatement2;
        }

        public CallableStatement getNativeCallableStatement(CallableStatement cs) {
            assertTrue(cs == mockCallableStatement);
            return mockCallableStatement2;
        }

        public ResultSet getNativeResultSet(ResultSet rs) {
            return rs;
        }
    });

    template.query("my query", new ResultSetExtractor() {
        public Object extractData(ResultSet rs2) {
            assertEquals(mockResultSet, rs2);
            return null;
        }
    });

    template.query(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection conn) {
            return mockPreparedStatement;
        }
    }, new ResultSetExtractor() {
        public Object extractData(ResultSet rs2) {
            assertEquals(mockResultSet, rs2);
            return null;
        }
    });

    template.call(new CallableStatementCreator() {
        public CallableStatement createCallableStatement(Connection con) {
            return mockCallableStatement;
        }
    }, new ArrayList());

    ctrlStatement.verify();
    ctrlStatement2.verify();
    ctrlPreparedStatement.verify();
    ctrlPreparedStatement2.verify();
    ctrlCallableStatement.verify();
    ctrlCallableStatement2.verify();
}

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

public void testCaseInsensitiveResultsMap() throws Exception {

    MockControl ctrlCallable;//from ww  w.  j a v  a2 s  .  co  m
    CallableStatement mockCallable;

    ctrlCallable = MockControl.createControl(CallableStatement.class);
    mockCallable = (CallableStatement) ctrlCallable.getMock();
    mockCallable.execute();
    ctrlCallable.setReturnValue(false);
    mockCallable.getUpdateCount();
    ctrlCallable.setReturnValue(-1);
    mockCallable.getObject(1);
    ctrlCallable.setReturnValue("X");
    if (debugEnabled) {
        mockCallable.getWarnings();
        ctrlCallable.setReturnValue(null);
    }
    mockCallable.close();
    ctrlCallable.setVoidCallable();

    mockConnection.prepareCall("my query");
    ctrlConnection.setReturnValue(mockCallable);

    ctrlCallable.replay();
    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource);
    assertTrue("default should have been NOT case insensitive", !template.isResultsMapCaseInsensitive());

    template.setResultsMapCaseInsensitive(true);
    assertTrue("now it should have been set to case insensitive", template.isResultsMapCaseInsensitive());

    List params = new ArrayList();
    params.add(new SqlOutParameter("a", 12));

    Map out = template.call(new CallableStatementCreator() {
        public CallableStatement createCallableStatement(Connection conn) throws SQLException {
            return conn.prepareCall("my query");
        }
    }, params);
    assertTrue("this should have been a LinkedCaseInsensitiveMap", out instanceof LinkedCaseInsensitiveMap);
    assertNotNull("we should have gotten the result with upper case", out.get("A"));
    assertNotNull("we should have gotten the result with lower case", out.get("a"));

    ctrlCallable.verify();
}