Example usage for org.springframework.jdbc.core JdbcTemplate queryForMap

List of usage examples for org.springframework.jdbc.core JdbcTemplate queryForMap

Introduction

In this page you can find the example usage for org.springframework.jdbc.core JdbcTemplate queryForMap.

Prototype

@Override
    public Map<String, Object> queryForMap(String sql) throws DataAccessException 

Source Link

Usage

From source file:io.spring.JpaApplicationTests.java

@Test
public void testBatchJobApp() throws Exception {
    final String INSERT_MESSAGE = "Hibernate: insert into task_run_output (";
    SpringApplication.run(JpaApplication.class, "--spring.datasource.url=" + DATASOURCE_URL,
            "--spring.datasource.username=" + DATASOURCE_USER_NAME,
            "--spring.datasource.driverClassName=" + DATASOURCE_DRIVER_CLASS_NAME,
            "--spring.jpa.database-platform=org.hibernate.dialect.H2Dialect");
    String output = this.outputCapture.toString();
    assertTrue("Unable to find the insert message: " + output, output.contains(INSERT_MESSAGE));
    JdbcTemplate template = new JdbcTemplate(this.dataSource);
    Map<String, Object> result = template.queryForMap("Select * from TASK_RUN_OUTPUT");
    assertThat((result.get("ID")), is(1L));
    assertThat(((String) result.get("OUTPUT")), containsString("Executed at"));
}

From source file:uk.ac.kcl.utils.PostGresTestUtils.java

@Override
public Map<String, Object> getRowInOutputTable(int primaryKey) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(targetDataSource);
    return jdbcTemplate.queryForMap(
            "SELECT * FROM tblOutputDocs WHERE primaryKeyFieldValue = " + Integer.toString(primaryKey));
}

From source file:org.springframework.jdbc.core.JdbcTemplateQueryTests.java

public void testQueryForMapWithSingleRowAndColumn() throws Exception {
    String sql = "SELECT AGE FROM CUSTMR WHERE ID < 3";

    mockResultSetMetaData.getColumnCount();
    ctrlResultSetMetaData.setReturnValue(1);
    mockResultSetMetaData.getColumnLabel(1);
    ctrlResultSetMetaData.setReturnValue("age", 1);

    mockResultSet.getMetaData();/*from  ww  w .  j a v  a  2 s .  co m*/
    ctrlResultSet.setReturnValue(mockResultSetMetaData);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getObject(1);
    ctrlResultSet.setReturnValue(new Integer(11));
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockStatement.executeQuery(sql);
    ctrlStatement.setReturnValue(mockResultSet);
    if (debugEnabled) {
        mockStatement.getWarnings();
        ctrlStatement.setReturnValue(null);
    }
    mockStatement.close();
    ctrlStatement.setVoidCallable();

    mockConnection.createStatement();
    ctrlConnection.setReturnValue(mockStatement);

    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource);

    Map map = template.queryForMap(sql);
    assertEquals("Wow is Integer", 11, ((Integer) map.get("age")).intValue());
}