org.springframework.jdbc.core.JdbcTemplate.queryForObject()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(141)

本文整理了Java中org.springframework.jdbc.core.JdbcTemplate.queryForObject()方法的一些代码示例,展示了JdbcTemplate.queryForObject()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JdbcTemplate.queryForObject()方法的具体详情如下:
包路径:org.springframework.jdbc.core.JdbcTemplate
类名称:JdbcTemplate
方法名:queryForObject

JdbcTemplate.queryForObject介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Count the rows in the given table.
 * @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations
 * @param tableName name of the table to count rows in
 * @return the number of rows in the table
 */
public static int countRowsInTable(JdbcTemplate jdbcTemplate, String tableName) {
  Integer result = jdbcTemplate.queryForObject("SELECT COUNT(0) FROM " + tableName, Integer.class);
  return (result != null ? result : 0);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public <T> T queryForObject(String sql, Class<T> requiredType) throws DataAccessException {
  return queryForObject(sql, getSingleColumnRowMapper(requiredType));
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public <T> T queryForObject(String sql, Object[] args, int[] argTypes, Class<T> requiredType)
    throws DataAccessException {
  return queryForObject(sql, args, argTypes, getSingleColumnRowMapper(requiredType));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testScriptNameWithPattern() throws Exception {
  context = new ClassPathXmlApplicationContext("org/springframework/jdbc/config/jdbc-initialize-pattern-config.xml");
  DataSource dataSource = context.getBean("dataSource", DataSource.class);
  assertCorrectSetup(dataSource);
  JdbcTemplate t = new JdbcTemplate(dataSource);
  assertEquals("Dave", t.queryForObject("select name from T_TEST", String.class));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testQueryForObjectWithBigInteger() throws Exception {
  String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
  given(this.resultSet.next()).willReturn(true, false);
  given(this.resultSet.getObject(1, BigInteger.class)).willReturn(new BigInteger("22"));
  assertEquals(new BigInteger("22"), this.template.queryForObject(sql, BigInteger.class));
  verify(this.resultSet).close();
  verify(this.statement).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testQueryForObjectWithBigDecimal() throws Exception {
  String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
  given(this.resultSet.next()).willReturn(true, false);
  given(this.resultSet.getBigDecimal(1)).willReturn(new BigDecimal("22.5"));
  assertEquals(new BigDecimal("22.5"), this.template.queryForObject(sql, BigDecimal.class));
  verify(this.resultSet).close();
  verify(this.statement).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testQueryForInt() throws Exception {
  String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
  given(this.resultSet.next()).willReturn(true, false);
  given(this.resultSet.getInt(1)).willReturn(22);
  int i = this.template.queryForObject(sql, Integer.class).intValue();
  assertEquals("Return of an int", 22, i);
  verify(this.resultSet).close();
  verify(this.statement).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testQueryForLong() throws Exception {
  String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
  given(this.resultSet.next()).willReturn(true, false);
  given(this.resultSet.getLong(1)).willReturn(87L);
  long l = this.template.queryForObject(sql, Long.class).longValue();
  assertEquals("Return of a long", 87, l);
  verify(this.resultSet).close();
  verify(this.statement).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void scriptWithMultipleStatements() throws Exception {
  databasePopulator.addScript(defaultSchema());
  databasePopulator.addScript(resource("db-test-data-multiple.sql"));
  DatabasePopulatorUtils.execute(databasePopulator, db);
  assertThat(jdbcTemplate.queryForObject("select COUNT(NAME) from T_TEST where NAME='Keith'", Integer.class),
    equalTo(1));
  assertThat(jdbcTemplate.queryForObject("select COUNT(NAME) from T_TEST where NAME='Dave'", Integer.class),
    equalTo(1));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testQueryForIntWithArgs() throws Exception {
  String sql = "SELECT AGE FROM CUSTMR WHERE ID = ?";
  given(this.resultSet.next()).willReturn(true, false);
  given(this.resultSet.getInt(1)).willReturn(22);
  int i = this.template.queryForObject(sql, new Object[] {3}, Integer.class).intValue();
  assertEquals("Return of an int", 22, i);
  verify(this.preparedStatement).setObject(1, 3);
  verify(this.resultSet).close();
  verify(this.preparedStatement).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void scriptWithMultipleStatementsAndNewlineSeparator() throws Exception {
  databasePopulator.addScript(defaultSchema());
  databasePopulator.addScript(resource("db-test-data-newline.sql"));
  DatabasePopulatorUtils.execute(databasePopulator, db);
  assertThat(jdbcTemplate.queryForObject("select COUNT(NAME) from T_TEST where NAME='Keith'", Integer.class),
    equalTo(1));
  assertThat(jdbcTemplate.queryForObject("select COUNT(NAME) from T_TEST where NAME='Dave'", Integer.class),
    equalTo(1));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testQueryForLongWithArgs() throws Exception {
  String sql = "SELECT AGE FROM CUSTMR WHERE ID = ?";
  given(this.resultSet.next()).willReturn(true, false);
  given(this.resultSet.getLong(1)).willReturn(87L);
  long l = this.template.queryForObject(sql, new Object[] {3}, Long.class).longValue();
  assertEquals("Return of a long", 87, l);
  verify(this.preparedStatement).setObject(1, 3);
  verify(this.resultSet).close();
  verify(this.preparedStatement).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testQueryForObjectWithInteger() throws Exception {
  String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
  given(this.resultSet.next()).willReturn(true, false);
  given(this.resultSet.getInt(1)).willReturn(22);
  assertEquals(Integer.valueOf(22), this.template.queryForObject(sql, Integer.class));
  verify(this.resultSet).close();
  verify(this.statement).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testQueryForObjectWithArgsAndInteger() throws Exception {
  String sql = "SELECT AGE FROM CUSTMR WHERE ID = ?";
  given(this.resultSet.next()).willReturn(true, false);
  given(this.resultSet.getInt(1)).willReturn(22);
  Object o = this.template.queryForObject(sql, new Object[] {3}, Integer.class);
  assertTrue("Correct result type", o instanceof Integer);
  verify(this.preparedStatement).setObject(1, 3);
  verify(this.resultSet).close();
  verify(this.preparedStatement).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void scriptWithMultipleStatementsAndLongSeparator() throws Exception {
  databasePopulator.addScript(defaultSchema());
  databasePopulator.addScript(resource("db-test-data-endings.sql"));
  databasePopulator.setSeparator("@@");
  DatabasePopulatorUtils.execute(databasePopulator, db);
  assertThat(jdbcTemplate.queryForObject("select COUNT(NAME) from T_TEST where NAME='Keith'", Integer.class),
    equalTo(1));
  assertThat(jdbcTemplate.queryForObject("select COUNT(NAME) from T_TEST where NAME='Dave'", Integer.class),
    equalTo(1));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void scriptWithMultipleStatementsAndWhitespaceSeparator() throws Exception {
  databasePopulator.addScript(defaultSchema());
  databasePopulator.addScript(resource("db-test-data-whitespace.sql"));
  databasePopulator.setSeparator("/\n");
  DatabasePopulatorUtils.execute(databasePopulator, db);
  assertThat(jdbcTemplate.queryForObject("select COUNT(NAME) from T_TEST where NAME='Keith'", Integer.class),
    equalTo(1));
  assertThat(jdbcTemplate.queryForObject("select COUNT(NAME) from T_TEST where NAME='Dave'", Integer.class),
    equalTo(1));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void scriptWithMultipleStatementsAndMultipleNewlineSeparator() throws Exception {
  databasePopulator.addScript(defaultSchema());
  databasePopulator.addScript(resource("db-test-data-multi-newline.sql"));
  databasePopulator.setSeparator("\n\n");
  DatabasePopulatorUtils.execute(databasePopulator, db);
  assertThat(jdbcTemplate.queryForObject("select COUNT(NAME) from T_TEST where NAME='Keith'", Integer.class),
    equalTo(1));
  assertThat(jdbcTemplate.queryForObject("select COUNT(NAME) from T_TEST where NAME='Dave'", Integer.class),
    equalTo(1));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testQueryForLongPrimitive() throws Exception {
  String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
  given(this.resultSet.next()).willReturn(true, false);
  given(this.resultSet.getLong(1)).willReturn(87L);
  long l = this.template.queryForObject(sql, long.class);
  assertEquals("Return of a long", 87, l);
  verify(this.resultSet).close();
  verify(this.statement).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testQueryForObjectWithIntegerAndNull() throws Exception {
  String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
  given(this.resultSet.next()).willReturn(true, false);
  given(this.resultSet.getInt(1)).willReturn(0);
  given(this.resultSet.wasNull()).willReturn(true);
  assertNull(this.template.queryForObject(sql, Integer.class));
  verify(this.resultSet).close();
  verify(this.statement).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testFactoryBeanLifecycle() throws Exception {
  EmbeddedDatabaseFactoryBean bean = new EmbeddedDatabaseFactoryBean();
  ResourceDatabasePopulator populator = new ResourceDatabasePopulator(resource("db-schema.sql"),
    resource("db-test-data.sql"));
  bean.setDatabasePopulator(populator);
  bean.afterPropertiesSet();
  DataSource ds = bean.getObject();
  JdbcTemplate template = new JdbcTemplate(ds);
  assertEquals("Keith", template.queryForObject("select NAME from T_TEST", String.class));
  bean.destroy();
}

相关文章