java - It asks for id and asks for name and then shows exception saying error in MySql syntax...how do i resolve? -
this question has answer here:
i have tried both ways queryupdate, shows same exception.
string queryupdate = "update student set name = '?' id = '?' ";
string queryupdate = "update student set name = ? id = ? ";
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.sql.connection; import java.sql.sqlexception; import java.sql.statement; import java.util.scanner; public class updaterecord { private connection connection; statement statement; string queryupdate = "update student set name = ? id = ? "; scanner sc = new scanner(system.in); bufferedreader buffer = new bufferedreader(new inputstreamreader(system.in)); public updaterecord() { try { class.forname("com.mysql.jdbc.driver"); connection = createconnection.getdatabase(); // creating connection class created in file createconnection.java statement = connection.preparestatement(query); system.out.println("enter id..."); int id = integer.parseint(buffer.readline()); system.out.println("enter name..."); string name = buffer.readline(); statement.executeupdate(query); system.out.println("\n\n\t\t!!!.........updated..........!!!"); } catch(classnotfoundexception e) { e.printstacktrace(); } catch(sqlexception s) { s.printstacktrace(); } catch(ioexception i) { i.printstacktrace(); } } public static void main(string[] args) { new updaterecord(); } } mysql exception:
class loaded connection created enter id... 1 enter name... hamid com.mysql.jdbc.exceptions.jdbc4.mysqlsyntaxerrorexception: you have error in sql syntax; check manual corresponds mysql server version right syntax use near '? id = ?' @ line 1 @ sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method) @ sun.reflect.nativeconstructoraccessorimpl.newinstance(nativeconstructoraccessorimpl.java:62) @ sun.reflect.delegatingconstructoraccessorimpl.newinstance(delegatingconstructoraccessorimpl.java:45) @ java.lang.reflect.constructor.newinstance(constructor.java:423) @ com.mysql.jdbc.util.handlenewinstance(util.java:406) @ com.mysql.jdbc.util.getinstance(util.java:381) @ com.mysql.jdbc.sqlerror.createsqlexception(sqlerror.java:1031) @ com.mysql.jdbc.sqlerror.createsqlexception(sqlerror.java:957) @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:3376) @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:3308) @ com.mysql.jdbc.mysqlio.sendcommand(mysqlio.java:1837) @ com.mysql.jdbc.mysqlio.sqlquerydirect(mysqlio.java:1961) @ com.mysql.jdbc.connectionimpl.execsql(connectionimpl.java:2537) @ com.mysql.jdbc.statementimpl.executeupdate(statementimpl.java:1564) @ com.mysql.jdbc.statementimpl.executeupdate(statementimpl.java:1485) @ updaterecord.(updaterecord.java:34) @ updaterecord.main(updaterecord.java:56)
it seems want use prepared statements positional parameters. choice, syntax has problems.
first, following line need change:
statement.executeupdate(query); preparedstatement#executeupdate() not take parameters. check javadoc possible mistake may have made.
also, never assigned positional parameters. try following full code snippet:
preparedstatement statement; connection = createconnection.getdatabase(); statement = connection.preparestatement(query); system.out.println("enter id..."); int id = integer.parseint(buffer.readline()); system.out.println("enter name..."); string name = buffer.readline(); statement.setstring(1, name); statement.setint(2, id); statement.executeupdate();
Comments
Post a Comment