简体   繁体   中英

Using Spring JdbcTemplate to extract one string

Can't seem to find a way to get one string from table using JdbcTemplate query. This is the table my sql returns:

ID | STREET_NAME
------------------------
1  | Elm street

Now how am I supposed to get the value of STREET_NAME. SQL always returns one row, so no need to worry about returning more than one row.

For some background info: INNER JOIN and COUNT in the same query

Using Tony Stark answer to get my table.

But how can I extract "Elm street" from it using JdbcTemplate?

It would help a lot to know what your SQL query looks like, but assuming it's something like SELECT STREET_NAME FROM table WHERE ID=1;

CODE:

public String getStreetNameById(int id) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "SELECT STREET_NAME FROM table WHERE ID=?";

    String streetName = (String) jdbcTemplate.queryForObject(
            sql, new Object[] { id }, String.class);

    return streetName;
}

If you want to get only one column "string" from your table (or any query with joins), you have to say the name of the column.

Using SELECT * FROM TABLE is a very-very bad practice by the way. I bet you did this.

@JLewkovich modified code:

public String getStreetNameById(int id) {
  JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
  String sql = "SELECT STREET_NAME FROM table WHERE ID=?";

  String streetName = (String) jdbcTemplate.queryForObject(
        sql, new Object[] { id }, String.class);

  return streetName;
}

But what if there is 0 or more than one result? Think about it!

But to getting a sequence value (in Oracle), this should work.

public Long getSequence() {
  Long seq;
  String sql = "select SEQ_XY.NEXTVAL from dual";
  seq = jdbcTemplateObject.queryForObject(sql, new Object[] {}, Long.class);
  return seq;
}

The class JdbcTemplate implements JdbcOperations .

If you look at the queryForObject javadocs in JdbcOperations it states:

Deprecated . as of 5.3, in favor of queryForObject(String, Class, Object...)

Basically, they have changed the method signature to get rid of Object[] arguments in the method signatures in favor of varargs. Please, see the relevant Github issue .

You can rewrite answer of @jlewkovich with the new method signature like this:

public String getStreetNameById(int id) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "SELECT STREET_NAME FROM table WHERE ID=?";

    String streetName = (String) jdbcTemplate.queryForObject(
            sql, String.class, id);

    return streetName;
}

As per latest specification queryForObject with below syntax is now deprecated

<T> T   queryForObject(String sql, Object[] args, Class<T> requiredType)

New method uses varargs .

<T> T   queryForObject(String sql, Class<T> requiredType, Object... args) 

Updated Solution: We have to replace the class type with Object args and vice-versa.

Sql Query: SELECT STREET_NAME FROM table WHERE ID=1;

public String getStreetNameById(int id) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "SELECT STREET_NAME FROM table WHERE ID=?";

    String streetName = (String) jdbcTemplate.queryForObject(
            sql, String.class, new Object[] { id });

    return streetName;
}

I usually do this way:

String result = DataAccessUtils.singleResult(
    jdbcTemplate.queryForList(
        "SELECT street_name FROM table WHERE id = :id",
        Collections.singletonMap("id", id),
        String.class
    )
)

queryForList is used instead of queryForObject for handling empty results. queryForObject throws EmptyResultDataAccessException for empty results. Often it is not desired behavior.

DataAccessUtils.singleResult + queryForList :

  • returns null for empty result
  • returns single result if exactly 1 row found
  • throws exception if more then 1 rows found. Should not happen for primary key / unique index search

DataAccessUtils.singleResult

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM