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:nz.geek.caffe.spring.hdb.HANAExceptionMappingTest.java

/**
 *///from  ww w.  j a va  2  s.c  o  m
@Test
public void testUnableToConnect() {
    final DriverManagerDataSource ds = new DriverManagerDataSource("jdbc:sap://blahhost:30115", "blah", "blah");

    JdbcTemplate template = new JdbcTemplate();
    template.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator("HDB"));
    template.setDataSource(new LazyConnectionDataSourceProxy(ds));
    template.afterPropertiesSet();

    try {
        template.execute("SELECT 1 FROM DUMMY");

        Assert.fail("connect should have failed");
    } catch (final DataAccessResourceFailureException e) {
        // expected
    }
}

From source file:data.DefaultExchanger.java

private void importSequence(String dbName, JsonParser parser, JdbcTemplate jdbcTemplate) throws IOException {
    if (hasSequence()) {
        JsonToken fieldNameToken = parser.nextToken();
        if (fieldNameToken == JsonToken.FIELD_NAME) {
            String fieldName = parser.getCurrentName();
            if (fieldName.equalsIgnoreCase(sequenceName())) {
                JsonToken current = parser.nextToken();
                if (current == JsonToken.VALUE_NUMBER_INT) {
                    long sequenceValue = parser.getNumberValue().longValue();
                    if (dbName.equals("MySQL")) {
                        jdbcTemplate
                                .execute("ALTER TABLE " + getTable() + " AUTO_INCREMENT = " + sequenceValue);
                    } else if (dbName.equals("H2")) {
                        jdbcTemplate/*from  w  ww .  j a  v  a  2s  .  com*/
                                .execute("ALTER SEQUENCE " + sequenceName() + " RESTART WITH " + sequenceValue);
                    }
                }
            }
        }
        play.Logger.info("imported sequence {{}}", sequenceName());
    }
}

From source file:com.alibaba.otter.node.etl.load.loader.db.interceptor.operation.AbstractOperationInterceptor.java

private void init(final JdbcTemplate jdbcTemplate, final String markTableName, final String markTableColumn) {
    int count = jdbcTemplate
            .queryForInt(MessageFormat.format(checkDataSql, markTableName, GLOBAL_THREAD_COUNT - 1));
    if (count != GLOBAL_THREAD_COUNT) {
        if (logger.isInfoEnabled()) {
            logger.info("Interceptor: init " + markTableName + "'s data.");
        }//  w ww  .  ja va2  s  .  c om
        TransactionTemplate transactionTemplate = new TransactionTemplate();
        transactionTemplate
                .setTransactionManager(new DataSourceTransactionManager(jdbcTemplate.getDataSource()));
        transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);// ??????
        transactionTemplate.execute(new TransactionCallback() {

            public Object doInTransaction(TransactionStatus status) {
                jdbcTemplate.execute(MessageFormat.format(deleteDataSql, markTableName));
                String batchSql = MessageFormat.format(updateSql,
                        new Object[] { markTableName, markTableColumn });
                jdbcTemplate.batchUpdate(batchSql, new BatchPreparedStatementSetter() {

                    public void setValues(PreparedStatement ps, int idx) throws SQLException {
                        ps.setInt(1, idx);
                        ps.setInt(2, 0);
                        // ps.setNull(3, Types.VARCHAR);
                    }

                    public int getBatchSize() {
                        return GLOBAL_THREAD_COUNT;
                    }
                });
                return null;
            }
        });

        if (logger.isInfoEnabled()) {
            logger.info("Interceptor: Init EROSA Client Data: " + updateSql);
        }
    }

}

From source file:org.jspresso.hrsample.backend.JspressoUnitOfWorkTest.java

/**
 * Tests merge modes./*from w ww.  j  a v a 2s.  c o m*/
 */
@Test
public void testMergeModes() {
    final HibernateBackendController hbc = (HibernateBackendController) getBackendController();

    EnhancedDetachedCriteria crit = EnhancedDetachedCriteria.forClass(City.class);
    final City c1 = hbc.findFirstByCriteria(crit, EMergeMode.MERGE_CLEAN_EAGER, City.class);
    String name = c1.getName();

    JdbcTemplate jdbcTemplate = getApplicationContext().getBean("jdbcTemplate", JdbcTemplate.class);
    jdbcTemplate.execute(new ConnectionCallback<Object>() {

        @Override
        public Object doInConnection(Connection con) throws SQLException {
            PreparedStatement ps = con.prepareStatement("UPDATE CITY SET NAME = ? WHERE ID = ?");
            ps.setString(1, "test");
            ps.setObject(2, c1.getId());
            assertEquals(1, ps.executeUpdate());
            return null;
        }
    });
    final City c2 = hbc.findById(c1.getId(), EMergeMode.MERGE_CLEAN_LAZY, City.class);

    assertSame(c1, c2);
    assertEquals(name, c2.getName());

    final City c3 = hbc.findById(c1.getId(), EMergeMode.MERGE_CLEAN_EAGER, City.class);
    assertSame(c1, c3);
    assertEquals("test", c3.getNameRaw());

    jdbcTemplate.execute(new ConnectionCallback<Object>() {

        @Override
        public Object doInConnection(Connection con) throws SQLException {
            PreparedStatement ps = con
                    .prepareStatement("UPDATE CITY SET NAME = ?, VERSION = VERSION+1 WHERE ID = ?");
            ps.setString(1, "test2");
            ps.setObject(2, c1.getId());
            assertEquals(1, ps.executeUpdate());
            return null;
        }
    });

    final City c4 = hbc.findById(c1.getId(), EMergeMode.MERGE_KEEP, City.class);
    assertSame(c1, c4);
    assertEquals("test", c4.getNameRaw());

    final City c5 = hbc.findById(c1.getId(), EMergeMode.MERGE_CLEAN_LAZY, City.class);
    assertSame(c1, c5);
    assertEquals("test2", c5.getNameRaw());
}

