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.jasig.schedassist.impl.owner.SpringJDBCOwnerDaoImplTest.java

/**
 * //from w w  w  .j  a  va 2  s.c  o m
 * @throws Exception
 */
@After
public void destroyDatabase() throws Exception {
    Resource destroyDdl = (Resource) this.applicationContext.getBean("destroyDdl");

    String sql = IOUtils.toString(destroyDdl.getInputStream());
    JdbcTemplate template = new JdbcTemplate((DataSource) this.applicationContext.getBean("dataSource"));
    template.execute(sql);

    // always clear the CalendarUserDao in case a mock was temporarily set
    ownerDao.setCalendarAccountDao(null);
}

From source file:net.sourceforge.subsonic.dao.schema.Schema47Upgrade.java

@Override
public void execute(JdbcTemplate template) {

    if (template.queryForInt("select count(*) from version where version = 100") == 0) {
        LOG.info("Updating database schema to version 100.");
        template.execute("insert into version values (100)");
    }// ww  w  .  j a  va2s  . co  m

    // Added in 4.7.beta3
    if (!rowExists(template, "table_name='ALBUM' and column_name='NAME' and ordinal_position=1",
            "information_schema.system_indexinfo")) {
        template.execute("create index idx_album_name on album(name)");
    }

    // Added new Usersettings
    if (!columnExists(template, "list_type", "user_settings")) {
        LOG.info("Database column 'user_settings.list_type' not found.  Creating it.");
        template.execute("alter table user_settings add list_type varchar default 'random' not null");
        LOG.info("Database column 'user_settings.list_type' was added successfully.");
    }

    // Update user_settings
    if (!columnExists(template, "list_rows", "user_settings")) {
        LOG.info("Database column 'user_settings.list_rows' not found.  Creating it.");
        template.execute("alter table user_settings add list_rows int default 2");
        LOG.info("Database column 'user_settings.list_rows' was added successfully.");
    }

    if (!columnExists(template, "list_columns", "user_settings")) {
        LOG.info("Database column 'user_settings.list_columns' not found.  Creating it.");
        template.execute("alter table user_settings add list_columns int default 5");
        LOG.info("Database column 'user_settings.list_columns' was added successfully.");
    }

    if (!columnExists(template, "playqueue_resize", "user_settings")) {
        LOG.info("Database column 'user_settings.playqueue_resize' not found.  Creating it.");
        template.execute("alter table user_settings add playqueue_resize boolean default false not null");
        LOG.info("Database column 'user_settings.playqueue_resize' was added successfully.");
    }

    if (!columnExists(template, "leftframe_resize", "user_settings")) {
        LOG.info("Database column 'user_settings.leftframe_resize' not found.  Creating it.");
        template.execute("alter table user_settings add leftframe_resize boolean default false not null");
        LOG.info("Database column 'user_settings.leftframe_resize' was added successfully.");
    }

    if (!columnExists(template, "leftframe_resize", "user_settings")) {
        LOG.info("Database column 'user_settings.leftframe_resize' not found.  Creating it.");
        template.execute("alter table user_settings add leftframe_resize boolean default false not null");
        LOG.info("Database column 'user_settings.leftframe_resize' was added successfully.");
    }

    // Update album Table
    if (!columnExists(template, "SetName", "album")) {
        LOG.info("Database column 'album.SetName' not found.  Creating it.");
        template.execute("alter table album add SetName varchar");
        template.execute("create index idx_album_SetName on album(SetName)");
        LOG.info("Database column 'album.SetName' was added successfully.");
    }

    // Update media_file
    if (!columnExists(template, "override", "media_file")) {
        LOG.info("Database column 'media_file.override' not found.  Creating it.");
        template.execute("alter table media_file add override boolean default false not null");
        LOG.info("Database column 'media_file.override' was added successfully.");
    }

    if (!columnExists(template, "album_name", "media_file")) {
        LOG.info("Database column 'media_file.album_name' not found.  Creating it.");
        template.execute("alter table media_file add album_name varchar");
        template.execute("create index idx_media_file_album_name on media_file(album_name)");
        LOG.info("Database column 'media_file.album_name' was added successfully.");
    }

    // Update to new Version System
    if (template.queryForInt("select count(*) from version where version = 21") == 1) {
        template.execute("delete from version where version= 21");

        if (template.queryForInt("select count(*) from version where version = 31") == 1) {
            template.execute("update version set version = 101 where version= 31");
        }

        if (template.queryForInt("select count(*) from version where version = 32") == 1) {
            template.execute("update version set version = 102 where version= 32");
        }

        if (template.queryForInt("select count(*) from version where version = 33") == 1) {
            template.execute("update version set version = 103 where version= 33");
        }

        if (template.queryForInt("select count(*) from version where version = 34") == 1) {
            template.execute("update version set version = 104 where version= 34");
        }

        if (template.queryForInt("select count(*) from version where version = 35") == 1) {
            template.execute("update version set version = 105 where version= 35");
        }

        if (template.queryForInt("select count(*) from version where version = 36") == 1) {
            template.execute("update version set version = 106 where version= 36");
        }

        LOG.info("Updating database schema to new Version System.");
    }

}

From source file:cc.notsoclever.examples.DatabaseBean.java

