Spring Framework JdbcTemplate - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Spring Framework JdbcTemplate class.

This code example shows how to use the following methods:execute
	public void afterPropertiesSet() throws Exception 
	{ 
		// because we're using an in-memory hsqldb for the sample app, a new one will be created 
// each time the 
		// app starts, so insert the sample admin user at startup: 
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); 
 
		jdbcTemplate.execute("insert into roles(id,name,description) values (1, 'user', 'The default role given to all users.')"); 
		jdbcTemplate.execute("insert into roles(id,name,description) values (2, 'admin', 'The admin role only given to site admins')"); 
		jdbcTemplate.execute("insert into roles_permissions values (2, 'user:*')"); 
		jdbcTemplate.execute("insert into roles_permissions values (2, 'view')"); 
		jdbcTemplate.execute("insert into users(id,username,email,password) values (1, 'admin', 'sample@shiro.apache.org', '" + 
			new Sha256Hash("admin").toHex() + "')"); 
		jdbcTemplate.execute("insert into users(id,username,email,password) values (2, 'user', 'sample@shiro.apache.org', '" + 
			new Sha256Hash("user").toHex() + "')"); 
		jdbcTemplate.execute("insert into users_roles values (1, 2)"); 
		jdbcTemplate.execute("insert into users_roles values (2, 1)"); 
 
	} 
} 

10
CodeRank
This code example shows how to use the following methods:
	protected JdbcTemplate jdbcTemplate;
 
 
	@Autowired 
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	} 
 
	@Test
	// test##_ prefix is required for @FixMethodOrder. 
	public void test01_classLevelScripts() { 
		assertNumUsers(1); 
	} 
 
	@Test
	@Sql("data-add-dogbert.sql") 
	// test##_ prefix is required for @FixMethodOrder. 
	public void test02_methodLevelScripts() { 
		assertNumUsers(2); 
	} 
9
CodeRank
This code example shows how to use the following methods:execute
	public void afterPropertiesSet() throws Exception 
	{ 
		// because we're using an in-memory hsqldb for the sample app, a new one will be created 
// each time the 
		// app starts, so insert the sample admin user at startup: 
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); 
 
		jdbcTemplate.execute("insert into roles(id,name,description) values (1, 'user', 'The default role given to all users.')"); 
		jdbcTemplate.execute("insert into roles(id,name,description) values (2, 'admin', 'The admin role only given to site admins')"); 
		jdbcTemplate.execute("insert into roles_permissions values (2, 'user:*')"); 
		jdbcTemplate.execute("insert into roles_permissions values (2, 'view')"); 
		jdbcTemplate.execute("insert into users(id,username,email,password) values (1, 'admin', 'sample@shiro.apache.org', '" + 
			new Sha256Hash("admin").toHex() + "')"); 
		jdbcTemplate.execute("insert into users(id,username,email,password) values (2, 'user', 'sample@shiro.apache.org', '" + 
			new Sha256Hash("user").toHex() + "')"); 
		jdbcTemplate.execute("insert into users_roles values (1, 2)"); 
		jdbcTemplate.execute("insert into users_roles values (2, 1)"); 
 
	} 
} 

10
CodeRank
This code example shows how to use the following methods:
	protected JdbcTemplate jdbcTemplate;
 
 
	@Autowired 
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	} 
 
	@Test
	// test##_ prefix is required for @FixMethodOrder. 
	public void test01_classLevelScripts() { 
		assertNumUsers(1); 
	} 
 
	@Test
	@Sql({ "drop-schema.sql", "schema.sql", "data.sql", "data-add-dogbert.sql" }) 
	// test##_ prefix is required for @FixMethodOrder. 
	public void test02_methodLevelScripts() { 
		assertNumUsers(2); 
	} 
