Search in sources :

Example 6 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project spring-framework by spring-projects.

the class NamedParameterJdbcTemplateTests method testBatchUpdateWithPlainMap.

@Test
public void testBatchUpdateWithPlainMap() throws Exception {
    @SuppressWarnings("unchecked") final Map<String, Integer>[] ids = new Map[2];
    ids[0] = Collections.singletonMap("id", 100);
    ids[1] = Collections.singletonMap("id", 200);
    final int[] rowsAffected = new int[] { 1, 2 };
    given(preparedStatement.executeBatch()).willReturn(rowsAffected);
    given(connection.getMetaData()).willReturn(databaseMetaData);
    JdbcTemplate template = new JdbcTemplate(dataSource, false);
    namedParameterTemplate = new NamedParameterJdbcTemplate(template);
    int[] actualRowsAffected = namedParameterTemplate.batchUpdate("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
    assertTrue("executed 2 updates", actualRowsAffected.length == 2);
    assertEquals(rowsAffected[0], actualRowsAffected[0]);
    assertEquals(rowsAffected[1], actualRowsAffected[1]);
    verify(connection).prepareStatement("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?");
    verify(preparedStatement).setObject(1, 100);
    verify(preparedStatement).setObject(1, 200);
    verify(preparedStatement, times(2)).addBatch();
    verify(preparedStatement, atLeastOnce()).close();
    verify(connection, atLeastOnce()).close();
}
Also used : JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 7 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project spring-framework by spring-projects.

the class StoredProcedureTests method testStoredProcedureSkippingUndeclaredResults.

@Test
@SuppressWarnings("unchecked")
public void testStoredProcedureSkippingUndeclaredResults() throws Exception {
    ResultSet resultSet = mock(ResultSet.class);
    given(resultSet.next()).willReturn(true, true, false);
    given(resultSet.getString(2)).willReturn("Foo", "Bar");
    given(callableStatement.execute()).willReturn(true);
    given(callableStatement.getUpdateCount()).willReturn(-1);
    given(callableStatement.getResultSet()).willReturn(resultSet);
    given(callableStatement.getMoreResults()).willReturn(true, false);
    given(callableStatement.getUpdateCount()).willReturn(-1, -1);
    given(connection.prepareCall("{call " + StoredProcedureWithResultSetMapped.SQL + "()}")).willReturn(callableStatement);
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    jdbcTemplate.setSkipUndeclaredResults(true);
    StoredProcedureWithResultSetMapped sproc = new StoredProcedureWithResultSetMapped(jdbcTemplate);
    Map<String, Object> res = sproc.execute();
    assertEquals("incorrect number of returns", 1, res.size());
    List<String> rs1 = (List<String>) res.get("rs");
    assertEquals(2, rs1.size());
    assertEquals("Foo", rs1.get(0));
    assertEquals("Bar", rs1.get(1));
    verify(resultSet).close();
}
Also used : ResultSet(java.sql.ResultSet) SqlReturnResultSet(org.springframework.jdbc.core.SqlReturnResultSet) List(java.util.List) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.Test)

Example 8 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project spring-framework by spring-projects.

the class StoredProcedureTests method testStoredProcedureConfiguredViaJdbcTemplateWithCustomExceptionTranslator.

/**
	 * Confirm no connection was used to get metadata. Does not use superclass replay
	 * mechanism.
	 *
	 * @throws Exception
	 */
