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:net.sourceforge.subsonic.backend.dao.schema.Schema.java

/**
 * Returns whether the given table exists.
 * @param template The JDBC template to use.
 * @param table The table in question.//from  ww w.  ja v a 2s  .c  om
 * @return Whether the table exists.
 */
protected boolean tableExists(JdbcTemplate template, String table) {
    try {
        template.execute("select 1 from " + table);
    } catch (Exception x) {
        return false;
    }
    return true;
}

From source file:com.hp.autonomy.frontend.find.core.AbstractDatabaseInitIT.java

/**
 * Check connection to our configured datasource.
 *
 * Depending on the datasource a dummy statement must sometimes
 * be executed to ensure a valid connection.
 *///w  ww  .j a  v  a  2  s .co  m
@Test
public void connectToDatabase() {
    final JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(dataSource);
    jdbcTemplate.execute("SELECT true");
}

From source file:org.apache.shiro.samples.sprhib.dao.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 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(//ww  w  . j av a 2  s.  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)");

}

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

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

    try {//from www.  j a va 2 s .  com
        jdbc.execute("drop table company");
    } catch (Throwable e) {
        // ignore
    }
}

From source file:com.demo.camelrestsqldemo.DatabaseBean.java

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

    try {//  ww  w .  ja  v  a2  s . c  o m
        jdbc.execute("drop table orders");
    } catch (Throwable e) {
        // ignore
    }
}

From source file:com.github.springtestdbunit.multiconnection.MultiConnectionTest.java

@Test
@ExpectedDatabase(connection = "dataSource2", value = "/META-INF/db/multi-expected.xml")
public void testExpected() throws Exception {
    JdbcTemplate jdbc = new JdbcTemplate(this.dataSource);
    jdbc.execute("insert into second(id, value) values (200, 'abc')");
}

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

@Override
public void beforeJob(JobExecution jobExecution) {
    JdbcTemplate template = new JdbcTemplate(dataSource.get(0));
    getYearFromData();//from www .ja v  a 2 s  .  co  m
    template.execute("delete from NCAA_STATS where year = " + getYearFromData());
}

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

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

    try {//www.j  a v a2s  . c om
        jdbc.execute("drop table ALIEN");
    } catch (Throwable e) {
        // ignore
    }
}

From source file:net.sourceforge.subsonic.backend.dao.schema.Schema.java

/**
 * Returns whether the given column in the given table exists.
 * @param template The JDBC template to use.
 * @param column The column in question.
 * @param table The table in question./*from  w ww  .  j av a  2 s .c  o m*/
 * @return Whether the column exists.
 */
protected boolean columnExists(JdbcTemplate template, String column, String table) {
    try {
        template.execute("select " + column + " from " + table + " where 1 = 0");
    } catch (Exception x) {
        return false;
    }
    return true;
}

From source file:org.activiti.spring.test.transaction.SpringTransactionIntegrationTest.java

@Deployment
public void testRollbackTransactionOnActivitiException() {

    // Create a table that the userBean is supposed to fill with some data
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    jdbcTemplate.execute("create table MY_TABLE (MY_TEXT varchar);");

    // The hello() method will start the process. The process will wait in a user task
    userBean.hello();//from w ww.  j ava 2 s  .  co  m
    assertEquals(0, jdbcTemplate.queryForLong("select count(*) from MY_TABLE"));

    // The completeTask() method will write a record to the 'MY_TABLE' table and complete the user task
    try {
        userBean.completeTask(taskService.createTaskQuery().singleResult().getId());
        fail();
    } catch (Exception e) {
    }

    // Since the service task after the user tasks throws an exception, both 
    // the record and the process must be rolled back !
    assertEquals("My Task", taskService.createTaskQuery().singleResult().getName());
    assertEquals(0, jdbcTemplate.queryForLong("select count(*) from MY_TABLE"));

    // Cleanup
    jdbcTemplate.execute("drop table MY_TABLE if exists;");
}