本文整理汇总了Java中org.springframework.jdbc.datasource.SingleConnectionDataSource类的典型用法代码示例。如果您正苦于以下问题:Java SingleConnectionDataSource类的具体用法?Java SingleConnectionDataSource怎么用?Java SingleConnectionDataSource使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SingleConnectionDataSource类属于org.springframework.jdbc.datasource包,在下文中一共展示了SingleConnectionDataSource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: validateSQL
import org.springframework.jdbc.datasource.SingleConnectionDataSource; //导入依赖的package包/类
private boolean validateSQL(JobVO jobVO) {
SingleConnectionDataSource ds = new SingleConnectionDataSource();
try {
ds.setDriverClassName(jobVO.getDriverClassName());
ds.setUrl(jobVO.getUrl());
ds.setUsername(jobVO.getUsername());
ds.setPassword(jobVO.getPassword());
JdbcTemplate template = new JdbcTemplate(ds);
template.execute(jobVO.getSql());
return true;
} catch (Throwable t) {
return false;
} finally {
ds.destroy();
}
}
示例2: testLeaveConnectionOpenOnRequest
import org.springframework.jdbc.datasource.SingleConnectionDataSource; //导入依赖的package包/类
@Test
public void testLeaveConnectionOpenOnRequest() throws Exception {
String sql = "SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3";
given(this.resultSet.next()).willReturn(false);
given(this.connection.isClosed()).willReturn(false);
given(this.connection.createStatement()).willReturn(this.preparedStatement);
// if close is called entire test will fail
willThrow(new RuntimeException()).given(this.connection).close();
SingleConnectionDataSource scf = new SingleConnectionDataSource(this.dataSource.getConnection(), false);
this.template = new JdbcTemplate(scf, false);
RowCountCallbackHandler rcch = new RowCountCallbackHandler();
this.template.query(sql, rcch);
verify(this.resultSet).close();
verify(this.preparedStatement).close();
}
示例3: executeSql
import org.springframework.jdbc.datasource.SingleConnectionDataSource; //导入依赖的package包/类
public String executeSql(String sql,String code) throws Exception {
SingleConnectionDataSource ds = getSingleConnectionDataSource(code);
this.setRtJdbcTemplate(ds);
String result;
try {
result = (String)this.rtJdbcTemplate.queryForObject(sql,null,java.lang.String.class);
return result;
} catch (Exception e) {
log.error(this.getClass() + "\n---方法:" + new Exception().getStackTrace()[0].getMethodName() + "\n---e.getMessage():\n" + e.toString() + "\n" + e);
throw new Exception(e.getMessage());
}
finally{
ds.getConnection().close();
}
}
示例4: getSingleConnectionDataSource
import org.springframework.jdbc.datasource.SingleConnectionDataSource; //导入依赖的package包/类
public SingleConnectionDataSource getSingleConnectionDataSource(String code) throws Exception {
//String sql ="select t2.DRIVER_CLASS as driver,t2.url ,t2.user_name ,t2.password from SYS_DATA_SOURCE t1,sys_db_links t2 where t1.link_id=t2.link_id and t1.code='"+code+"'";
String sql ="select t2.DRIVER_CLASS as driver,t2.url ,t2.user_name ,t2.password from SYS_DATA_SOURCE t1,sys_db_links t2 where t1.link_id=t2.link_id and t1.sh_code='"+code+"'";
List<Map<String,Object>> list = (List<Map<String,Object>>)this.jdbcTemplate.queryForList(sql);
Map<String,Object> map = new HashMap<String,Object>();
if(list.size()>0){
map = list.get(0);
}
SingleConnectionDataSource ds = new SingleConnectionDataSource();
ds.setDriverClassName(ParseUtil.getString(map.get("driver")));
ds.setUrl(ParseUtil.getString(map.get("url")));
ds.setUsername(ParseUtil.getString(map.get("user_name")));
ds.setPassword(ParseUtil.getString(map.get("password")));
return ds;
}
示例5: createVerifier
import org.springframework.jdbc.datasource.SingleConnectionDataSource; //导入依赖的package包/类
@Override
public NonceVerifier createVerifier(int maxAge) {
DataSource dataSource = new SingleConnectionDataSource(
"org.hsqldb.jdbcDriver",
"jdbc:hsqldb:mem:saasstore_security_client", "sa", "", true);
SimpleJdbcTemplate jdbcTemplate = new SimpleJdbcTemplate(dataSource);
jdbcTemplate.getJdbcOperations().execute(
"DROP TABLE IF EXISTS openid_nonce;");
jdbcTemplate
.getJdbcOperations()
.execute(
"CREATE TABLE openid_nonce ( "
+ "opurl varchar(255) NOT NULL, nonce varchar(25) NOT NULL, "
+ "date datetime DEFAULT NULL, PRIMARY KEY (opurl,nonce))");
JdbcNonceVerifier jdbcNonceVerifier = new JdbcNonceVerifier(maxAge,
"openid_nonce");
jdbcNonceVerifier.setDataSource(dataSource);
return jdbcNonceVerifier;
}
示例6: addAndGet
import org.springframework.jdbc.datasource.SingleConnectionDataSource; //导入依赖的package包/类
@Test
@DirtiesContext
public void addAndGet() throws SQLException {
DataSource dataSource = new SingleConnectionDataSource(
"jdbc:mysql://54.64.47.206/mykumitestdb","dbuser", "dbuser1*",true);
jdbcContext.setDataSource(dataSource);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
userDao.setJdbcTemplate(jdbcTemplate);
userDao.deleteAll();
assertThat(userDao.getCount(), is(0));
User user = new User("u1", "user01", "111111", Level.BASIC,1,0);
userDao.add(user);
assertThat(userDao.getCount(), is(1));
User user2 = userDao.get(user.getId());
assertThat(user.getName(), is(user2.getName()));
assertThat(user.getPassword(), is(user2.getPassword()));
}
示例7: applyScripts
import org.springframework.jdbc.datasource.SingleConnectionDataSource; //导入依赖的package包/类
private void applyScripts(String url) throws SQLException, IOException {
log.info("Apply Scripts ...");
Connection connection = getConnection(url);
DataSource ds = new SingleConnectionDataSource(connection, false);
FileSystemScanner scanner = new FileSystemScanner();
for (String location : builder.locations) {
File directory = new File(location);
if (directory.exists() && directory.isDirectory()) {
Resource[] resources = scanner.scanForResources(location, "", ".sql");
ResourceDatabasePopulator populator = new ResourceDatabasePopulator(resources);
populator.setSeparator(builder.separator);
populator.execute(ds);
} else {
// log not existing directory
}
}
log.info("Scripts applied!");
}
示例8: testSql2O
import org.springframework.jdbc.datasource.SingleConnectionDataSource; //导入依赖的package包/类
@Test
public void testSql2O() throws SQLException, ParseException {
Connection connection = DbHelper.objectDb();
try {
SingleConnectionDataSource scds = new SingleConnectionDataSource(connection, true);
Sql2o sql2o = new Sql2o(scds);
Query query = sql2o.open().createQuery(DbHelper.TEST_DB_OBJECT_QUERY);
query.setAutoDeriveColumnNames(true);
query.setResultSetHandlerFactoryBuilder(new SfmResultSetHandlerFactoryBuilder());
List<DbObject> dbObjects = query.executeAndFetch(DbObject.class);
assertEquals(1, dbObjects.size());
DbHelper.assertDbObjectMapping(dbObjects.get(0));
} finally {
connection.close();
}
}
示例9: setUp
import org.springframework.jdbc.datasource.SingleConnectionDataSource; //导入依赖的package包/类
@Before
public void setUp() throws SQLException {
connection = mock(Connection.class);
statement = mock(Statement.class);
preparedStatement = mock(PreparedStatement.class);
resultSet = mock(ResultSet.class);
given(connection.createStatement()).willReturn(statement);
given(connection.prepareStatement(anyString())).willReturn(preparedStatement);
given(statement.executeQuery(anyString())).willReturn(resultSet);
given(preparedStatement.executeQuery()).willReturn(resultSet);
given(resultSet.next()).willReturn(true, true, false);
given(resultSet.getString(1)).willReturn("tb1", "tb2");
given(resultSet.getInt(2)).willReturn(1, 2);
template = new JdbcTemplate();
template.setDataSource(new SingleConnectionDataSource(connection, false));
template.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
template.afterPropertiesSet();
}
示例10: updateCurrentChunk
import org.springframework.jdbc.datasource.SingleConnectionDataSource; //导入依赖的package包/类
private void updateCurrentChunk() {
if (logger.isDebugEnabled()) {
logger.debug("Updating HILO chunk...");
}
long t = System.currentTimeMillis();
try (Connection conn = dataSource.getConnection()) {
conn.setAutoCommit(false);
JdbcTemplate jdbcTemplate = new JdbcTemplate(new SingleConnectionDataSource(conn, true));
jdbcTemplate.update(SQL_LOCK);
currentChunk = jdbcTemplate.queryForObject(SQL_SELECT, Integer.class);
if (logger.isDebugEnabled())
logger.debug("Current chunk: " + currentChunk);
jdbcTemplate.execute(SQL_UPDATE);
jdbcTemplate.execute("commit");
if (logger.isDebugEnabled()) {
logger.debug("Updating HILO chunk done in " + (System.currentTimeMillis() - t) + " ms");
}
currentId = 0;
} catch (SQLException e) {
logger.error("Unable to update current chunk", e);
throw new IllegalStateException("Unable to update current chunk");
}
}
示例11: findLatestUtilizationPostgreSQL
import org.springframework.jdbc.datasource.SingleConnectionDataSource; //导入依赖的package包/类
private Set<Utilization> findLatestUtilizationPostgreSQL(Long[] facilityIds, SingleConnectionDataSource dataSource) {
NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
return new LinkedHashSet<>(jdbcTemplate.query("" +
"SELECT latest.* " +
"FROM (" +
" SELECT DISTINCT facility_id, capacity_type, usage " +
" FROM pricing " +
(facilityIds.length > 0 ? " WHERE facility_id IN (:facility_ids) " : "") +
") p " +
"JOIN LATERAL ( " +
" SELECT * " +
" FROM facility_utilization " +
" WHERE facility_id = p.facility_id AND capacity_type = p.capacity_type AND usage = p.usage " +
" ORDER BY ts DESC " +
" LIMIT 1 " +
") latest ON TRUE " +
"ORDER BY facility_id, capacity_type, usage",
new MapSqlParameterSource("facility_ids", Arrays.asList(facilityIds)),
utilizationRowMapper));
}
示例12: Mock
import org.springframework.jdbc.datasource.SingleConnectionDataSource; //导入依赖的package包/类
public Mock(MockType type)
throws Exception {
connection = mock(Connection.class);
statement = mock(Statement.class);
resultSet = mock(ResultSet.class);
resultSetMetaData = mock(ResultSetMetaData.class);
given(connection.createStatement()).willReturn(statement);
given(statement.executeQuery(anyString())).willReturn(resultSet);
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
given(resultSet.next()).willReturn(true, false);
given(resultSet.getString(1)).willReturn("Bubba");
given(resultSet.getLong(2)).willReturn(22L);
given(resultSet.getTimestamp(3)).willReturn(new Timestamp(1221222L));
given(resultSet.getBigDecimal(4)).willReturn(new BigDecimal("1234.56"));
given(resultSet.wasNull()).willReturn(type == MockType.TWO ? true : false);
given(resultSetMetaData.getColumnCount()).willReturn(4);
given(resultSetMetaData.getColumnLabel(1)).willReturn(
type == MockType.THREE ? "Last Name" : "name");
given(resultSetMetaData.getColumnLabel(2)).willReturn("age");
given(resultSetMetaData.getColumnLabel(3)).willReturn("birth_date");
given(resultSetMetaData.getColumnLabel(4)).willReturn("balance");
jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(new SingleConnectionDataSource(connection, false));
jdbcTemplate.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
jdbcTemplate.afterPropertiesSet();
}
示例13: setUp
import org.springframework.jdbc.datasource.SingleConnectionDataSource; //导入依赖的package包/类
@Before
public void setUp() throws SQLException {
given(connection.createStatement()).willReturn(statement);
given(connection.prepareStatement(anyString())).willReturn(preparedStatement);
given(statement.executeQuery(anyString())).willReturn(resultSet);
given(preparedStatement.executeQuery()).willReturn(resultSet);
given(resultSet.next()).willReturn(true, true, false);
given(resultSet.getString(1)).willReturn("tb1", "tb2");
given(resultSet.getInt(2)).willReturn(1, 2);
template.setDataSource(new SingleConnectionDataSource(connection, false));
template.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
template.afterPropertiesSet();
}
示例14: init
import org.springframework.jdbc.datasource.SingleConnectionDataSource; //导入依赖的package包/类
@Before
public void init() {
EmbeddedDatabaseConnection db = EmbeddedDatabaseConnection.HSQL;
this.dataSource = new SingleConnectionDataSource(db.getUrl() + ";shutdown=true",
"sa", "", false);
this.dataSource.setDriverClassName(db.getDriverClassName());
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:8,代码来源:DataSourceHealthIndicatorTests.java
示例15: executeSqlForTableDs
import org.springframework.jdbc.datasource.SingleConnectionDataSource; //导入依赖的package包/类
public String executeSqlForTableDs(Map<String,String> sqlmap) throws Exception{
String resultSql = sqlmap.get("resultSql");
String trStyleStr = sqlmap.get("trStyleStr");
List<String> array = ParseUtil.parseTrStyleStr(trStyleStr);
StringBuilder sb = new StringBuilder();
SingleConnectionDataSource ds = getSingleConnectionDataSource(sqlmap.get("code"));
this.setRtJdbcTemplate(ds);
try {
List<Map<String,Object>> list = (List<Map<String,Object>>)this.rtJdbcTemplate.queryForList(resultSql);
if(list.size()>0){
for (Map<String, Object> map : list) {
String newStr =trStyleStr;
for (String str :array) {
String v = map.get(str.trim())==null?"":map.get(str.trim()).toString();
newStr = newStr.replaceAll("\\[&SV,"+str+",&]",v);
}
sb.append(newStr);
}
}
return sb.toString();
} catch (Exception e) {
log.error(this.getClass() + "\n---方法:" + new Exception().getStackTrace()[0].getMethodName() + "\n---e.getMessage():\n" + e.toString() + "\n" + e);
throw new Exception(e.getMessage());
}
finally{
ds.getConnection().close();
}
}