public void create() throws Exception {
    JdbcTemplate jdbc = new JdbcTemplate(dataSource);

    String sql = "create table company (\n"
            + "  ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),\n"
            + "  name varchar(30),\n" + "  symbol varchar(10)\n" + ")";

    LOG.info("Creating table company ...");

    try {//ww  w .  j a  v a  2 s .  c om
        jdbc.execute("drop table company");
    } catch (Throwable e) {
        // ignore
    }

    jdbc.execute(sql);

    LOG.info("... created table company");
}

From source file:com.github.viktornar.migration.schema.Schema.java

/**
 * Drops given tables by given names of tables.
 *
 * @param template//from w  w w .  ja va 2  s .  co m
 * @param tablesToDrop
 */
protected void drop(JdbcTemplate template, List<String> tablesToDrop) {
    assert template != null;
    assert tablesToDrop != null;

    if (tablesToDrop.size() > 0) {
        String tableList = join(tablesToDrop.toArray(), ", ");
        String dropSql = format("DROP TABLE %s", tableList);
        template.execute(dropSql);
        logger.info(format("Database tables '%s' was dropped successfully.", tableList));
    } else {
        logger.info(format("There are no tables to drop in database."));
    }
}

From source file:org.awesomeagile.dao.testing.TestDatabase.java

private void waitForDatabase(int retries) {
    JdbcTemplate jdbcTemplate = jdbcTemplate();
    int left = retries;
    while (left > 0) {
        left--;/* w  w w  . jav  a 2s .  c  o m*/
        System.out.println(left + " attempts left");
        try {
            Thread.sleep(1000);
            jdbcTemplate.execute("select 1");
            return;
        } catch (Exception ex) {
            // ignore
        }
    }
    throw new RuntimeException("Database did not come up after " + retries + " attempts");
}

From source file:com.swcguild.blacksmithblogcapstone.dao.NewEmptyJUnitTest.java

@Before
public void setUp() {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("test-applicationContext.xml");
    dao = ctx.getBean("testDao", BlackSmithDao.class);

    JdbcTemplate cleaner = ctx.getBean("jdbcTemplate", JdbcTemplate.class);

    cleaner.execute("DELETE FROM BlogEntriesCategories");
    cleaner.execute("DELETE FROM BlogEntriesKeywords");
    cleaner.execute("DELETE FROM Categories");
    cleaner.execute("DELETE FROM Comments");
    cleaner.execute("DELETE FROM Keywords");
    cleaner.execute("DELETE FROM BlogEntries");

}

From source file:org.jboss.fuse.examples.jdbc.DatabaseBean.java

public void create() throws Exception {
    JdbcTemplate jdbc = new JdbcTemplate(dataSource);

    String sequence = "CREATE SEQUENCE aln_id AS int start WITH 1";
    String sql = "create table ALIEN (\n" + "  aln_id integer primary key,\n" + "  aln_name varchar(60),\n"
            + "  aln_fingerprint varchar(60)\n" + ")";

    LOG.info("Creating table ALIEN ...");

    try {/*  w w  w. j  a  va2 s. c o m*/
        jdbc.execute("drop table ALIEN");
    } catch (Throwable e) {
        // ignore
    }

    try {
        jdbc.execute("drop sequence aln_id");
    } catch (Throwable e) {
        // ignore
    }

    jdbc.execute(sequence);
    jdbc.execute(sql);

    LOG.info("... created table ALIEN");
}

From source file:io.cloudslang.engine.data.SimpleHiloIdentifierGenerator.java

private void updateCurrentChunk() {
    if (logger.isDebugEnabled()) {
        logger.debug("Updating HILO chunk...");
    }/*  ww w  .  ja  v a2s .c o m*/

    long t = System.currentTimeMillis();
    try (Connection conn = dataSource.getConnection()) {
        conn.setAutoCommit(false);
        JdbcTemplate jdbcTemplate = new JdbcTemplate(new SingleConnectionDataSource(conn, true));

        jdbcTemplate.update(SQL_LOCK);
        currentChunk = jdbcTemplate.queryForObject(SQL_SELECT, Integer.class);
        if (logger.isDebugEnabled())
            logger.debug("Current chunk: " + currentChunk);
        jdbcTemplate.execute(SQL_UPDATE);
        jdbcTemplate.execute("commit");

        if (logger.isDebugEnabled()) {
            logger.debug("Updating HILO chunk done in " + (System.currentTimeMillis() - t) + " ms");
        }
        currentId = 0;
    } catch (SQLException e) {
        logger.error("Unable to update current chunk", e);
        throw new IllegalStateException("Unable to update current chunk");
    }
}

From source file:data.DefaultExchanger.java

private void truncateTable(JdbcTemplate jdbcTemplate) {
    play.Logger.debug("truncate table {}", getTable());
    jdbcTemplate.execute("TRUNCATE TABLE " + getTable());
    play.Logger.debug("truncated table {}", getTable());
}

From source file:com.aegis.cms.dao.PostTagDaoTest.java

@After
public void tearDown() {
    JdbcTemplate cleaner = (JdbcTemplate) ctx.getBean("jdbcTemplate");
    cleaner.execute("delete from post");
    cleaner.execute("delete from users");
    cleaner.execute("delete from tag");
}