Example usage for org.springframework.jdbc.core JdbcTemplate execute

List of usage examples for org.springframework.jdbc.core JdbcTemplate execute

Introduction

In this page you can find the example usage for org.springframework.jdbc.core JdbcTemplate execute.

Prototype

@Override
    public void execute(final String sql) throws DataAccessException 

Source Link

Usage

From source file:org.springframework.test.jdbc.JdbcTestUtils.java

/**
 * Drop the specified tables.//from www. jav a2s .  co  m
 * @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations
 * @param tableNames the names of the tables to drop
 */
public static void dropTables(JdbcTemplate jdbcTemplate, String... tableNames) {
    for (String tableName : tableNames) {
        jdbcTemplate.execute("DROP TABLE " + tableName);
        if (logger.isInfoEnabled()) {
            logger.info("Dropped table " + tableName);
        }
    }
}

From source file:org.wicketstuff.shiro.example.jdbc.BootstrapDataPopulator.java

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 create the tables and insert the 2 sample users on bootstrap:

    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    jdbcTemplate.execute(CREATE_TABLES);

    // password is 'user' SHA hashed and base64 encoded:
    // The first argument to the hash constructor is the actual value to be hased. The 2nd is
    // the//w  ww .  jav  a2 s .  co m
    // salt. In this simple demo scenario, the username and the password are the same, but to
    // clarify the
    // distinction, you would see this in practice:
    // new Sha256Hash( <password>, <username> )
    String query = "insert into users values ('user', '" + new Sha256Hash("user", "user").toBase64() + "' )";
    jdbcTemplate.execute(query);
    log.debug("Created user.");

    // password is 'admin' SHA hashed and base64 encoded:
    query = "insert into users values ( 'admin', '" + new Sha256Hash("admin", "admin").toBase64() + "' )";
    jdbcTemplate.execute(query);
    log.debug("Created admin.");

    query = "insert into roles values ( 'user' )";
    jdbcTemplate.execute(query);
    log.debug("Created user");

    query = "insert into roles values ( 'admin' )";
    jdbcTemplate.execute(query);
    log.debug("Created admin");

    query = "insert into roles_permissions values ( 'user', 'view')";
    jdbcTemplate.execute(query);
    log.debug("Created permission view for role user");

    query = "insert into roles_permissions values ( 'admin', 'user:*')";
    jdbcTemplate.execute(query);
    log.debug("Created permission user:* for role admin");

    query = "insert into user_roles values ( 'user', 'user' )";
    jdbcTemplate.execute(query);
    log.debug("Assigned user role user");

    query = "insert into user_roles values ( 'admin', 'admin' )";
    jdbcTemplate.execute(query);
    log.debug("Assigned admin role admin");
}

From source file:test.jdbc.datasource.DataSourceInitializer.java

private void doExecuteScript(final Resource scriptResource) {
    if (scriptResource == null || !scriptResource.exists())
        return;//from w  ww  .ja v  a2s  . c  om
    final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String[] scripts;
    try {
        String[] list = StringUtils.delimitedListToStringArray(
                stripComments(IOUtils.readLines(scriptResource.getInputStream())), ";");
        scripts = list;
    } catch (IOException e) {
        throw new BeanInitializationException("Cannot load script from [" + scriptResource + "]", e);
    }
    for (int i = 0; i < scripts.length; i++) {
        final String script = scripts[i].trim();
        TransactionTemplate transactionTemplate = new TransactionTemplate(
                new DataSourceTransactionManager(dataSource));
        transactionTemplate.execute(new TransactionCallback<Void>() {

            @Override
            public Void doInTransaction(TransactionStatus status) {
                if (StringUtils.hasText(script)) {
                    try {
                        jdbcTemplate.execute(script);
                    } catch (DataAccessException e) {
                        if (!script.toUpperCase().startsWith("DROP")) {
                            throw e;
                        }
                    }
                }
                return null;
            }

        });
    }

}