本文整理汇总了Java中org.springframework.jdbc.core.JdbcTemplate.execute方法的典型用法代码示例。如果您正苦于以下问题:Java JdbcTemplate.execute方法的具体用法?Java JdbcTemplate.execute怎么用?Java JdbcTemplate.execute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.jdbc.core.JdbcTemplate
的用法示例。
在下文中一共展示了JdbcTemplate.execute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: AbstractDbDialect
import org.springframework.jdbc.core.JdbcTemplate; //导入方法依赖的package包/类
public AbstractDbDialect(final JdbcTemplate jdbcTemplate, LobHandler lobHandler){
this.jdbcTemplate = jdbcTemplate;
this.lobHandler = lobHandler;
// 初始化transction
this.transactionTemplate = new TransactionTemplate();
transactionTemplate.setTransactionManager(new DataSourceTransactionManager(jdbcTemplate.getDataSource()));
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
// 初始化一些数据
jdbcTemplate.execute(new ConnectionCallback() {
public Object doInConnection(Connection c) throws SQLException, DataAccessException {
DatabaseMetaData meta = c.getMetaData();
databaseName = meta.getDatabaseProductName();
databaseMajorVersion = meta.getDatabaseMajorVersion();
databaseMinorVersion = meta.getDatabaseMinorVersion();
return null;
}
});
initTables(jdbcTemplate);
}
示例2: test
import org.springframework.jdbc.core.JdbcTemplate; //导入方法依赖的package包/类
@Test
public void test() throws SQLException {
BasicDataSource dataSource = getDataSource("");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("CREATE TABLE employee (id INTEGER)");
dataSource.close();
List<MockSpan> finishedSpans = mockTracer.finishedSpans();
assertEquals(1, finishedSpans.size());
checkTags(finishedSpans, "myservice", "jdbc:hsqldb:mem:spring");
checkSameTrace(finishedSpans);
assertNull(mockTracer.scopeManager().active());
}
示例3: testWithSpanOnlyWithParent
import org.springframework.jdbc.core.JdbcTemplate; //导入方法依赖的package包/类
@Test
public void testWithSpanOnlyWithParent() throws SQLException {
try (Scope activeSpan = mockTracer.buildSpan("parent").startActive(true)) {
BasicDataSource dataSource = getDataSource(";traceWithActiveSpanOnly=true");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("CREATE TABLE with_parent_skip (id INTEGER)");
dataSource.close();
}
List<MockSpan> finishedSpans = mockTracer.finishedSpans();
assertEquals(2, finishedSpans.size());
checkSameTrace(finishedSpans);
assertNull(mockTracer.scopeManager().active());
}
示例4: findSqlServerPKIndex
import org.springframework.jdbc.core.JdbcTemplate; //导入方法依赖的package包/类
/**
* 查找sqlserver的主键索引
*
* @param dbInofId
* @param tableName
* @return 返回主键索引的值
* @throws Exception
*/
public String findSqlServerPKIndex(String dbInofId, final String tableName) throws Exception {
DataSource ds = getDataSourceByDbInfoId(dbInofId);
JdbcTemplate jdbcTemplate = SpringJdbcUtils.getJdbcTemplate(ds);
String s = jdbcTemplate.execute(new ConnectionCallback<String>() {
public String doInConnection(Connection con) throws SQLException, DataAccessException {
String pkName = null;
if (con.getMetaData().getURL().toLowerCase().contains("sqlserver")) {
CallableStatement call = con.prepareCall("{call sp_pkeys(?)}");
call.setString(1, tableName);
ResultSet rs = call.executeQuery();
while (rs.next()) {
pkName = rs.getString("PK_NAME");
}
}
return pkName;
}
});
return s;
}
示例5: testSpringXaTx
import org.springframework.jdbc.core.JdbcTemplate; //导入方法依赖的package包/类
@Test
public void testSpringXaTx() throws Exception {
DataSource ds = wrap(createHsqlDataSource());
JdbcTemplate jdbc = new JdbcTemplate(ds);
TransactionTemplate tx = new TransactionTemplate(ptm);
jdbc.execute(DROP_USER);
jdbc.execute(CREATE_TABLE_USER);
tx.execute(ts -> jdbc.update(INSERT_INTO_USER, 1, "user1"));
User user = tx.execute(ts -> jdbc.queryForObject(SELECT_FROM_USER_BY_ID, new BeanPropertyRowMapper<>(User.class), 1));
assertEquals(new User(1, "user1"), user);
tx.execute(ts -> jdbc.update(DELETE_FROM_USER_BY_ID, 1));
tx.execute(ts -> {
int nb = jdbc.update(INSERT_INTO_USER, 1, "user1");
ts.setRollbackOnly();
return nb;
});
try {
user = tx.execute(ts -> jdbc.queryForObject(SELECT_FROM_USER_BY_ID, new BeanPropertyRowMapper<>(User.class), 1));
fail("Expected a EmptyResultDataAccessException");
} catch (EmptyResultDataAccessException e) {
// expected
}
}
示例6: testSpringXaTx
import org.springframework.jdbc.core.JdbcTemplate; //导入方法依赖的package包/类
@Test
public void testSpringXaTx() throws Exception {
DataSource ds = wrap(createH2DataSource());
JdbcTemplate jdbc = new JdbcTemplate(ds);
TransactionTemplate tx = new TransactionTemplate(ptm);
jdbc.execute(DROP_USER);
jdbc.execute(CREATE_TABLE_USER);
tx.execute(ts -> jdbc.update(INSERT_INTO_USER, 1, "user1"));
User user = tx.execute(ts -> jdbc.queryForObject(SELECT_FROM_USER_BY_ID, new BeanPropertyRowMapper<>(User.class), 1));
assertEquals(new User(1, "user1"), user);
tx.execute(ts -> jdbc.update(DELETE_FROM_USER_BY_ID, 1));
tx.execute(ts -> {
int nb = jdbc.update(INSERT_INTO_USER, 1, "user1");
ts.setRollbackOnly();
return nb;
});
try {
user = tx.execute(ts -> jdbc.queryForObject(SELECT_FROM_USER_BY_ID, new BeanPropertyRowMapper<>(User.class), 1));
fail("Expected a EmptyResultDataAccessException");
} catch (EmptyResultDataAccessException e) {
// expected
}
}
示例7: testSpringLocalTx
import org.springframework.jdbc.core.JdbcTemplate; //导入方法依赖的package包/类
@Test
public void testSpringLocalTx() throws Exception {
DataSource ds = wrap(createH2DataSource());
JdbcTemplate jdbc = new JdbcTemplate(ds);
TransactionTemplate tx = new TransactionTemplate(new DataSourceTransactionManager(ds));
jdbc.execute(DROP_USER);
jdbc.execute(CREATE_TABLE_USER);
tx.execute(ts -> jdbc.update(INSERT_INTO_USER, 1, "user1"));
User user = tx.execute(ts -> jdbc.queryForObject(SELECT_FROM_USER_BY_ID, new BeanPropertyRowMapper<>(User.class), 1));
assertEquals(new User(1, "user1"), user);
tx.execute(ts -> jdbc.update(DELETE_FROM_USER_BY_ID, 1));
tx.execute(ts -> {
int nb = jdbc.update(INSERT_INTO_USER, 1, "user1");
ts.setRollbackOnly();
return nb;
});
try {
user = tx.execute(ts -> jdbc.queryForObject(SELECT_FROM_USER_BY_ID, new BeanPropertyRowMapper<>(User.class), 1));
fail("Expected a EmptyResultDataAccessException");
} catch (EmptyResultDataAccessException e) {
// expected
}
}
示例8: spring
import org.springframework.jdbc.core.JdbcTemplate; //导入方法依赖的package包/类
@Test
public void spring() throws SQLException {
BasicDataSource dataSource = getDataSource(false);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("CREATE TABLE employee (id INTEGER)");
dataSource.close();
List<MockSpan> finishedSpans = mockTracer.finishedSpans();
assertEquals(1, finishedSpans.size());
MockSpan mockSpan = finishedSpans.get(0);
assertEquals(Tags.SPAN_KIND_CLIENT, mockSpan.tags().get(Tags.SPAN_KIND.getKey()));
assertEquals(JdbcTracingUtils.COMPONENT_NAME, mockSpan.tags().get(Tags.COMPONENT.getKey()));
assertThat(mockSpan.tags().get(Tags.DB_STATEMENT.getKey()).toString()).isNotEmpty();
assertEquals("h2", mockSpan.tags().get(Tags.DB_TYPE.getKey()));
assertEquals("sa", mockSpan.tags().get(Tags.DB_USER.getKey()));
assertEquals(0, mockSpan.generatedErrors().size());
assertNull(mockTracer.activeSpan());
}
示例9: spring_with_parent
import org.springframework.jdbc.core.JdbcTemplate; //导入方法依赖的package包/类
@Test
public void spring_with_parent() throws Exception {
try (Scope ignored = mockTracer.buildSpan("parent").startActive(true)) {
BasicDataSource dataSource = getDataSource(false);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("CREATE TABLE with_parent_1 (id INTEGER)");
jdbcTemplate.execute("CREATE TABLE with_parent_2 (id INTEGER)");
dataSource.close();
}
List<MockSpan> spans = mockTracer.finishedSpans();
assertEquals(3, spans.size());
checkSameTrace(spans);
}
示例10: spring_with_parent_and_active_span_only
import org.springframework.jdbc.core.JdbcTemplate; //导入方法依赖的package包/类
@Test
public void spring_with_parent_and_active_span_only() throws Exception {
try (Scope ignored = mockTracer.buildSpan("parent").startActive(true)) {
BasicDataSource dataSource = getDataSource(true);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("CREATE TABLE with_parent_skip_1 (id INTEGER)");
jdbcTemplate.execute("CREATE TABLE with_parent_skip_2 (id INTEGER)");
dataSource.close();
}
List<MockSpan> spans = mockTracer.finishedSpans();
assertEquals(3, spans.size());
checkSameTrace(spans);
}
示例11: zoekPersonen
import org.springframework.jdbc.core.JdbcTemplate; //导入方法依赖的package包/类
@Override
public List<Long> zoekPersonen(final SqlStamementZoekPersoon sql, final boolean postgres) throws QueryCancelledException {
final JdbcTemplate jdbcTemplate = new JdbcTemplate(masterDataSource);
if (postgres) {
//statement timeout zorgt ook voor het cancellen van query op server
jdbcTemplate.execute(String.format("set local statement_timeout = %d", TimeUnit.SECONDS.toMillis(maxStatementDuurSec)));
}
try {
return jdbcTemplate.queryForList(sql.getSql(), Long.class, (Object[]) sql.getParameters().toArray());
} catch (DataAccessException e) {
if (e.getCause() != null && e.getCause() instanceof SQLException && SQL_TIMEOUT_CODE.equals(((SQLException) e.getCause()).getSQLState())) {
throw new QueryCancelledException(e);
}
throw e;
}
}
示例12: testWithSpanOnlyNoParent
import org.springframework.jdbc.core.JdbcTemplate; //导入方法依赖的package包/类
@Test
public void testWithSpanOnlyNoParent() throws SQLException {
BasicDataSource dataSource = getDataSource(";traceWithActiveSpanOnly=true");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("CREATE TABLE skip_new_spans (id INTEGER)");
dataSource.close();
List<MockSpan> finishedSpans = mockTracer.finishedSpans();
assertEquals(0, finishedSpans.size());
assertNull(mockTracer.scopeManager().active());
}
示例13: testSpring
import org.springframework.jdbc.core.JdbcTemplate; //导入方法依赖的package包/类
@Test
public void testSpring() throws Exception {
DataSource ds = wrap(createHsqlDataSource());
JdbcTemplate jdbc = new JdbcTemplate(ds);
jdbc.execute(DROP_USER);
jdbc.execute(CREATE_TABLE_USER);
jdbc.update(INSERT_INTO_USER, 1, "user1");
User user = jdbc.queryForObject(SELECT_FROM_USER_BY_ID, new BeanPropertyRowMapper<>(User.class), 1);
assertEquals(new User(1, "user1"), user);
jdbc.update(DELETE_FROM_USER_BY_ID, 1);
}
示例14: getDialect
import org.springframework.jdbc.core.JdbcTemplate; //导入方法依赖的package包/类
protected IDialect getDialect(JdbcTemplate jdbcTemplate){
return jdbcTemplate.execute(new ConnectionCallback<IDialect>(){
public IDialect doInConnection(Connection connection) throws SQLException,
DataAccessException {
IDialect result=null;
for(IDialect dialect:dialects){
if(dialect.support(connection)){
result=dialect;
break;
}
}
return result;
}
});
}
示例15: testSpring
import org.springframework.jdbc.core.JdbcTemplate; //导入方法依赖的package包/类
@Test
public void testSpring() throws Exception {
DataSource ds = wrap(createH2DataSource());
JdbcTemplate jdbc = new JdbcTemplate(ds);
jdbc.execute(DROP_USER);
jdbc.execute(CREATE_TABLE_USER);
jdbc.update(INSERT_INTO_USER, 1, "user1");
User user = jdbc.queryForObject(SELECT_FROM_USER_BY_ID, new BeanPropertyRowMapper<>(User.class), 1);
assertEquals(new User(1, "user1"), user);
jdbc.update(DELETE_FROM_USER_BY_ID, 1);
}