mysql - SQL syntax in JAVA forms getText(); -
just looking little help,
created application in java , using jtable gather data myphp database, using insert, update , delete sql commands user able manipulate data in table.
delete works perfectly, having trouble update , insert commands, wondering if can see if im using ("'+) incorrectly, im not eagle eyed more experienced seeing if can shed light :)
thanks!
insert code :
string query = "insert `supplier`(`company name`, `contact`, `address`, `postcode`, `phone`) values ('"+jtextfield_suppliercompany.gettext()+"','"+jtextfield_suppliercontact.gettext()+"',"+jtextfield_supplieraddress.gettext()+"','"+jtextfield_supplierpostcode.gettext()+"',"+jtextfield_supplierphone.gettext()+")";
update code:
string query = "update `supplier` set `company name`='"+jtextfield_suppliercompany.gettext() + "',`contact`='"+jtextfield_suppliercontact.gettext() + "',`address`="+jtextfield_supplieraddress.gettext() + "',`postcode`="+jtextfield_supplierpostcode.gettext() + "',`phone`="+jtextfield_supplierphone.gettext() + " `id` = "+jtextfield_supplierid.gettext();
error:
the error throwing misuse of clause in "update" statement... may obvious cant head around it.
to avoid these type of syntax errors, or sql injection, can use preparedstatement instead, simple , helpful :
string query = "insert `supplier`(`company name`, `contact`, `address`, `postcode`, `phone`) " + "values (?, ?, ?, ?, ?)"; try (preparedstatement pstm = conn.preparestatement(query)) { pstm.setstring(1, jtextfield_suppliercompany.gettext()); pstm.setstring(2, jtextfield_suppliercontact.gettext()); pstm.setstring(3, jtextfield_supplieraddress.gettext()); pstm.setstring(4, jtextfield_supplierpostcode.gettext()); pstm.setstring(5, jtextfield_supplierphone.gettext()); pstm.executeupdate(); }
your error happen because forgot close string ''
check query , see :
+"', " + jtextfield_supplieraddress.gettext() + "' //--^--------------------------------------------^
Comments
Post a Comment