Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how can I use LIKE expression on JdbcTemplate?

 
andy brave
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all hi to ervey one!
The problem is this: i have a



So the probklem is that I can't do the query.

It throws an SQLException

org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL [SELECT * FROM Guitarra WHERE numeroSerie LIKE '?' AND modelo LIKE '?' AND numeroCuerdas LIKE '?' AND nueva LIKE '?' AND color LIKE '?' AND precio LIKE '?' AND fabricante LIKE '?' AND maderaAnterior LIKE '?' AND maderaPosterior LIKE '?' AND tipo LIKE '?']; Parameter index out of range (1 > number of parameters, which is 0).; nested exception is java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:106)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
.
.
.

So how can I use the like expression to do this "SELECT * FROM guitarra WHERE numeroSerie LIKE '%21' AND color LIKE '%' AND numeroCuerdas LIKE '%';" but using in Java in JdbcTemplate??

By the way i'm trying to build a query to search by specific fields I mean, if someone searches a guitar "azul" and 12 cuerdas, this query should work, so what do you recommend me to do??

Greetings and thanks!!

 
lakshman siva
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

@Override
public List<Book> searchBooks() {
@SuppressWarnings("unchecked")
List<Book> list=
jdbcTemplate.query("select bid,bname,author from book where author like ? or bid like ? "
,new String[]{"%Man%","%BCA%"}
,new RowMapper() {

@Override
public Object mapRow(ResultSet rs, int arg1) throws SQLException {
Book book=new Book();
book.setBid(rs.getString(1));
book.setBname(rs.getString(2));
book.setAuthor(rs.getString(3));
return book;
}
});
return list;
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic