List of usage examples for org.springframework.jdbc.core JdbcTemplate setDataSource
public void setDataSource(@Nullable DataSource dataSource)
From source file:org.springframework.jdbc.core.JdbcTemplateTests.java
/** * Test that we see an SQLException translated using Error Code. * If we provide the SQLExceptionTranslator, we shouldn't use a connection * to get the metadata// w w w .ja va2 s . c om */ public void testUseCustomSQLErrorCodeTranslator() throws Exception { // Bad SQL state final SQLException sex = new SQLException("I have a known problem", "07000", 1054); final String sql = "SELECT ID FROM CUSTOMER"; MockControl ctrlResultSet = MockControl.createControl(ResultSet.class); ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock(); mockResultSet.next(); ctrlResultSet.setReturnValue(true); mockResultSet.close(); ctrlResultSet.setVoidCallable(); MockControl ctrlStatement = MockControl.createControl(PreparedStatement.class); PreparedStatement mockStatement = (PreparedStatement) ctrlStatement.getMock(); mockStatement.executeQuery(sql); ctrlStatement.setReturnValue(mockResultSet); mockStatement.close(); ctrlStatement.setVoidCallable(); mockConnection.createStatement(); ctrlConnection.setReturnValue(mockStatement); // Change behaviour in setUp() because we only expect one call to getConnection(): // none is necessary to get metadata for exception translator ctrlConnection = MockControl.createControl(Connection.class); mockConnection = (Connection) ctrlConnection.getMock(); mockConnection.createStatement(); ctrlConnection.setReturnValue(mockStatement, 1); mockConnection.close(); ctrlConnection.setVoidCallable(1); ctrlConnection.replay(); ctrlDataSource = MockControl.createControl(DataSource.class); mockDataSource = (DataSource) ctrlDataSource.getMock(); mockDataSource.getConnection(); ctrlDataSource.setReturnValue(mockConnection, 1); ctrlDataSource.replay(); ///// end changed behaviour ctrlResultSet.replay(); ctrlStatement.replay(); JdbcTemplate template = new JdbcTemplate(); template.setDataSource(mockDataSource); // Set custom exception translator template.setExceptionTranslator(new SQLStateSQLExceptionTranslator()); template.afterPropertiesSet(); try { template.query(sql, new RowCallbackHandler() { public void processRow(ResultSet rs) throws SQLException { throw sex; } }); fail("Should have thrown exception"); } catch (BadSqlGrammarException ex) { assertTrue("Wanted same exception back, not " + ex, sex == ex.getCause()); } ctrlResultSet.verify(); ctrlStatement.verify(); // We didn't call superclass replay() so we need to check these ourselves ctrlDataSource.verify(); ctrlConnection.verify(); }
From source file:org.springframework.jdbc.object.StoredProcedureTests.java
/** * Confirm our JdbcTemplate is used// w w w . j ava 2 s . c o m * @throws Exception */ public void testStoredProcedureConfiguredViaJdbcTemplate() throws Exception { mockCallable.setObject(1, new Integer(1106), Types.INTEGER); ctrlCallable.setVoidCallable(); mockCallable.registerOutParameter(2, Types.INTEGER); ctrlCallable.setVoidCallable(); mockCallable.execute(); ctrlCallable.setReturnValue(false); mockCallable.getUpdateCount(); ctrlCallable.setReturnValue(-1); mockCallable.getObject(2); ctrlCallable.setReturnValue(new Integer(4)); if (debugEnabled) { mockCallable.getWarnings(); ctrlCallable.setReturnValue(null); } mockCallable.close(); ctrlCallable.setVoidCallable(); mockConnection.prepareCall("{call " + StoredProcedureConfiguredViaJdbcTemplate.SQL + "(?, ?)}"); ctrlConnection.setReturnValue(mockCallable); replay(); JdbcTemplate t = new JdbcTemplate(); t.setDataSource(mockDataSource); StoredProcedureConfiguredViaJdbcTemplate sp = new StoredProcedureConfiguredViaJdbcTemplate(t); assertEquals(sp.execute(1106), 4); }