9
CodeRank
This code example shows how to use the following methods:
 
 
	@Test 
	@Sql("data-add-dogbert.sql") 
	public void database1() { 
		assertUsers(new JdbcTemplate(dataSource1), "Dilbert", "Dogbert"); 
	} 
 
	@Test 
	@Transactional("txMgr2") 
	@Sql(scripts = "data-add-catbert.sql", config = @SqlConfig(dataSource = "dataSource2", transactionManager = "txMgr2")) 
	public void database2() { 
		assertUsers(new JdbcTemplate(dataSource2), "Dilbert", "Catbert"); 
	} 
 
	private void assertUsers(JdbcTemplate jdbcTemplate, String... users) { 
		List<String> expected = Arrays.asList(users); 
		Collections.sort(expected); 
		List<String> actual = jdbcTemplate.queryForList("select name from user", String.class); 
		Collections.sort(actual); 
9
CodeRank
This code example shows how to use the following methods:
	@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 
	@Transactional("txMgr2") 
	@Sql(scripts = "data-add-catbert.sql", config = @SqlConfig(transactionManager = "txMgr2")) 
	public void database2() { 
		assertInTransaction(true); 
		assertUsers(new JdbcTemplate(dataSource2), "Dilbert", "Catbert"); 
	} 
 
	private void assertUsers(JdbcTemplate jdbcTemplate, String... users) { 
		List<String> expected = Arrays.asList(users); 
		Collections.sort(expected); 
		List<String> actual = jdbcTemplate.queryForList("select name from user", String.class); 
9
CodeRank
This code example shows how to use the following methods:
	} 
 
	@Autowired 
	public void setDataSource(DataSource dataSource) { 
		this.dataSourceViaInjection = dataSource; 
		this.jdbcTemplate = new JdbcTemplate(dataSource); 
	} 
 
	private int countRowsInTable(String tableName) { 
		return jdbcTemplate.queryForObject("SELECT COUNT(0) FROM " + tableName, Integer.class); 
	} 
 
	private int createPerson(String name) { 
		return jdbcTemplate.update("INSERT INTO person VALUES(?)", name); 
	} 
 
	protected int deletePerson(String name) { 
		return jdbcTemplate.update("DELETE FROM person WHERE name=?", name); 
	} 
 
9
CodeRank
This code example shows how to use the following methods:
	 * @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); 
	} 
 
	/** 
	 * Return the JDBC DataSource used by this DAO. 
	 */ 
	public final DataSource getDataSource() { 
		return (this.jdbcTemplate != null ? this.jdbcTemplate.getDataSource() : null); 
	} 
 
	/** 
	 * Set the JdbcTemplate for this DAO explicitly, 
	 * as an alternative to specifying a DataSource. 
	 */ 
	public final void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 
9
CodeRank
This code example shows how to use the following methods:
		setColumnNames(Arrays.asList(columnNames));
		return this;
	} 
 
	@Override 
	public SimpleJdbcInsert usingGeneratedKeyColumns(String... columnNames) {
		setGeneratedKeyNames(columnNames);
		return this;
	} 
 
	@Override 
	public SimpleJdbcInsertOperations withoutTableColumnMetaDataAccess() {
		setAccessTableColumnMetaData(false);
		return this;
	} 
 
	@Override 
	public SimpleJdbcInsertOperations includeSynonymsForTableColumnMetaData() {
		setOverrideIncludeSynonymsDefault(true);
		return this;
9
CodeRank
This code example shows how to use the following methods:
	@Test 
	public void createAndDestroy() throws Exception { 
		ClassPathXmlApplicationContext context = context("jdbc-destroy-config.xml"); 
		try { 
			DataSource dataSource = context.getBean(DataSource.class); 
			JdbcTemplate template = new JdbcTemplate(dataSource); 
			assertNumRowsInTestTable(template, 1); 
			context.getBean(DataSourceInitializer.class).destroy(); 
			expected.expect(BadSqlGrammarException.class); // Table has been dropped 
			assertNumRowsInTestTable(template, 1); 
		} 
		finally { 
			context.close(); 
		} 
	} 
 
	@Test 
	public void createAndDestroyNestedWithHsql() throws Exception { 
		ClassPathXmlApplicationContext context = context("jdbc-destroy-nested-config.xml"); 
		try { 
9
CodeRank
See Code Examples for Spring Framework JdbcTemplate Methods: