lkallas
lkallas

Reputation: 1406

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?

Upvotes: 35

Views: 129432

Answers (5)

jccampanero
jccampanero

Reputation: 53411

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;
}

Upvotes: 16

TriS
TriS

Reputation: 4038

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;
}

Upvotes: 3

Anton Yuriev
Anton Yuriev

Reputation: 628

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

Upvotes: 1

jlewkovich
jlewkovich

Reputation: 2775

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;
}

Upvotes: 61

Laszlo Lugosi
Laszlo Lugosi

Reputation: 3829

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;
}

Upvotes: 7

Related Questions