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.kiriboum.sub.CustomSchema.java

@Override
public void execute(JdbcTemplate template) {

    if (!columnExists(template, "url", "player")) {
        LOG.info("Database column 'player.url' not found.  Creating it.");
        template.execute("alter table player add url varchar");
        LOG.info("Database column 'player.url' was added successfully.");
    }// w w w  .  j ava  2 s. c om
}

From source file:org.apache.shiro.samples.spring.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(this.dataSource);
    jdbcTemplate.execute(CREATE_TABLES);

    //password is 'user1' SHA hashed and base64 encoded:
    //The first argument to the hash constructor is the actual value to be hased.  The 2nd is the
    //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>, <cryptographically strong randomly generated salt> (not the username!) )
    String query = "insert into users values ('user1', '" + new Sha256Hash("user1", "user1").toBase64() + "' )";
    jdbcTemplate.execute(query);/*from  ww w  .j av a2s .c  om*/
    log.debug("Created user1.");

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

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

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

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

    query = "insert into roles_permissions values ( 'role1', 'permission2')";
    jdbcTemplate.execute(query);
    log.debug("Created permission 2 for role 1");

    query = "insert into roles_permissions values ( 'role2', 'permission1')";
    jdbcTemplate.execute(query);
    log.debug("Created permission 1 for role 2");

    query = "insert into user_roles values ( 'user1', 'role1' )";
    jdbcTemplate.execute(query);
    query = "insert into user_roles values ( 'user1', 'role2' )";
    jdbcTemplate.execute(query);
    log.debug("Assigned user1 roles role1 and role2");

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

From source file:com.mycompany.vendingmachinev2.dao.VendingDaoImplTest.java

@Before
public void setUp() {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("test-applicationContext.xml");
    dao = (VendingDao) ctx.getBean("VendingDao");
    // Grab a JdbcTemplate to use for cleaning up
    JdbcTemplate cleaner = (JdbcTemplate) ctx.getBean("jdbcTemplate");
    cleaner.execute("delete from items");
}

From source file:sparklr.common.AbstractIntegrationTests.java

private void clear(ApprovalStore approvalStore) throws Exception {
    if (approvalStore instanceof Advised) {
        Advised advised = (Advised) tokenStore;
        ApprovalStore target = (ApprovalStore) advised.getTargetSource().getTarget();
        clear(target);/*  w  ww  .j  a  va  2s .c  om*/
        return;
    }
    if (approvalStore instanceof InMemoryApprovalStore) {
        ((InMemoryApprovalStore) approvalStore).clear();
    }
    if (approvalStore instanceof JdbcApprovalStore) {
        JdbcTemplate template = new JdbcTemplate(dataSource);
        template.execute("delete from oauth_approvals");
    }
}

From source file:com.swcguild.addressbookfortheweb.AddressDaoTest.java

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

    JdbcTemplate cleaner = (JdbcTemplate) ctx.getBean("jdbcTemplate");
    cleaner.execute("DELETE FROM address");
}

From source file:sparklr.common.AbstractIntegrationTests.java

private void clear(TokenStore tokenStore) throws Exception {
    if (tokenStore instanceof Advised) {
        Advised advised = (Advised) tokenStore;
        TokenStore target = (TokenStore) advised.getTargetSource().getTarget();
        clear(target);//from  ww  w  .  j a  va2s .c o m
        return;
    }
    if (tokenStore instanceof InMemoryTokenStore) {
        ((InMemoryTokenStore) tokenStore).clear();
    }
    if (tokenStore instanceof JdbcTokenStore) {
        JdbcTemplate template = new JdbcTemplate(dataSource);
        template.execute("delete from oauth_access_token");
        template.execute("delete from oauth_refresh_token");
        template.execute("delete from oauth_client_token");
        template.execute("delete from oauth_code");
    }
}

From source file:com.mycompany.contactlistmvc.dao.ContactListDaoTest.java

@Before
public void setUp() {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("test-applicationContext.xml");
    dao = (ContactListDao) ctx.getBean("contactListDao");
    // Grab a JdbcTemplate to use for cleaning up
    JdbcTemplate cleaner = (JdbcTemplate) ctx.getBean("jdbcTemplate");
    cleaner.execute("delete from contacts");
}

From source file:com.swcguild.dvdlibraryarch.dao.DvdListDaoTest.java

@Before
public void setUp() {// Ask Spring for my DAO
    ApplicationContext ctx = new ClassPathXmlApplicationContext("test-applicationContext.xml");
    dao = (DvdListDao) ctx.getBean("dvdListDao");
    // Grab a JdbcTemplate to use for cleaning up
    JdbcTemplate cleaner = (JdbcTemplate) ctx.getBean("jdbcTemplate");
    cleaner.execute("delete from dvds");
}

From source file:com.swcguild.bluraymvc.test.BluRayTest_New.java

@Before
public void setUp() {
    // NEW CODE START- this cleans up the database table before each test
    // Ask Spring for my DAO
    //I think we use the bean id here
    ApplicationContext ctx = new ClassPathXmlApplicationContext("test-applicationContext.xml");
    dao = (BluRayDaoInMemImpl) ctx.getBean("movieDao");

    // Grab a JdbcTemplate to use for cleaning up
    JdbcTemplate cleaner = (JdbcTemplate) ctx.getBean("jdbcTemplate");
    cleaner.execute("delete from movies");
    // NEW CODE STOP
}

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

private void createDatabase() {
    JdbcTemplate jdbcTemplate = jdbcTemplate();
    jdbcTemplate.execute(String.format("DROP DATABASE IF EXISTS %s", databaseName));
    jdbcTemplate.execute(String.format("CREATE DATABASE %s", databaseName));
}