@Test
public void testStoredProcedureConfiguredViaJdbcTemplateWithCustomExceptionTranslator() throws Exception {
    given(callableStatement.execute()).willReturn(false);
    given(callableStatement.getUpdateCount()).willReturn(-1);
    given(callableStatement.getObject(2)).willReturn(5);
    given(connection.prepareCall("{call " + StoredProcedureConfiguredViaJdbcTemplate.SQL + "(?, ?)}")).willReturn(callableStatement);
    class TestJdbcTemplate extends JdbcTemplate {

        int calls;

        @Override
        public Map<String, Object> call(CallableStatementCreator csc, List<SqlParameter> declaredParameters) throws DataAccessException {
            calls++;
            return super.call(csc, declaredParameters);
        }
    }
    TestJdbcTemplate t = new TestJdbcTemplate();
    t.setDataSource(dataSource);
    // Will fail without the following, because we're not able to get a connection
    // from the DataSource here if we need to create an ExceptionTranslator
    t.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
    StoredProcedureConfiguredViaJdbcTemplate sp = new StoredProcedureConfiguredViaJdbcTemplate(t);
    assertEquals(sp.execute(11), 5);
    assertEquals(1, t.calls);
    verify(callableStatement).setObject(1, 11, Types.INTEGER);
    verify(callableStatement).registerOutParameter(2, Types.INTEGER);
}
Also used : CallableStatementCreator(org.springframework.jdbc.core.CallableStatementCreator) List(java.util.List) SQLStateSQLExceptionTranslator(org.springframework.jdbc.support.SQLStateSQLExceptionTranslator) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.Test)

Example 9 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project spring-framework by spring-projects.

the class StoredProcedureTests method testStoredProcedureConfiguredViaJdbcTemplate.

/**
	 * Confirm our JdbcTemplate is used
	 *
	 * @throws Exception
	 */
@Test
public void testStoredProcedureConfiguredViaJdbcTemplate() throws Exception {
    given(callableStatement.execute()).willReturn(false);
    given(callableStatement.getUpdateCount()).willReturn(-1);
    given(callableStatement.getObject(2)).willReturn(4);
    given(connection.prepareCall("{call " + StoredProcedureConfiguredViaJdbcTemplate.SQL + "(?, ?)}")).willReturn(callableStatement);
    JdbcTemplate t = new JdbcTemplate();
    t.setDataSource(dataSource);
    StoredProcedureConfiguredViaJdbcTemplate sp = new StoredProcedureConfiguredViaJdbcTemplate(t);
    assertEquals(sp.execute(1106), 4);
    verify(callableStatement).setObject(1, 1106, Types.INTEGER);
    verify(callableStatement).registerOutParameter(2, Types.INTEGER);
}
Also used : JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.Test)

Example 10 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project spring-framework by spring-projects.

the class RdbmsOperationTests method parameterPropagation.

@Test
public void parameterPropagation() {
    SqlOperation operation = new SqlOperation() {
    };
    DataSource ds = new DriverManagerDataSource();
    operation.setDataSource(ds);
    operation.setFetchSize(10);
    operation.setMaxRows(20);
    JdbcTemplate jt = operation.getJdbcTemplate();
    assertEquals(ds, jt.getDataSource());
    assertEquals(10, jt.getFetchSize());
    assertEquals(20, jt.getMaxRows());
}
Also used : DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataSource(javax.sql.DataSource) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) Test(org.junit.Test)

Aggregations

JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)121 Test (org.junit.Test)45 DataSource (javax.sql.DataSource)36 Before (org.junit.Before)18 SQLException (java.sql.SQLException)11 EmbeddedDatabaseBuilder (org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder)11 BaseDbTest (com.alibaba.otter.node.etl.BaseDbTest)6 DbDialect (com.alibaba.otter.node.etl.common.db.dialect.DbDialect)6 DbMediaSource (com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource)6 Connection (java.sql.Connection)6 JdbcOperations (org.springframework.jdbc.core.JdbcOperations)6 TransactionStatus (org.springframework.transaction.TransactionStatus)6 Test (org.testng.annotations.Test)6 SqlTemplate (com.alibaba.otter.node.etl.common.db.dialect.SqlTemplate)5 PreparedStatement (java.sql.PreparedStatement)5 Table (org.apache.ddlutils.model.Table)5 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)5 DataAccessException (org.springframework.dao.DataAccessException)5 AbstractDriverBasedDataSource (org.springframework.jdbc.datasource.AbstractDriverBasedDataSource)5 TransactionCallback (org.springframework.transaction.support.TransactionCallback)5