Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a NamedParamterJdbcTemplate object to specify a mysql field and value to query on
    primarykey
    data
    text
    <p>I am currently in the process of learning the Java Spring Framework, and I am having difficulty understanding why the following query is failing to return any results from the database.</p> <p>I am ultimately trying to create a <code>where</code> method in my <code>OffersDAO</code> class that allows my to query on a specific field, for a specific value.</p> <pre><code>public List&lt;Offer&gt; where(String field, String value){ MapSqlParameterSource params = new MapSqlParameterSource(); params.addValue("field", field); params.addValue("value", value); String sql = "select * from offers where :field = :value"; return jdbc.query(sql, params, new RowMapper&lt;Offer&gt;(){ public Offer mapRow(ResultSet rs, int arg1) throws SQLException { Offer offer = new Offer(); offer.setId(rs.getInt("id")); offer.setName(rs.getString("name")); offer.setText(rs.getString("text")); offer.setEmail(rs.getString("email")); return offer; } }); } </code></pre> <p>I am able to successfully query the database for results when I specify the field explicitly, as follows:</p> <pre><code>String sql = "select * from offers where name = :value"; </code></pre> <p>Obviously there is something wrong with specifying the field name dynamically. My guess is it is most likely due to the fact that the <code>field</code> key is being inserted as a mysql string (with <code>''</code>), when in fact mysql expects a column name for the :field placeholder.</p> <p>My questions are as follows:</p> <ol> <li><p>Is there a way to accomplish what I am attempting to do above, using the jdbc NamedParameterJdbcTemplate class?</p></li> <li><p>If I cannot accomplish the above, by what means can I?</p></li> </ol> <p>Thank you</p> <p>Edit: No exceptions are thrown. In the case when I am attempting to supply the column name, a empty result set is returned.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload