Search in sources :

Example 1 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project perun by CESNET.

the class TaskResultDaoTest method setUp.

@Before
public void setUp() throws InternalErrorException {
    if (perunSession == null || jdbcTemplate == null) {
        perunSession = perun.getPerunSession(new PerunPrincipal("perunTests", ExtSourcesManager.EXTSOURCE_NAME_INTERNAL, ExtSourcesManager.EXTSOURCE_INTERNAL), new PerunClient());
        jdbcTemplate = new JdbcTemplate(dataSource);
    }
}
Also used : JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Before(org.junit.Before)

Example 2 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project opennms by OpenNMS.

the class HttpNotificationStrategy method doSql.

private void doSql(String contents) {
    if (getSql() == null) {
        LOG.info("send: optional sql argument is null.");
        return;
    }
    if (contents == null) {
        LOG.info("doSql: HTTP reply is null");
        return;
    }
    LOG.debug("send: compiling expression: {}", getSwitchValue("result-match"));
    Pattern p = Pattern.compile(getSwitchValue("result-match"));
    Matcher m = p.matcher(contents);
    if (m.matches()) {
        LOG.debug("send: compiled expression ready to run sql: {}", getSql());
        MatchTable matches = new MatchTable(m);
        String sqlString = PropertiesUtils.substitute(getSql(), matches);
        LOG.debug("send: running sql: {}", sqlString);
        JdbcTemplate template = new JdbcTemplate(DataSourceFactory.getInstance());
        template.execute(sqlString);
    } else {
        LOG.info("send: result didn't match, not running sql");
    }
}
Also used : Pattern(java.util.regex.Pattern) MatchTable(org.opennms.core.utils.MatchTable) Matcher(java.util.regex.Matcher) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Example 3 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project opennms by OpenNMS.

the class TemporaryDatabasePostgreSQL method setupDatabase.

public void setupDatabase() throws TemporaryDatabaseException {
    try {
        setDataSource(new SimpleDataSource(m_driver, m_url + getTestDatabase(), m_adminUser, m_adminPassword));
        setAdminDataSource(new SimpleDataSource(m_driver, m_url + "template1", m_adminUser, m_adminPassword));
        m_xaDataSource = new PGXADataSource();
        m_xaDataSource.setServerName("localhost");
        m_xaDataSource.setDatabaseName(getTestDatabase());
        m_xaDataSource.setUser(m_adminUser);
        m_xaDataSource.setPassword(m_adminPassword);
        m_adminXaDataSource = new PGXADataSource();
        m_adminXaDataSource.setServerName("localhost");
        m_adminXaDataSource.setDatabaseName("template1");
        m_adminXaDataSource.setUser(m_adminUser);
        m_adminXaDataSource.setPassword(m_adminPassword);
    } catch (final ClassNotFoundException e) {
        throw new TemporaryDatabaseException("Failed to initialize driver " + m_driver, e);
    }
    if (!m_useExisting) {
        // to the template1 database
        synchronized (TEMPLATE1_MUTEX) {
            createTestDatabase();
        }
    }
    setJdbcTemplate(new JdbcTemplate(this));
    // Test connecting to test database and ensuring we can do a basic query
    try {
        getJdbcTemplate().queryForObject("SELECT now()", Date.class);
    } catch (DataAccessException e) {
        throw new TemporaryDatabaseException("Error occurred while testing database is connectable: " + e.getMessage(), e);
    }
    if (!m_useExisting) {
        setupBlame(getJdbcTemplate(), getBlame());
    }
}
Also used : SimpleDataSource(org.opennms.core.db.install.SimpleDataSource) PGXADataSource(org.postgresql.xa.PGXADataSource) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataAccessException(org.springframework.dao.DataAccessException)

Example 4 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project libresonic by Libresonic.

the class AbstractDao method namedQueryWithLimit.

protected <T> List<T> namedQueryWithLimit(String sql, RowMapper<T> rowMapper, Map<String, Object> args, int limit) {
    long t = System.nanoTime();
    JdbcTemplate jdbcTemplate = new JdbcTemplate(daoHelper.getDataSource());
    jdbcTemplate.setMaxRows(limit);
    NamedParameterJdbcTemplate namedTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
    List<T> result = namedTemplate.query(sql, args, rowMapper);
    log(sql, t);
    return result;
}
Also used : NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Example 5 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project spring-boot by spring-projects.

the class DataSourceHealthIndicator method setDataSource.

/**
	 * Set the {@link DataSource} to use.
	 * @param dataSource the data source
	 */
public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
    this.jdbcTemplate = new JdbcTemplate(dataSource);
}
Also used : JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Aggregations

JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)444 Test (org.junit.jupiter.api.Test)60 DataSource (javax.sql.DataSource)48 Test (org.junit.Test)47 NamedParameterJdbcTemplate (org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate)41 SQLException (java.sql.SQLException)40 PreparedStatement (java.sql.PreparedStatement)27 Before (org.junit.Before)24 Map (java.util.Map)20 BatchPreparedStatementSetter (org.springframework.jdbc.core.BatchPreparedStatementSetter)19 UserTO (org.apache.syncope.common.lib.to.UserTO)15 EmbeddedDatabaseBuilder (org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder)15 JdbcOperations (org.springframework.jdbc.core.JdbcOperations)13 ArrayList (java.util.ArrayList)11 DataAccessException (org.springframework.dao.DataAccessException)11 Bean (org.springframework.context.annotation.Bean)10 ResultSet (java.sql.ResultSet)9 Transactional (org.springframework.transaction.annotation.Transactional)9 Connection (java.sql.Connection)8 HashMap (java.util.HashMap)8