Example usage for java.sql CallableStatement getUpdateCount

List of usage examples for java.sql CallableStatement getUpdateCount

Introduction

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

Prototype

int getUpdateCount() throws SQLException;

Source Link

Document

Retrieves the current result as an update count; if the result is a ResultSet object or there are no more results, -1 is returned.

Usage

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

/**
 * Extract returned ResultSets from the completed stored procedure.
 * @param cs JDBC wrapper for the stored procedure
 * @param parameters Parameter list for the stored procedure
 * @return Map that contains returned results
 *//*w  w  w  .  jav a2 s .c om*/
protected Map extractReturnedResultSets(CallableStatement cs, List parameters, int updateCount)
        throws SQLException {
    Map returnedResults = new HashMap();
    int rsIndex = 0;
    boolean moreResults;
    do {
        if (updateCount == -1) {
            Object param = null;
            if (parameters != null && parameters.size() > rsIndex) {
                param = parameters.get(rsIndex);
            }
            if (param instanceof SqlReturnResultSet) {
                SqlReturnResultSet rsParam = (SqlReturnResultSet) param;
                returnedResults.putAll(processResultSet(cs.getResultSet(), rsParam));
            } else {
                logger.warn("ResultSet returned from stored procedure but a corresponding "
                        + "SqlReturnResultSet parameter was not declared");
            }
            rsIndex++;
        }
        moreResults = cs.getMoreResults();
        updateCount = cs.getUpdateCount();
        if (logger.isDebugEnabled()) {
            logger.debug("CallableStatement.getUpdateCount returned [" + updateCount + "]");
        }
    } while (moreResults || updateCount != -1);
    return returnedResults;
}

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 ww.  j  a v a  2  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 testExecuteClosed() throws Exception {
    MockControl ctrlResultSet;//from www.j  av  a2 s.  co  m
    ResultSet mockResultSet;
    MockControl ctrlCallable;
    CallableStatement mockCallable;

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

    ctrlCallable = MockControl.createControl(CallableStatement.class);
    mockCallable = (CallableStatement) ctrlCallable.getMock();
    mockCallable.execute();
    ctrlCallable.setReturnValue(true);
    mockCallable.getUpdateCount();
    ctrlCallable.setReturnValue(-1);
    mockCallable.getResultSet();
    ctrlCallable.setReturnValue(mockResultSet);
    mockCallable.close();
    ctrlCallable.setVoidCallable();

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

    ctrlResultSet.replay();
    ctrlCallable.replay();
    replay();

    List params = new ArrayList();
    params.add(new SqlReturnResultSet("", new RowCallbackHandler() {
        public void processRow(ResultSet rs) {
            throw new InvalidDataAccessApiUsageException("");
        }

    }));

    JdbcTemplate template = new JdbcTemplate(mockDataSource);
    try {
        template.call(new CallableStatementCreator() {
            public CallableStatement createCallableStatement(Connection conn) throws SQLException {
                return conn.prepareCall("my query");
            }
        }, params);
    } catch (InvalidDataAccessApiUsageException idaauex) {
        // ok
    }

    // verify confirms if test is successful by checking if close() called
    ctrlResultSet.verify();
    ctrlCallable.verify();
}

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

public void testCaseInsensitiveResultsMap() throws Exception {

    MockControl ctrlCallable;//  w  ww  . j av  a  2  s.  c om
    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();
}

From source file:org.springframework.jdbc.object.GenericStoredProcedureTests.java

@Test
public void testAddInvoices() throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
            new ClassPathResource("org/springframework/jdbc/object/GenericStoredProcedureTests-context.xml"));
    Connection connection = mock(Connection.class);
    DataSource dataSource = mock(DataSource.class);
    given(dataSource.getConnection()).willReturn(connection);
    CallableStatement callableStatement = mock(CallableStatement.class);
    TestDataSourceWrapper testDataSource = (TestDataSourceWrapper) bf.getBean("dataSource");
    testDataSource.setTarget(dataSource);

    given(callableStatement.execute()).willReturn(false);
    given(callableStatement.getUpdateCount()).willReturn(-1);
    given(callableStatement.getObject(3)).willReturn(new Integer(4));

    given(connection.prepareCall("{call " + "add_invoice" + "(?, ?, ?)}")).willReturn(callableStatement);

    StoredProcedure adder = (StoredProcedure) bf.getBean("genericProcedure");
    Map<String, Object> in = new HashMap<String, Object>(2);
    in.put("amount", 1106);
    in.put("custid", 3);
    Map out = adder.execute(in);//from  w w w . ja  va 2 s.  c  o m
    Integer id = (Integer) out.get("newid");
    assertEquals(4, id.intValue());

    verify(callableStatement).setObject(1, new Integer(1106), Types.INTEGER);
    verify(callableStatement).setObject(2, new Integer(3), Types.INTEGER);
    verify(callableStatement).registerOutParameter(3, Types.INTEGER);
    verify(callableStatement).close();
}

From source file:org.wso2.carbon.dataservices.core.description.query.SQLQuery.java

private ResultSet getFirstRSOfStoredProc(CallableStatement stmt) throws SQLException {
    boolean resultAndNoUpdateCount = stmt.execute();
    ResultSet result = null;/*from   w w  w .j ava2 s  .  c o m*/
    while (true) {
        if (!resultAndNoUpdateCount) {
            if (stmt.getUpdateCount() == -1) {
                break;
            }
        } else {
            result = stmt.getResultSet();
            break;
        }
        try {
            resultAndNoUpdateCount = stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT);
        } catch (SQLException e) {
            /*
             * for some DBMS, this will throw an unsupported feature
             * exception, even when after a valid result set is retrieved
             * first, so if there's a valid result set existing, we will
             * ignore the eventual unsupported feature exception
             */
            if (result == null) {
                throw e;
            }
            break;
        }
    }
    return result;
}

From source file:org.xsystem.sql2.dml.DmlCommand.java

public Object execute(Connection con, String stmt, List<DmlParams> paramsSpec, Map<String, Object> values,
        boolean singleRow) {

    Object ret = null;//from ww w. j a  va2 s.  c  o m
    CallableStatement cs = null;
    ResultSet rs = null;
    try {
        cs = con.prepareCall(stmt);
        setParms(cs, paramsSpec, values);
        if (cs.execute()) {
            rs = cs.getResultSet();
            ret = new ArrayList();
            Map row = null;
            while (rs.next()) {
                row = rowAsMap(rs);
                if (singleRow) {
                    break;
                }
                ((List) ret).add(row);
            }
            if (singleRow) {
                return row;
            }
            ;
            return ret;
        } else {
            ret = buildOutParams(cs, paramsSpec);
            if (ret == null) {
                ret = (Integer) cs.getUpdateCount();
            }
        }
    } catch (SQLException ex) {
        buildError(ex, stmt, paramsSpec, values);
    } finally {
        Auxilary.close(rs);
        Auxilary.close(cs);
    }
    return ret;
}