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);
}
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;
}
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();
}
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();
}
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());
}
Aggregations