Search in sources :

Example 61 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project cas by apereo.

the class JdbcCloudConfigBootstrapConfiguration method locate.

@Override
public PropertySource<?> locate(final Environment environment) {
    final Properties props = new Properties();
    try {
        final JdbcCloudConnection connection = new JdbcCloudConnection(environment);
        final DataSource dataSource = JpaBeans.newDataSource(connection);
        final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        final List<Map<String, Object>> rows = jdbcTemplate.queryForList(connection.getSql());
        for (final Map row : rows) {
            props.put(row.get("name"), row.get("value"));
        }
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return new PropertiesPropertySource(getClass().getSimpleName(), props);
}
Also used : PropertiesPropertySource(org.springframework.core.env.PropertiesPropertySource) Properties(java.util.Properties) AbstractJpaProperties(org.apereo.cas.configuration.model.support.jpa.AbstractJpaProperties) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Map(java.util.Map) DataSource(javax.sql.DataSource)

Example 62 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project uPortal by Jasig.

the class SqlQueryPortletController method handleRenderRequest.

@Override
public ModelAndView handleRenderRequest(RenderRequest request, RenderResponse response) throws Exception {
    // find the configured SQL statement
    PortletPreferences preferences = request.getPreferences();
    String sqlQuery = preferences.getValue(SQL_QUERY_PARAM_NAME, null);
    String dsName = preferences.getValue(DATASOURCE_BEAN_NAME_PARAM_NAME, BasePortalJpaDao.PERSISTENCE_UNIT_NAME);
    String viewName = preferences.getValue(VIEW_PARAM_NAME, "jsp/SqlQuery/results");
    // Allow substituting attributes from the request and userInfo objects using the SPEL ${}
    // notation..
    String spelSqlQuery = evaluateSpelExpression(sqlQuery, request);
    List<Map<String, Object>> results = null;
    String cacheKey = createCacheKey(spelSqlQuery, dsName);
    Cache cache = getCache(request);
    if (cache != null) {
        Element cachedElement = cache.get(cacheKey);
        if (cachedElement != null) {
            log.debug("Cache hit. Returning item for query: {}, substituted query: {}, from cache {} for key {}", sqlQuery, spelSqlQuery, cache.getName(), cacheKey);
            results = (List<Map<String, Object>>) cachedElement.getObjectValue();
        }
    }
    if (results == null) {
        // generate a JDBC template for the requested data source
        DataSource ds = (DataSource) getApplicationContext().getBean(dsName);
        JdbcTemplate template = new JdbcTemplate(ds);
        // Execute the SQL query and build a results object.  This result will consist of one
        // rowname -> rowvalue map for each row in the result set
        results = template.query(spelSqlQuery, new ColumnMapRowMapper());
        log.debug("found {} results for query {}", results.size(), spelSqlQuery);
        if (cache != null) {
            log.debug("Adding SQL results to cache {}, query: {}, substituted query: {}", cache.getName(), sqlQuery, spelSqlQuery);
            Element cachedElement = new Element(cacheKey, results);
            cache.put(cachedElement);
        }
    }
    // build the model
    ModelAndView modelandview = new ModelAndView(viewName);
    modelandview.addObject("results", results);
    return modelandview;
}
Also used : Element(net.sf.ehcache.Element) ModelAndView(org.springframework.web.portlet.ModelAndView) PortletPreferences(javax.portlet.PortletPreferences) ColumnMapRowMapper(org.springframework.jdbc.core.ColumnMapRowMapper) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Map(java.util.Map) Cache(net.sf.ehcache.Cache) DataSource(javax.sql.DataSource)

Example 63 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project leopard by tanhaichao.

the class JdbcDaoSupportTest method setJdbcTemplate.

@Test
public void setJdbcTemplate() {
    JdbcTemplate jdbcTemplate = Mockito.mock(JdbcTemplate.class);
    jdbcDao.setJdbcTemplate(jdbcTemplate);
    Assert.assertNotNull(jdbcDao.getJdbcTemplate());
    jdbcDao.getExceptionTranslator();
}
Also used : JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.Test)

Example 64 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project camel by apache.

the class ElSqlConsumerDeleteTest method setUp.

