Example usage for org.springframework.jdbc.core JdbcOperations queryForObject

List of usage examples for org.springframework.jdbc.core JdbcOperations queryForObject

Introduction

In this page you can find the example usage for org.springframework.jdbc.core JdbcOperations queryForObject.

Prototype

@Nullable
<T> T queryForObject(String sql, Class<T> requiredType, @Nullable Object... args) throws DataAccessException;

Source Link

Document

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result object.

Usage

From source file:org.springframework.jdbc.core.simple.SimpleJdbcTemplateTests.java

public void testQueryForObjectWithArgs() throws Exception {
    String sql = "SELECT SOMEDATE FROM BAR WHERE ID=? AND XY=?";
    Date expectedResult = new Date();
    double arg1 = 24.7;
    String arg2 = "foo";
    Object arg3 = new Object();

    MockControl mc = MockControl.createControl(JdbcOperations.class);
    JdbcOperations jo = (JdbcOperations) mc.getMock();
    jo.queryForObject(sql, new Object[] { arg1, arg2, arg3 }, Date.class);
    mc.setDefaultMatcher(new ArrayMatcher());
    mc.setReturnValue(expectedResult);//w ww. j  a v a  2s. c om
    mc.replay();

    SimpleJdbcTemplate jth = new SimpleJdbcTemplate(jo);
    Date result = jth.queryForObject(sql, Date.class, arg1, arg2, arg3);
    assertEquals(expectedResult, result);
    mc.verify();
}

From source file:org.springframework.jdbc.core.simple.SimpleJdbcTemplateTests.java

public void testQueryForObjectWithArgArray() throws Exception {
    String sql = "SELECT SOMEDATE FROM BAR WHERE ID=? AND XY=?";
    Date expectedResult = new Date();
    double arg1 = 24.7;
    String arg2 = "foo";
    Object arg3 = new Object();

    MockControl mc = MockControl.createControl(JdbcOperations.class);
    JdbcOperations jo = (JdbcOperations) mc.getMock();
    jo.queryForObject(sql, new Object[] { arg1, arg2, arg3 }, Date.class);
    mc.setDefaultMatcher(new ArrayMatcher());
    mc.setReturnValue(expectedResult);//from  www  .j a  va  2  s .  co m
    mc.replay();

    SimpleJdbcTemplate jth = new SimpleJdbcTemplate(jo);
    Object args = new Object[] { arg1, arg2, arg3 };
    Date result = jth.queryForObject(sql, Date.class, args);
    assertEquals(expectedResult, result);
    mc.verify();
}

From source file:org.springframework.jdbc.core.simple.SimpleJdbcTemplateTests.java

public void testQueryForObjectWithMap() throws Exception {
    String sql = "SELECT SOMEDATE FROM BAR WHERE ID=? AND XY=?";
    Date expectedResult = new Date();
    double arg1 = 24.7;
    String arg2 = "foo";
    Object arg3 = new Object();

    MockControl mc = MockControl.createControl(JdbcOperations.class);
    JdbcOperations jo = (JdbcOperations) mc.getMock();
    jo.queryForObject(sql, new Object[] { arg1, arg2, arg3 }, Date.class);
    mc.setDefaultMatcher(new ArrayMatcher());
    mc.setReturnValue(expectedResult);/*from  w ww .j  a v a 2 s  . com*/
    mc.replay();

    SimpleJdbcTemplate jth = new SimpleJdbcTemplate(jo);
    Date result = jth.queryForObject(sql, Date.class, arg1, arg2, arg3);
    assertEquals(expectedResult, result);
    mc.verify();
}

From source file:org.springframework.jdbc.core.simple.SimpleJdbcTemplateTests.java

public void testQueryForObjectWithRowMapperAndArgs() throws Exception {
    String sql = "SELECT SOMEDATE FROM BAR WHERE ID=? AND XY=?";
    Date expectedResult = new Date();
    double arg1 = 24.7;
    String arg2 = "foo";
    Object arg3 = new Object();

    ParameterizedRowMapper<Date> rm = new ParameterizedRowMapper<Date>() {
        public Date mapRow(ResultSet rs, int rowNum) {
            return new Date();
        }// w w w  .  j  a v  a  2s  .c  o  m
    };

    MockControl mc = MockControl.createControl(JdbcOperations.class);
    JdbcOperations jo = (JdbcOperations) mc.getMock();
    jo.queryForObject(sql, new Object[] { arg1, arg2, arg3 }, rm);
    mc.setDefaultMatcher(new ArrayMatcher());
    mc.setReturnValue(expectedResult);
    mc.replay();

    SimpleJdbcTemplate jth = new SimpleJdbcTemplate(jo);
    Date result = jth.queryForObject(sql, rm, arg1, arg2, arg3);
    assertEquals(expectedResult, result);
    mc.verify();
}

From source file:org.springframework.jdbc.core.simple.SimpleJdbcTemplateTests.java

public void testQueryForObjectWithRowMapperAndMap() throws Exception {
    String sql = "SELECT SOMEDATE FROM BAR WHERE ID=? AND XY=?";
    Date expectedResult = new Date();
    double arg1 = 24.7;
    String arg2 = "foo";
    Object arg3 = new Object();

    ParameterizedRowMapper<Date> rm = new ParameterizedRowMapper<Date>() {
        public Date mapRow(ResultSet rs, int rowNum) {
            return new Date();
        }/* w w  w  .j  a v  a  2  s  . c  o m*/
    };

    MockControl mc = MockControl.createControl(JdbcOperations.class);
    JdbcOperations jo = (JdbcOperations) mc.getMock();
    jo.queryForObject(sql, new Object[] { arg1, arg2, arg3 }, rm);
    mc.setDefaultMatcher(new ArrayMatcher());
    mc.setReturnValue(expectedResult);
    mc.replay();

    SimpleJdbcTemplate jth = new SimpleJdbcTemplate(jo);
    Date result = jth.queryForObject(sql, rm, arg1, arg2, arg3);
    assertEquals(expectedResult, result);
    mc.verify();
}