Search in sources :

Example 6 with NamedParameterJdbcOperations

use of org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations in project JavaForFun by gumartinm.

the class RawSpringJDBCWithPoolExample method main.

public static void main(final String[] args) throws PropertyVetoException {
    // Just for fun, programmatic configuration.
    final DataSource dataSource = getDataSource();
    final Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("AD_ID", 1);
    // 3. Using Spring JdbcTemplate
    final JdbcOperations jdbcTemplate = new JdbcTemplate(dataSource);
    jdbcTemplate.execute("SELECT * FROM AD");
    // 4. Using SimpleJdbcTemplate
    final SimpleJdbcOperations simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
    final int deprecatedResult = simpleJdbcTemplate.queryForInt("SELECT * FROM AD", parameters);
    logger.info("Deprecated result: " + deprecatedResult);
    // 5. Using NamedParameterJdbcTemplate
    final NamedParameterJdbcOperations namedParameterJdbcOperations = new NamedParameterJdbcTemplate(dataSource);
    final int namedResult = namedParameterJdbcOperations.queryForInt("SELECT * FROM AD", parameters);
    logger.info("Named result: " + namedResult);
    // 6. Using Spring SimpleJdbcInsert
    final SimpleJdbcInsertOperations simpleJdbcInsert = new SimpleJdbcInsert(dataSource);
    simpleJdbcInsert.withTableName("ad");
    simpleJdbcInsert.execute(parameters);
    // 7. Using Spring SimpleJdbcCall
    final SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(dataSource);
    // Now we close the whole pool :)
    ((ComboPooledDataSource) dataSource).close();
}
Also used : HashMap(java.util.HashMap) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) JdbcOperations(org.springframework.jdbc.core.JdbcOperations) NamedParameterJdbcOperations(org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations) SimpleJdbcOperations(org.springframework.jdbc.core.simple.SimpleJdbcOperations) SimpleJdbcTemplate(org.springframework.jdbc.core.simple.SimpleJdbcTemplate) SimpleJdbcOperations(org.springframework.jdbc.core.simple.SimpleJdbcOperations) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) SimpleJdbcTemplate(org.springframework.jdbc.core.simple.SimpleJdbcTemplate) SimpleJdbcCall(org.springframework.jdbc.core.simple.SimpleJdbcCall) ComboPooledDataSource(com.mchange.v2.c3p0.ComboPooledDataSource) DataSource(javax.sql.DataSource) NamedParameterJdbcOperations(org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations) ComboPooledDataSource(com.mchange.v2.c3p0.ComboPooledDataSource) SimpleJdbcInsertOperations(org.springframework.jdbc.core.simple.SimpleJdbcInsertOperations) SimpleJdbcInsert(org.springframework.jdbc.core.simple.SimpleJdbcInsert)

Aggregations

NamedParameterJdbcOperations (org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations)6 DefaultDataAccessStrategy (org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy)3 JdbcRepositoryFactory (org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory)3 HashMap (java.util.HashMap)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Test (org.junit.jupiter.api.Test)2 BasicJdbcConverter (org.springframework.data.jdbc.core.convert.BasicJdbcConverter)2 DefaultJdbcTypeFactory (org.springframework.data.jdbc.core.convert.DefaultJdbcTypeFactory)2 DelegatingDataAccessStrategy (org.springframework.data.jdbc.core.convert.DelegatingDataAccessStrategy)2 JdbcConverter (org.springframework.data.jdbc.core.convert.JdbcConverter)2 JdbcCustomConversions (org.springframework.data.jdbc.core.convert.JdbcCustomConversions)2 SqlGeneratorSource (org.springframework.data.jdbc.core.convert.SqlGeneratorSource)2 JdbcMappingContext (org.springframework.data.jdbc.core.mapping.JdbcMappingContext)2 Dialect (org.springframework.data.relational.core.dialect.Dialect)2 H2Dialect (org.springframework.data.relational.core.dialect.H2Dialect)2 HsqlDbDialect (org.springframework.data.relational.core.dialect.HsqlDbDialect)2 RelationalMappingContext (org.springframework.data.relational.core.mapping.RelationalMappingContext)2 JdbcOperations (org.springframework.jdbc.core.JdbcOperations)2 KeyHolder (org.springframework.jdbc.support.KeyHolder)2 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)1