mysql - Java : SQL syntax error for entering values in Table -
i trying add 2 methods withdraw , deposit
money in bank class . database name javatest
. table name bank
, following code . problem when run code compiler says you have error in sql syntax;
did check code 3-4 times unable please me .
public static void main(string[] args) { connection connection= null ; statement stmt = null ; try { class.forname("com.mysql.jdbc.driver"); connection= drivermanager.getconnection("jdbc:mysql://localhost:3306/javatest","root",""); stmt= connection.createstatement(); withdrawfromchecking(connection, stmt, new bigdecimal(100), 1); depositinsaving(connection, stmt, new bigdecimal(444), 1); stmt.executebatch(); system.out.println("done"); } catch (classnotfoundexception e) {e.getmessage();} catch (sqlexception e) {e.printstacktrace();} { if(connection!=null){try {connection.close();} catch (sqlexception e) {e.printstacktrace();}} if(stmt!=null){try {stmt.close();} catch (sqlexception e) {e.printstacktrace();}} } } public static void withdrawfromchecking(connection connection ,statement stmt, bigdecimal amount , int id ) throws sqlexception { stmt.addbatch("update bank set checkingbalance = checkingbalance-"+amount+"where id="+id); } public static void depositinsaving(connection connection ,statement stmt, bigdecimal amount , int id ) throws sqlexception { stmt.addbatch("update bank set savingbalance = savingbalance+ "+amount+"where id="+id); } }
error comes line - stmt.executebatch();
when run program
edit : exact error statement
java.sql.batchupdateexception: have error in sql syntax; check manual corresponds mysql server version right syntax use near 'id =1' @ line 1 @ com.mysql.jdbc.statementimpl.executebatch(statementimpl.java:1193) @ mypackage.bankaccount.main(bankaccount.java:24)
in code (line 24 stmt.executebatch();
in both of sqls, there no space between concatenation of amount , word where
-- looks this: checkingbalance-100where id=
.
place space before both where
words.
stmt.addbatch("update bank set checkingbalance = checkingbalance-" // +- add space here // v +amount+" id="+id);
Comments
Post a Comment