/** * Constructor to be used when initializing using a {@link DataSource}. * @param dataSource the DataSource to be used */ protected AbstractJdbcInsert(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); }
/** * Create a JdbcTemplate for the given DataSource. * Only invoked if populating the DAO with a DataSource reference! * <p>Can be overridden in subclasses to provide a JdbcTemplate instance * with different configuration, or a custom JdbcTemplate subclass. * @param dataSource the JDBC DataSource to create a JdbcTemplate for * @return the new JdbcTemplate instance * @see #setDataSource */ protected JdbcTemplate createJdbcTemplate(DataSource dataSource) { return new JdbcTemplate(dataSource); }
/** * Constructor to be used when initializing using a {@link DataSource}. * @param dataSource the DataSource to be used */ protected AbstractJdbcCall(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); }
/** * Set the DataSource to use to obtain database connections. * Will implicitly create a new JdbcTemplate with the given DataSource. */ public void setDataSource(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); }
/** * Create a new NamedParameterJdbcTemplate for the given {@link DataSource}. * <p>Creates a classic Spring {@link org.springframework.jdbc.core.JdbcTemplate} and wraps it. * @param dataSource the JDBC DataSource to access */ public NamedParameterJdbcTemplate(DataSource dataSource) { Assert.notNull(dataSource, "DataSource must not be null"); this.classicJdbcTemplate = new JdbcTemplate(dataSource); }
@Autowired @Override public void setDataSource(DataSource dataSource) { jdbcTemplate = new JdbcTemplate(dataSource); }
@Autowired @Override public void setDataSource(DataSource dataSource) { jdbcTemplate = new JdbcTemplate(dataSource); }
@Test public void testScriptNameWithPattern() throws Exception { context = new ClassPathXmlApplicationContext("org/springframework/jdbc/config/jdbc-initialize-pattern-config.xml"); DataSource dataSource = context.getBean("dataSource", DataSource.class); assertCorrectSetup(dataSource); JdbcTemplate t = new JdbcTemplate(dataSource); assertEquals("Dave", t.queryForObject("select name from T_TEST", String.class)); }
@Test public void testBatchUpdateWithEmptyMap() throws Exception { @SuppressWarnings("unchecked") final Map<String, Integer>[] ids = new Map[0]; namedParameterTemplate = new NamedParameterJdbcTemplate(new JdbcTemplate(dataSource, false)); int[] actualRowsAffected = namedParameterTemplate.batchUpdate( "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids); assertTrue("executed 0 updates", actualRowsAffected.length == 0); }
@Test public void testBatchUpdateWithEmptyList() throws Exception { final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; JdbcTemplate template = new JdbcTemplate(this.dataSource, false); int[] actualRowsAffected = template.batchUpdate(sql, Collections.emptyList()); assertTrue("executed 0 updates", actualRowsAffected.length == 0); }
@Test @Sql("data-add-dogbert.sql") public void database1() { assertUsers(new JdbcTemplate(dataSource1), "Dilbert", "Dogbert"); }
@Test @Transactional(transactionManager = "txMgr2") @Sql(scripts = "data-add-catbert.sql", config = @SqlConfig(dataSource = "dataSource2", transactionManager = "txMgr2")) public void database2() { assertUsers(new JdbcTemplate(dataSource2), "Dilbert", "Catbert"); }
@Test @Transactional("txMgr1") @Sql(scripts = "data-add-dogbert.sql", config = @SqlConfig(transactionManager = "txMgr1")) public void database1() { assertInTransaction(true); assertUsers(new JdbcTemplate(dataSource1), "Dilbert", "Dogbert"); }
@Test @Sql("data-add-dogbert.sql") public void database1() { assertUsers(new JdbcTemplate(dataSource1), "Dilbert", "Dogbert"); }
@Test @Sql(scripts = "data-add-catbert.sql", config = @SqlConfig(dataSource = "dataSource2", transactionManager = "txMgr2")) public void database2() { assertUsers(new JdbcTemplate(dataSource2), "Dilbert", "Catbert"); }
@Test @Sql(scripts = "data-add-catbert.sql", config = @SqlConfig(transactionManager = "txMgr2")) public void database2() { assertInTransaction(false); assertUsers(new JdbcTemplate(dataSource2), "Dilbert", "Catbert"); }
@Test @Sql(scripts = "data-add-dogbert.sql", config = @SqlConfig(transactionManager = "txMgr1")) public void database1() { assertInTransaction(false); assertUsers(new JdbcTemplate(dataSource1), "Dilbert", "Dogbert"); }
@Test public void testCouldNotGetConnectionForOperationOrExceptionTranslator() throws SQLException { SQLException sqlException = new SQLException("foo", "07xxx"); this.dataSource = mock(DataSource.class); given(this.dataSource.getConnection()).willThrow(sqlException); JdbcTemplate template = new JdbcTemplate(this.dataSource, false); RowCountCallbackHandler rcch = new RowCountCallbackHandler(); this.thrown.expect(CannotGetJdbcConnectionException.class); this.thrown.expect(exceptionCause(sameInstance(sqlException))); template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch); }
@Before public void setUp() { db = new EmbeddedDatabaseBuilder().setType(getEmbeddedDatabaseType()).build(); jdbcTemplate = new JdbcTemplate(db); }
@Test public void testFactoryBeanLifecycle() throws Exception { EmbeddedDatabaseFactoryBean bean = new EmbeddedDatabaseFactoryBean(); ResourceDatabasePopulator populator = new ResourceDatabasePopulator(resource("db-schema.sql"), resource("db-test-data.sql")); bean.setDatabasePopulator(populator); bean.afterPropertiesSet(); DataSource ds = bean.getObject(); JdbcTemplate template = new JdbcTemplate(ds); assertEquals("Keith", template.queryForObject("select NAME from T_TEST", String.class)); bean.destroy(); }