Hi Kim,
The JDBI documentation <http://jdbi.org> briefly mentions dynamic inserts
and queries, but the documentation is unfortunately sparse and incomplete.
Basically, while most of the examples show SQL queries created using @SqlQuery
annotations with @Bind to bind input values, if you need to create the
query on the fly you can explicitly makes these calls in regular Java like
this:
public List<String> *getNames*(*final* *int* minCredits, *final* String
type) {
*final* DBI dbi = *new* DBI("jdbc:h2:mem:test");
*try* (Handle h = dbi.open()) {
String sql = "select name from Customers where credits >=
:minCredits";
*if* (type != *null*) {
sql += " and type = :type";
}
*final* Query<Map<String, Object>> q = h.createQuery(sql);
q.bind("minCredits", minCredits);
*if* (type != *null*) {
q.bind("type", type);
}
*final* List<String> names = q.map(StringColumnMapper.*INSTANCE*
).list();
*return* names;
}
}
This allows you to create the query based on a dynamic list of parameters.
I hope this helps.
On Friday, October 14, 2016 at 4:15:11 PM UTC-4, Kim Kantola wrote:
>
> Hi All,
>
> I am new to JDBI. I have seen several simple examples of annotating a DAO
> with a query like "Select * from tableName where id=?".
>
> Could someone point me to documentation for more complex queries such as
> searching by a dynamic list of parameters? For example, a user can search
> by name, age, id, height, etc, but I am unaware until runtime which
> combination of these search parameters may come in to my search method. I
> may not get a search value for age, and so do not need to bind anything to
> the search query for that.
>
> Any pointers appreciated,
> ~Kim
>
--
You received this message because you are subscribed to the Google Groups
"dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.