From source file:au.aurin.org.svc.GeodataFinder.java

public String updateorgs(final String user_id, final String[] org_id) {

    String query = "";
    try {// w ww .ja v a 2 s.c  o m
        final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        query = "delete from user_orgs  " + " where " + " user_id = " + user_id;
        LOGGER.info("delete org query is {} ", query);
        jdbcTemplate.execute(query);
        for (final String orgid : org_id) {
            query = "Insert into user_orgs (user_id,org_id) values(" + user_id + "," + orgid + ")";

            LOGGER.info("insert org query is {} ", query);

            jdbcTemplate.execute(query);
        }
        return "Success";

    } catch (final Exception e) {

        LOGGER.info("error in  updateorgs is : {}", e.toString());

    }
    return null;

}

From source file:au.aurin.org.svc.GeodataFinder.java

public String updateapps(final String user_id, final String[] app_id) {

    String query = "";
    try {//from   ww w  . ja  v a  2 s.  co  m
        final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        query = "delete from user_apps  " + " where " + " user_id = " + user_id;
        LOGGER.info("delete app query is {} ", query);
        jdbcTemplate.execute(query);
        for (final String appid : app_id) {
            query = "Insert into user_apps (user_id,app_id) values(" + user_id + "," + appid + ")";

            LOGGER.info("insert app query is {} ", query);

            jdbcTemplate.execute(query);
        }
        return "Success";

    } catch (final Exception e) {

        LOGGER.info("error in  updateapps is : {}", e.toString());

    }
    return null;

}

From source file:au.aurin.org.svc.GeodataFinder.java

public String deleteuser(final String user_id) {

    String query = "";
    try {/*from w  w  w .  j  a v a 2s .c  om*/
        final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        query = "delete from user_orgs where user_id= " + user_id;
        LOGGER.info("updateuser query is {} ", query);
        jdbcTemplate.execute(query);

        query = "delete from user_apps where user_id= " + user_id;
        LOGGER.info("updateuser query is {} ", query);
        jdbcTemplate.execute(query);

        query = "delete from user_roles where user_id= " + user_id;
        LOGGER.info("updateuser query is {} ", query);
        jdbcTemplate.execute(query);

        query = "delete from user_acclvls where user_id= " + user_id;
        LOGGER.info("updateuser query is {} ", query);
        jdbcTemplate.execute(query);

        query = "delete from agreement where user_id= " + user_id;
        LOGGER.info("updateuser query is {} ", query);
        jdbcTemplate.execute(query);

        query = "delete from users where user_id= " + user_id;
        LOGGER.info("updateuser query is {} ", query);
        jdbcTemplate.execute(query);

        return "Success";

    } catch (final Exception e) {

        LOGGER.info("error in  deleteuser is : {}", e.toString());

    }
    return null;

}

From source file:au.aurin.org.svc.GeodataFinder.java

public String updateRoles(final String user_id, final String[] role_id) {

    String query = "";
    try {//  w ww  . j a va 2s.  c o m
        final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        query = "delete from user_roles  " + " where " + " user_id = " + user_id;
        LOGGER.info("delete role query is {} ", query);
        jdbcTemplate.execute(query);
        for (final String roleid : role_id) {
            query = "Insert into user_roles (user_id,role_id) values(" + user_id + "," + roleid + ")";

            LOGGER.info("insert role query is {} ", query);

            jdbcTemplate.execute(query);
        }
        return "Success";

    } catch (final Exception e) {

        LOGGER.info("error in  updateRoles is : {}", e.toString());

    }
    return null;

}

From source file:au.aurin.org.svc.GeodataFinder.java

public String updateaccs(final String user_id, final String[] acclvl_id) {

    String query = "";
    try {/*  w  w  w .ja  va  2  s .  c o  m*/
        final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        query = "delete from user_acclvls  " + " where " + " user_id = " + user_id;
        LOGGER.info("delete acc query is {} ", query);
        jdbcTemplate.execute(query);
        for (final String accid : acclvl_id) {
            query = "Insert into user_acclvls (user_id,acclvl_id) values(" + user_id + "," + accid + ")";

            LOGGER.info("insert acc query is {} ", query);

            jdbcTemplate.execute(query);
        }
        return "Success";

    } catch (final Exception e) {

        LOGGER.info("error in  updateaccs is : {}", e.toString());

    }
    return null;

}