当前位置: 首页>>代码示例>>Java>>正文


Java JdbcTemplate.execute方法代码示例

本文整理汇总了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);
}
 
开发者ID:luoyaogui,项目名称:otter-G,代码行数:24,代码来源:AbstractDbDialect.java

示例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());
}
 
开发者ID:opentracing-contrib,项目名称:java-p6spy,代码行数:17,代码来源:SpringTest.java

示例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());
}
 
开发者ID:opentracing-contrib,项目名称:java-p6spy,代码行数:17,代码来源:SpringTest.java

示例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;
}
 
开发者ID:bsteker,项目名称:bdf2,代码行数:29,代码来源:DbService.java

示例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
    }
}
 
开发者ID:ops4j,项目名称:org.ops4j.pax.transx,代码行数:27,代码来源:HsqlTest.java

示例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
    }
}
 
开发者ID:ops4j,项目名称:org.ops4j.pax.transx,代码行数:27,代码来源:H2Test.java

示例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
    }
}
 
开发者ID:ops4j,项目名称:org.ops4j.pax.transx,代码行数:27,代码来源:H2Test.java

示例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());
}
 
开发者ID:opentracing-contrib,项目名称:java-jdbc,代码行数:23,代码来源:SpringTest.java

示例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);
}
 
开发者ID:opentracing-contrib,项目名称:java-jdbc,代码行数:18,代码来源:SpringTest.java

示例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);
}
 
开发者ID:opentracing-contrib,项目名称:java-jdbc,代码行数:18,代码来源:SpringTest.java

示例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;
    }
}
 
开发者ID:MinBZK,项目名称:OperatieBRP,代码行数:17,代码来源:ZoekPersoonRepositoryImpl.java

示例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());
}
 
开发者ID:opentracing-contrib,项目名称:java-p6spy,代码行数:15,代码来源:SpringTest.java

示例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);
}
 
开发者ID:ops4j,项目名称:org.ops4j.pax.transx,代码行数:15,代码来源:HsqlTest.java

示例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;
		}
	});
}
 
开发者ID:bsteker,项目名称:bdf2,代码行数:16,代码来源:JdbcDao.java

示例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);
}
 
开发者ID:ops4j,项目名称:org.ops4j.pax.transx,代码行数:15,代码来源:H2Test.java


注:本文中的org.springframework.jdbc.core.JdbcTemplate.execute方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。