foo
foo

Reputation: 481

Spring jdbcTemplate -add filter to query

I write a queries in jdbcTemplate to create reports, Now I want to add filter to the reports. for example if i have query to create report of all contacts per day, now I want to filter it that be just between two dates not all What the best way to do it ? There is a special way to do it in Spring jdbcTemplate?

Upvotes: 1

Views: 3106

Answers (1)

Atilla Ozgur
Atilla Ozgur

Reputation: 14701

See following tutorial and example code taken from that one below.

public Person select(String name){
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("name", name);

     String selectAllSql = "SELECT * FROM PERSON where name = :name";

     List<Person> persons = getJdbcTemplate().query(selectAllSql, new PersonRowMapper(),parameters);

     return persons.get(0);
  }

}

To answer your comment:

If some times i dont use the filter what i do with the parmeters what i have to send

You have two options

1) You will need to use if statements to construct your sql and parameters. 2) Use another library for this purpose. I used ElSql in production before.

Upvotes: 1

Related Questions