Spring Framework SimpleJdbcTemplate - 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 SimpleJdbcTemplate class.

This code example shows how to use the following methods:update
 
    public void setDataSource(DataSource ds) { 
 
        // Initializes the in-memory database (not necessary in production) 
        if (!initialized) { 
            SimpleJdbcTemplate jt = new SimpleJdbcTemplate(ds); 
            jt.update("CREATE TABLE sample_table ( id INTEGER IDENTITY, str_col VARCHAR(256), num_col INTEGER)"); 
            jt.update("INSERT INTO sample_table(str_col,num_col) VALUES('Ford', 100)"); 
            jt.update("INSERT INTO sample_table(str_col,num_col) VALUES('Toyota', 200)"); 
            jt.update("INSERT INTO sample_table(str_col,num_col) VALUES('Mazda', 300)"); 
            initialized = true; 
        } 
        this.ds = ds; 
    } 
 
    public String execute() { 
 
        // Only refresh the data every minute as needed 
        long now = System.currentTimeMillis(); 
        if (lastLoaded + CACHE_TIME < now) { 
6
CodeRank
This code example shows how to use the following methods:update
    public LotteryTicketPurchasedEventHandler() { 
        super(LotteryTicketPurchasedEvent.class); 
    } 
 
    public void handleMessage(LotteryTicketPurchasedEvent message) { 
        simpleJdbcTemplate.update("insert into ticket (number, lottery_id, customer_id) values (?, ?, ?)",  
                message.getTicketNumber(),  
                message.getAggregateRootId().getId(),  
                message.getCustomerId().getId()); 
        simpleJdbcTemplate.update("update lottery set version = ? where id = ?",  
                message.getAggregateRootId().getVersion(),  
                message.getAggregateRootId().getId()); 
    } 
 
} 
6
CodeRank
This code example shows how to use the following methods:queryForObject
		rewardNetwork.rewardAccountFor(dining); 
		String sql = "select SAVINGS from T_ACCOUNT_BENEFICIARY where NAME = ?"; 
 
		// Rollback the transaction test transaction 
		transactionManager.rollback(status); 
		assertEquals(Double.valueOf(4.00), template.queryForObject(sql, Double.class, "Annabelle")); 
		assertEquals(Double.valueOf(4.00), template.queryForObject(sql, Double.class, "Corgan")); 
 
	} 
} 
5
CodeRank
This code example shows how to use the following methods:queryForObject, update
	 */ 
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED) 
	@Override 
	public String setString(Long id, String value, Throwable toThrow)throws Throwable { 
		// Insert 
		simpleJdbcTemplate.update(INSERT_INTO_TRANS_TEST_ID_NAME_VALUES, id, value, value); 
		// Now throw the exception 
		if(toThrow != null){ 
			throw toThrow; 
		} 
		return getString(id); 
	} 
 
	@Override 
	public String getString(Long id) { 
		// get the current value 
		return simpleJdbcTemplate.queryForObject(SELECT_NAME, String.class, id); 
	} 
	 
	/** 

5
CodeRank
This code example shows how to use the following methods:
 
   private SimpleJdbcInsert bookInsert; 
 
   @Autowired(required = true) 
   public void initialize(final DataSource dataSource) { 
      this.jdbcTemplate = new SimpleJdbcTemplate(dataSource); 
      this.bookInsert = new SimpleJdbcInsert(dataSource).withTableName("books") 
            .usingGeneratedKeyColumns("id"); 
   } 
 
   /** 
    * <p> 
    * Look up and return the {@code Book} identified by the supplied {@code bookId}. By annotating 
    * this method with {@code @Cacheable(value = "books", key = "#bookId")} we achieve the 
    * following: 
    * <ol> 
    * <li> 
    * {@code Book} instances returned from this method will be cached in a named 
    * {@link org.springframework.cache.Cache <code>Cache</code>} &quot;books&quot;</li> 
    * <li> 
5
CodeRank
This code example shows how to use the following methods:update
 
    @Override 
    @Transactional 
    public void deleteContact(Integer id) { 
 
        this.simpleJdbcTemplate.update(SQL_DELETE, id); 
    } 
 
    @Override 
    public Contact addContact(Contact contact) { 
 
        Map<String, Object> params = new HashMap<String, Object>(); 
        params.put("first_name", contact.getFirstName()); 
        params.put("last_name", contact.getLastName()); 
        params.put("birth_date", contact.getBirthDate()); 
 
        Number newId = simpleInsert.executeAndReturnKey(params); 
        contact.setId(newId.intValue()); 
 
        return contact; 
3
CodeRank
This code example shows how to use the following methods:
	 
	protected void checkDaoConfig() { 
		super.checkDaoConfig(); 
		if(dialect == null) throw new IllegalStateException("'dialect' property must be not null"); 
		log.info("use jdbc dialect:"+dialect); 
		simpleJdbcTemplate = new SimpleJdbcTemplate(getJdbcTemplate()); 
		namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(getJdbcTemplate()); 
	} 
	 
	public SimpleJdbcTemplate getSimpleJdbcTemplate() { 
		return simpleJdbcTemplate; 
	} 
 
	public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() { 
		return namedParameterJdbcTemplate; 
	} 
	 
	public Object getIdentifierPropertyValue(Object entity) { 
		try { 
			return PropertyUtils.getProperty(entity, getIdentifierPropertyName()); 
3
CodeRank
This code example shows how to use the following methods:queryForObject
	} 
	 
	@Transactional 
	public String getUserNameFailTwice() { 
		count1++; 
		String user =  
			simpleJdbcTemplate.queryForObject("select {fn user()} from dual", String.class); 
		if (count1 <= 2) { 
            SQLException se = new SQLException("First Error-"+count1, "1000", 1000); 
			throw new InvalidDataAccessApiUsageException("First Failure", se); 
		} 
 
		return user; 
	} 
 
	@Transactional 
	public String getUserNameFailAlways(int id) { 
		if (id > 0) { 
			count2++; 
		} 
4
CodeRank
This code example shows how to use the following methods:queryForInt
        String s = "<item id=\"11\">\n" + 
                "  <itemName>Bar</itemName>\n" + 
                "  <price>311</price>\n" + 
                "</item>"; 
        xmlDao.addXmlItem(11L, s); 
        int count = simpleJdbcTemplate.queryForInt("select count(*) from xml_table where id = 11"); 
        assertEquals("actor not added", 1, count); 
        String result = xmlDao.getXmlItem(11L); 
        assertTrue("xml text not found", result.contains("<itemName>Bar</itemName>")); 
    } 
 
    @Transactional @Test 
    public void marshallAndUnsmarshallXml() { 
        Item i = new Item(); 
        i.setId(2L); 
        i.setName("Bar"); 
        i.setPrice(new BigDecimal("123.45")); 
        xmlDao.addItem(i); 
        int count = simpleJdbcTemplate.queryForInt("select count(*) from xml_table where id = 2"); 
        assertEquals("item not added", 1, count); 
4
CodeRank
This code example shows how to use the following methods:queryForList
     * @param username 
     * @return KEY:[value: 允许失败次数; fail_set_type: 失败后设置(0,验证码; 1,锁定用户); locked_time:锁定时间(分钟);] 
     */ 
    public Map<String, Object> getSecuritySetting(String username){ 
    	Map<String, Object> result = new HashMap<String, Object>(); 
    	List<Map<String, Object>> list = jdbcTemplate.queryForList(SETTING_SQL, username); 
    	if(list.isEmpty()){ 
        	result.put("value", 3); 
        	result.put("fail_set_type", 0); 
        	result.put("locked_time", 30); 
    	}else{ 
        	result.put("value", list.get(0).get("value")); 
        	result.put("fail_set_type", list.get(0).get("fail_set_type")); 
        	result.put("locked_time", list.get(0).get("locked_time")); 
    	} 
    	return result; 
    } 
     
    public final void setDataSource(final DataSource dataSource) { 
        this.jdbcTemplate = new SimpleJdbcTemplate(dataSource); 
4
CodeRank
See Code Examples for Spring Framework SimpleJdbcTemplate Methods: