List of usage examples for org.springframework.jdbc.core JdbcTemplate setDataSource
public void setDataSource(@Nullable DataSource dataSource)
From source file:com.hp.autonomy.frontend.find.core.AbstractDatabaseInitIT.java
/** * Check connection to our configured datasource. * * Depending on the datasource a dummy statement must sometimes * be executed to ensure a valid connection. *///from w ww .j ava2 s.co m @Test public void connectToDatabase() { final JdbcTemplate jdbcTemplate = new JdbcTemplate(); jdbcTemplate.setDataSource(dataSource); jdbcTemplate.execute("SELECT true"); }
From source file:io.cloudslang.schema.context.ScoreDatabaseContext.java
@Bean JdbcTemplate jdbcTemplate(DataSource dataSource) { JdbcTemplate jdbcTemplate = new JdbcTemplate(); jdbcTemplate.setDataSource(dataSource); return jdbcTemplate; }
From source file:nz.geek.caffe.spring.hdb.HANAExceptionMappingTest.java
/** *//*from w w w. j ava 2s. com*/ @Test public void testUnableToConnect() { final DriverManagerDataSource ds = new DriverManagerDataSource("jdbc:sap://blahhost:30115", "blah", "blah"); JdbcTemplate template = new JdbcTemplate(); template.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator("HDB")); template.setDataSource(new LazyConnectionDataSourceProxy(ds)); template.afterPropertiesSet(); try { template.execute("SELECT 1 FROM DUMMY"); Assert.fail("connect should have failed"); } catch (final DataAccessResourceFailureException e) { // expected } }
From source file:edu.jhuapl.openessence.datasource.jdbc.JdbcOeDataSource.java
public JdbcTemplate getJdbcTemplate() { JdbcTemplate copy = new JdbcTemplate(); copy.setDataSource(jdbcTemplate.getDataSource()); return copy;// ww w.j a v a 2s . c o m }
From source file:org.encuestame.test.persistence.config.DBTestConfig.java
@Bean @Qualifier(value = "jdbcDataSource") public JdbcTemplate jdbcTemplate(BasicDataSource dataSource) { final JdbcTemplate jdbcTemplate = new JdbcTemplate(); jdbcTemplate.setDataSource(dataSource); return jdbcTemplate; }
From source file:org.springframework.jdbc.core.JdbcTemplateTests.java
private void doTestStrings(JdbcTemplateCallback jdbcTemplateCallback, boolean usePreparedStatement, Integer fetchSize, Integer maxRows, Integer queryTimeout, Object argument) throws Exception { String sql = "SELECT FORENAME FROM CUSTMR"; String[] results = { "rod", "gary", " portia" }; class StringHandler implements RowCallbackHandler { private List list = new LinkedList(); public void processRow(ResultSet rs) throws SQLException { list.add(rs.getString(1));//from w ww . j a v a 2 s. c o m } public String[] getStrings() { return (String[]) list.toArray(new String[list.size()]); } } MockControl ctrlResultSet = MockControl.createControl(ResultSet.class); ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock(); mockResultSet.next(); ctrlResultSet.setReturnValue(true); mockResultSet.getString(1); ctrlResultSet.setReturnValue(results[0]); mockResultSet.next(); ctrlResultSet.setReturnValue(true); mockResultSet.getString(1); ctrlResultSet.setReturnValue(results[1]); mockResultSet.next(); ctrlResultSet.setReturnValue(true); mockResultSet.getString(1); ctrlResultSet.setReturnValue(results[2]); mockResultSet.next(); ctrlResultSet.setReturnValue(false); mockResultSet.close(); ctrlResultSet.setVoidCallable(); MockControl ctrlStatement = MockControl.createControl(PreparedStatement.class); PreparedStatement mockStatement = (PreparedStatement) ctrlStatement.getMock(); if (fetchSize != null) { mockStatement.setFetchSize(fetchSize.intValue()); } if (maxRows != null) { mockStatement.setMaxRows(maxRows.intValue()); } if (queryTimeout != null) { mockStatement.setQueryTimeout(queryTimeout.intValue()); } if (argument != null) { mockStatement.setObject(1, argument); } if (usePreparedStatement) { mockStatement.executeQuery(); } else { mockStatement.executeQuery(sql); } ctrlStatement.setReturnValue(mockResultSet); if (debugEnabled) { mockStatement.getWarnings(); ctrlStatement.setReturnValue(null); } mockStatement.close(); ctrlStatement.setVoidCallable(); if (usePreparedStatement) { mockConnection.prepareStatement(sql); } else { mockConnection.createStatement(); } ctrlConnection.setReturnValue(mockStatement); ctrlResultSet.replay(); ctrlStatement.replay(); replay(); StringHandler sh = new StringHandler(); JdbcTemplate template = new JdbcTemplate(); template.setDataSource(mockDataSource); if (fetchSize != null) { template.setFetchSize(fetchSize.intValue()); } if (maxRows != null) { template.setMaxRows(maxRows.intValue()); } if (queryTimeout != null) { template.setQueryTimeout(queryTimeout.intValue()); } jdbcTemplateCallback.doInJdbcTemplate(template, sql, sh); // Match String[] forenames = sh.getStrings(); assertTrue("same length", forenames.length == results.length); for (int i = 0; i < forenames.length; i++) { assertTrue("Row " + i + " matches", forenames[i].equals(results[i])); } ctrlResultSet.verify(); ctrlStatement.verify(); }
From source file:org.springframework.jdbc.core.JdbcTemplateTests.java
public void testCouldntGetConnectionForOperationWithLazyExceptionTranslator() throws SQLException { SQLException sex = new SQLException("foo", "07xxx"); ctrlDataSource = MockControl.createControl(DataSource.class); mockDataSource = (DataSource) ctrlDataSource.getMock(); mockDataSource.getConnection();//from w ww. j ava 2 s. c o m ctrlDataSource.setThrowable(sex, 1); replay(); try { JdbcTemplate template2 = new JdbcTemplate(); template2.setDataSource(mockDataSource); template2.afterPropertiesSet(); RowCountCallbackHandler rcch = new RowCountCallbackHandler(); template2.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch); fail("Shouldn't have executed query without a connection"); } catch (CannotGetJdbcConnectionException ex) { // pass assertTrue("Check root cause", ex.getCause() == sex); } ctrlDataSource.verify(); }
From source file:org.springframework.jdbc.core.JdbcTemplateTests.java
/** * If beanProperty is true, initialize via exception translator bean property; * if false, use afterPropertiesSet().//from w w w . java 2s. co m */ private void doTestCouldntGetConnectionInOperationWithExceptionTranslatorInitialized(boolean beanProperty) throws SQLException { SQLException sex = new SQLException("foo", "07xxx"); ctrlConnection = MockControl.createControl(Connection.class); mockConnection = (Connection) ctrlConnection.getMock(); //mockConnection.getMetaData(); //ctrlConnection.setReturnValue(null, 1); //mockConnection.close(); //ctrlConnection.setVoidCallable(1); ctrlConnection.replay(); // Change behaviour in setUp() because we only expect one call to getConnection(): // none is necessary to get metadata for exception translator ctrlDataSource = MockControl.createControl(DataSource.class); mockDataSource = (DataSource) ctrlDataSource.getMock(); // Upfront call for metadata - no longer the case //mockDataSource.getConnection(); //ctrlDataSource.setReturnValue(mockConnection, 1); // One call for operation mockDataSource.getConnection(); ctrlDataSource.setThrowable(sex, 2); ctrlDataSource.replay(); try { JdbcTemplate template2 = new JdbcTemplate(); template2.setDataSource(mockDataSource); template2.setLazyInit(false); if (beanProperty) { // This will get a connection. template2.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator(mockDataSource)); } else { // This will cause creation of default SQL translator. // Note that only call should be effective. template2.afterPropertiesSet(); template2.afterPropertiesSet(); } RowCountCallbackHandler rcch = new RowCountCallbackHandler(); template2.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch); fail("Shouldn't have executed query without a connection"); } catch (CannotGetJdbcConnectionException ex) { // pass assertTrue("Check root cause", ex.getCause() == sex); } ctrlDataSource.verify(); ctrlConnection.verify(); }
From source file:org.springframework.jdbc.core.JdbcTemplateTests.java
public void testSQLErrorCodeTranslationWithSpecifiedDbName() throws Exception { final SQLException sex = new SQLException("I have a known problem", "99999", 1054); final String sql = "SELECT ID FROM CUSTOMER"; MockControl ctrlResultSet = MockControl.createControl(ResultSet.class); ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock(); mockResultSet.next();/* www. j a v a2s.c om*/ 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); ctrlResultSet.replay(); ctrlStatement.replay(); replay(); JdbcTemplate template = new JdbcTemplate(); template.setDataSource(mockDataSource); template.setDatabaseProductName("MySQL"); template.afterPropertiesSet(); try { template.query(sql, new RowCallbackHandler() { public void processRow(ResultSet rs) throws SQLException { throw sex; } }); fail("Should have thrown BadSqlGrammarException"); } catch (BadSqlGrammarException ex) { // expected assertTrue("Wanted same exception back, not " + ex, sex == ex.getCause()); } ctrlResultSet.verify(); ctrlStatement.verify(); }