@Before
public void setUp() throws Exception {
    db = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.DERBY).addScript("sql/createAndPopulateDatabase.sql").build();
    jdbcTemplate = new JdbcTemplate(db);
    super.setUp();
}
Also used : EmbeddedDatabaseBuilder(org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Before(org.junit.Before)

Example 65 with JdbcTemplate

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

the class MonitoringLocationsMigratorOfflineIT method testMigrateLargeConfigToDatabase.

/**
 * Test fixing the configuration file.
 *
 * @throws Exception the exception
 */
@Test
public void testMigrateLargeConfigToDatabase() throws Exception {
    MonitoringLocationsMigratorOffline migrator = new MonitoringLocationsMigratorOffline();
    migrator.preExecute();
    migrator.execute();
    migrator.postExecute();
    assertFalse(new File("target/home/etc/monitoring-locations.xml").exists());
    assertTrue(new File("target/home/etc/monitoring-locations.xml.zip").exists());
    assertTrue(new File("target/home/etc/monitoring-locations.xml.zip").isFile());
    final JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceFactory.getInstance());
    // this first one has been a little persnickity for unknown reasons so we log some more information
    String first = jdbcTemplate.queryForObject("SELECT monitoringarea FROM monitoringlocations ORDER BY id LIMIT 1", String.class);
    String last = jdbcTemplate.queryForObject("SELECT monitoringarea FROM monitoringlocations ORDER BY id DESC LIMIT 1", String.class);
    // 2864 from our file, plus 1 that comes in create.sql for localhost
    assertEquals("count of monitoringlocations; first: " + first + "; last: " + last, 2864 + 1, jdbcTemplate.queryForObject("SELECT COUNT(*) FROM monitoringlocations", Integer.class).intValue());
    assertEquals("count of monitoringlocations WHERE tag = 'divisbileBy3'", 954, jdbcTemplate.queryForObject("SELECT COUNT(*) FROM monitoringlocationstags WHERE tag = 'divisbileBy3'", Integer.class).intValue());
    assertEquals("count of monitoringlocations WHERE tag = 'divisibleBy5'", 572, jdbcTemplate.queryForObject("SELECT COUNT(*) FROM monitoringlocationstags WHERE tag = 'divisibleBy5'", Integer.class).intValue());
    assertEquals("count of monitoringlocations WHERE tag = 'divisibleBy7'", 409, jdbcTemplate.queryForObject("SELECT COUNT(*) FROM monitoringlocationstags WHERE tag = 'divisibleBy7'", Integer.class).intValue());
    assertEquals("count of monitoringlocations WHERE tag = 'odd'", 1432, jdbcTemplate.queryForObject("SELECT COUNT(*) FROM monitoringlocationstags WHERE tag = 'odd'", Integer.class).intValue());
    assertEquals("count of monitoringlocations WHERE tag = 'even'", 1432, jdbcTemplate.queryForObject("SELECT COUNT(*) FROM monitoringlocationstags WHERE tag = 'even'", Integer.class).intValue());
}
Also used : JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) File(java.io.File) Test(org.junit.Test)

Aggregations

JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)124 Test (org.junit.Test)46 DataSource (javax.sql.DataSource)37 Before (org.junit.Before)19 SQLException (java.sql.SQLException)11 EmbeddedDatabaseBuilder (org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder)11 BaseDbTest (com.alibaba.otter.node.etl.BaseDbTest)6 DbDialect (com.alibaba.otter.node.etl.common.db.dialect.DbDialect)6 DbMediaSource (com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource)6 Connection (java.sql.Connection)6 JdbcOperations (org.springframework.jdbc.core.JdbcOperations)6 TransactionStatus (org.springframework.transaction.TransactionStatus)6 Test (org.testng.annotations.Test)6 SqlTemplate (com.alibaba.otter.node.etl.common.db.dialect.SqlTemplate)5 PreparedStatement (java.sql.PreparedStatement)5 Table (org.apache.ddlutils.model.Table)5 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)5 DataAccessException (org.springframework.dao.DataAccessException)5 AbstractDriverBasedDataSource (org.springframework.jdbc.datasource.AbstractDriverBasedDataSource)5 TransactionCallback (org.springframework.transaction.support.TransactionCallback)5