java - JavaScript error in Parameterized query -
here trying (for on day :( user clicks on link of book name , read name of book. take book name , make ajax request jersey resource. within jersey resource, call method in pojo class 1 method interacts database , gets data sent jersey resource. have got many errors have been able fix them 1 @ time. error stuck @ is:
you have error in sql syntax; check manual corresponds mysql server version right syntax use near '?' @ line 1
here javascript code:
function dealwithdata(nameofbook){ var bookname = encodeuri(nameofbook); console.log("http://localhost:8080/library/rest/books/allbooks/"+bookname); var requestdata = { "contenttype": "application/json", "datatype": "text", "type": "get", "url": "http://localhost:8080/library/rest/books/allbooks/"+bookname **//beforesend has been added edit original code** beforesend: function (jqxhr, settings) { var theurlbeingsent = settings.url; alert(theurlbeingsent); } }; var request = $.ajax(requestdata); request.success(function(data) { alert("success!!"); }); request.fail(function(jqxhr, status, errormessage) { if((errormessage = $.trim(errormessage)) === "") { alert("an unspecified error occurred. check server error log details."); } else { alert("an error occurred: " + errormessage); } }); }
for reason in above code, console.log line shows url spaces being encoded %20 while in variable 'requestdata', url doesn't have encoding. unable understand why.
here code resource:
@get @path("/allbooks/{bookname}") @produces(mediatype.application_json) public response getbook(@pathparam("bookname") string bookname){ system.out.println("book name is: "+ bookname); bookinformation bookinfo = new bookinformation(); string bookinformation =bookinfo.bookinformation(bookname); responsebuilder responsebuilder = response.status(status.ok); responsebuilder.entity(bookinformation); response response = responsebuilder.build(); return response; }
here bookinformation method:
public string bookinformation(string bookname){ string infoquery = "select * bookinfo name = ?"; resultset result = null; conn = newconnection.dbconnection(); try { preparedstatement = conn.preparestatement(infoquery); preparedstatement.setstring(1, bookname); result = preparedstatement.executequery(infoquery); } catch (sqlexception e) { e.printstacktrace(); } try { if(result != null){ while(result.next()){ availability = result.getstring("availability"); isbn = result.getint("isbn"); hardback = result.getstring("hardback"); paperback = result.getstring("paperback"); name = result.getstring("name"); } } else{ system.out.println("no result set obtained"); } } catch (sqlexception e) { e.printstacktrace(); } //i build string using string builder return string finalbookinformation = information.tostring(); return finalbookinformation; }
earlier, in datatype had json throwing different error, realized not building json changed datatype text , error went away. parametirized query doesn't execute. if try hard coding value database, works fine not when use prepared statement. want return json want work. appreciated. have tried researching , doing whatever can not working. encoding causing problem? ajax call? appreciated. thanks.
seems issue in database query execution please replace code
preparedstatement = conn.preparestatement(infoquery); preparedstatement.setstring(1, bookname); result = preparedstatement.executequery(infoquery);
with
preparedstatement = conn.preparestatement(infoquery); preparedstatement.setstring(1, bookname); result = preparedstatement.executequery();
Comments
Post a Comment