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();
}
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();
}
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);
}
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);
}
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());
}
Aggregations