java - JDBC is throwing grammar error when the query is working in php myadmin -
i working on java website using jdbc mysql. here simple function fetch data sql,
@override public list fetchmusic(string _uname, string _chnl_name) throws sqlexception { string sql = "select * `channel_songs` `uid` = " + " (select `id` `user` binary `uname` = ?)"+ " , `chnl_id` = (select `id` `channel` binary `chnl_name` = ?);"; connection conn = datasource.getconnection(); preparedstatement ps = conn.preparestatement(sql); ps.setstring(1, _uname); ps.setstring(2, _chnl_name); list<map<string, object>> musiclistsbychnl = jdbctemplate.queryforlist(string.valueof(ps)); return musiclistsbychnl; }
so, when running this, getting error says,
org.springframework.jdbc.badsqlgrammarexception: statementcallback; bad sql grammar [com.mysql.jdbc.jdbc4preparedstatement@3a4eb50a: select * `channel_songs`]; nested exception com.mysql.jdbc.exceptions.jdbc4.mysqlsyntaxerrorexception: have error in sql syntax; check manual corresponds mysql server version right syntax use near 'com.mysql.jdbc.jdbc4preparedstatement@3a4eb50a: select * `channel_songs`' @ line 1
so, don't know wrong or is. when trying query on phpmyadmin, working , returning data. can me here?
thanx in advance. ;)
you using prepared statement wrong... preparedstatement
not "convenient" device build escaped sql strings. result of string.valueof(ps)
not defined valid sql command @ all. ps.tostring()
, value should used debugging only. exception gave:
check manual corresponds mysql server version right syntax use near 'com.mysql.jdbc.jdbc4preparedstatement@3a4eb50a: select *
channel_songs
' @ line 1
you supposed this:
@override public list fetchmusic(string _uname, string _chnl_name) throws sqlexception { string sql = "select * `channel_songs` `uid` = " + " (select `id` `user` binary `uname` = ?)"+ " , `chnl_id` = (select `id` `channel` binary `chnl_name` = ?);"; connection conn = datasource.getconnection(); preparedstatement ps = conn.preparestatement(sql); ps.setstring(1, _uname); ps.setstring(2, _chnl_name); // is, original code falls apart.... // list<map<string, object>> musiclistsbychnl = jdbctemplate.queryforlist(string.valueof(ps)); list<map<string,object>> answer = new arraylist<>(); try (resultset rs = ps.executequery()) { while (rs.next()) { // read single row of result set using // getters of `rs` instance (rs.getstring(...), // rs.getint(...), ...) } } return musiclistsbychnl; }
if not want write boiler plate stuff (and seem have jdbctemplate
@ hand): spring's jdbctemplate
has lot more convenience methods, deal assigning query parameters , like. find 1 closest needs.
Comments
Post a Comment