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:com.acme.spring.jdbc.JdbcTestHelper.java

/**
 * <p>Executes a sql script.</p>
 *
 * @param jdbcTemplate the jdbc template
 * @param fileName     the file name//from   www.j ava  2s.  co m
 *
 * @throws IOException if any error occurs
 */
public static void runScript(JdbcTemplate jdbcTemplate, String fileName) throws IOException {

    // retrieves the resource from class path
    InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);

    BufferedReader inputReader = new BufferedReader(new InputStreamReader(input));

    // loads the entire file
    StringBuilder stringBuilder = new StringBuilder();
    String line;
    while ((line = inputReader.readLine()) != null) {

        if (!line.startsWith("--")) {
            stringBuilder.append(line);
        }
    }

    // splits the commands by semicolon
    String[] commands = stringBuilder.toString().split(";");

    for (String command : commands) {

        jdbcTemplate.execute(command);
    }
}

From source file:com.googlecode.flyway.sample.appengine.migration.V1_2__Another_user.java

public void migrate(JdbcTemplate jdbcTemplate) throws Exception {
    jdbcTemplate.execute("INSERT INTO test_user (name) VALUES ('Obelix')");
}

From source file:flywayspike.migration.V1_2__Another_user.java

public void migrate(JdbcTemplate jdbcTemplate) throws Exception {
    jdbcTemplate.execute("INSERT INTO test_user (name) VALUES ('Obelix111')");
}

From source file:edu.wisc.my.portlets.bookmarks.dao.SqlShutdownHelper.java

public void close() {
    final JdbcTemplate jdbcTemplate = this.getJdbcTemplate();
    jdbcTemplate.execute(this.shutdownSql);
    this.logger.info("Executed '" + this.shutdownSql + "'");
}

From source file:camelinaction.InventoryCreateTable.java

public InventoryCreateTable(DataSource ds) {
    JdbcTemplate jdbc = new JdbcTemplate(ds);

    try {//from  w w  w.  ja  v  a 2  s  . c o m
        jdbc.execute("drop table partner_inventory");
    } catch (Exception e) {
        // ignore as the table may not already exists
    }
    jdbc.execute("create table partner_inventory "
            + "( supplier_id varchar(10), part_id varchar(10), name varchar(10), amount varchar(10) )");
}

From source file:camelinaction.OrderCreateTable.java

public OrderCreateTable(CamelContext camelContext) {
    DataSource ds = camelContext.getRegistry().lookupByNameAndType("myDataSource", DataSource.class);
    JdbcTemplate jdbc = new JdbcTemplate(ds);

    try {//from  w w w .  ja  v  a  2 s. com
        jdbc.execute("drop table riders_order");
    } catch (Exception e) {
        // ignore as the table may not already exists
    }
    jdbc.execute("create table riders_order "
            + "( customer_id varchar(10), ref_no varchar(10), part_id varchar(10), amount varchar(10) )");
}

From source file:com.electronicpanopticon.wb.shell.commands.CoreCommands.java

/**
 *
 * @return//from   w  w  w. j a  va2 s.  co  m
 * @throws IOException
 */
@CliCommand(value = "ping", help = "Tests the database connection.")
public String pingCommand() throws IOException {

    JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);
    jdbcTemplate.execute("SELECT relname FROM pg_class WHERE pg_table_is_visible(oid);");

    return "ping";
}

From source file:io.spring.marchmadness.BeforeMooreJobNotification.java

@Override
public void beforeJob(JobExecution jobExecution) {
    JdbcTemplate template = new JdbcTemplate(dataSource.get(0));
    template.execute("delete from MOORE_NCAA_STATS where year = " + getYearFromData());
}

From source file:org.thlim.castle.dao.BootstrapEmptyTables.java

public void afterPropertiesSet() throws Exception {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

    jdbcTemplate.execute("delete from user_roles_group");
    jdbcTemplate.execute("delete from roles_group_role");
    jdbcTemplate.execute("delete from role_permission");
    jdbcTemplate.execute("delete from roles_group");
    jdbcTemplate.execute("delete from permission");
    jdbcTemplate.execute("delete from role");
    jdbcTemplate.execute("delete from user");
}

From source file:org.xinta.eazycode.components.shiro.dao.BootstrapDataPopulator.java

public void afterPropertiesSet() {
    //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(this.dataSource);

    jdbcTemplate.execute("insert into roles values (1, 'user', 'The default role given to all users.')");
    jdbcTemplate.execute(//from  w w  w .ja  v  a2s .  com
            "insert into roles values (2, 'admin', 'The administrator role only given to site admins')");
    jdbcTemplate.execute("insert into roles_permissions values (2, 'user:*')");
    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_roles values (1, 2)");

}