java - MySQL query correct syntax is not working in JdbcTemplate -
i'm using like
keyword query form table single search parameter. when run following sql statement in mysql workbench, it's working expected.
set @search = 'b'; select t.*,d.divisionname township t inner join division d on t.divisionid=d.divisionid t.townshipcode concat('%', @search, '%') or t.townshipname concat('%', @search, '%') or d.divisionname concat('%', @search, '%') order t.townshipcode limit 0,10
but, when execute form java code jdbctemplate
, got badsqlgrammarexception
. following java code :
public list<township> getlist(integer pagenumber, integer pagedisplaylength, string searchparameter) { int start = ((pagenumber - 1) * pagedisplaylength); string query = ""; if (null != searchparameter && !searchparameter.equals("")){ query = "set @search = '" + searchparameter + "'; " + "select t.*,d.divisionname township t " + "inner join division d on t.divisionid=d.divisionid " + "where t.townshipcode concat('%', @search, '%') or " + "t.townshipname concat('%', @search, '%') or " + "d.divisionname concat('%', @search, '%') " + "order t.townshipcode limit " + start + "," + pagedisplaylength; } else{ query = "select t.*,d.divisionname township t " + "inner join division d on t.divisionid=d.divisionid order t.townshipcode limit " + start + "," + pagedisplaylength; } list<township> townshiplist = getmultirecords(query); return townshiplist; }
and following getmultirecords
method it's base class :
protected list<t> getmultirecords(string query){ list<t> listresult = jdbctemplate.query(query, new rowmapper<t>(){ @override public t maprow(resultset rs, int rownum) throws sqlexception { t entity = readrecord(rs); return entity; } }); return listresult; }
here badsqlgrammarexception
details.
request processing failed; nested exception org.springframework.jdbc.badsqlgrammarexception: statementcallback; bad sql grammar [set @search = 'b'; select t.*,d.divisionname township t inner join division d on t.divisionid=d.divisionid t.townshipcode concat('%', @search, '%') or t.townshipname concat('%', @search, '%') or d.divisionname concat('%', @search, '%') order t.townshipcode limit 0,10]; nested exception com.mysql.jdbc.exceptions.jdbc4.mysqlsyntaxerrorexception: have error in sql syntax; check manual corresponds mysql server version right syntax use near 'select t.*,d.divisionname township t inner join division d on t.divis' @ line 1
please me find out problem.
i'd guess parser unhappy 2 statements using in 1 statement. instead of doing set
, adding parameters string
should use parameter binding. wouldn't surprised if set statement unsupported.
you statement misses closing semicolon well. missing exact/full error message it's guesswork.
Comments
Post a Comment