Sql Error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Sql Error

java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from, phone, address, type, del_flag) values('sk', 'OTHER_STATES', '01372305', '' at line 1 --> Here myQuery @Override public void save(Customers customers) { String addNew = "insert into customers(name, from, phone, address, type, del_flag) values(?, ?, ?, ?, ?, ?)"; String update = "update customers set name = ? ,from = ? ,phone = ? ,address = ? ,type = ? ,del_flag = ? where id = ?"; boolean isUpdate = customers.getId() > 0; try(Connection con = Connection_Manager.getConnection(); PreparedStatement statement = con.prepareStatement(isUpdate ? update : addNew)) { statement.setString(1, customers.getName()); statement.setString(2, customers.getRegion().name());//I created this one as enum in Customers class statement.setString(3, customers.getPhone()); statement.setString(4, customers.getAddress()); statement.setString(5, customers.getTypeOfCustomer().name());//also that one statement.setBoolean(6, customers.isDelete()); if(isUpdate) { statement.setInt(7, customers.getId()); } statement.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } }

27th Mar 2020, 6:58 PM
Pyae Sone Kyaw
Pyae Sone Kyaw - avatar
2 Answers
+ 3
Paye Sone Kyaw hi 1) I see one main problem with your queries: FROM is a reserved keyword in SQL So I would advise you to change the column name 'from' to anything else in your table definition should avoid errors in your queries. 2) According to the error you get, it seems it's the insert command that makes the error, so in case you can't change the field name, try the insert without mentioning column names like this: INSERT INTO Customers VALUES(..,.,..) Link to SQL insert lesson: https://www.sololearn.com/learn/SQL/1868/ 3) Best practices indicate to write all SQL commands in uppercase. If you do so it becomes obvious which words are reserved and which concern your own data. Ex.: SELECT * FROM Customers WHERE firstname='Pyae' Get back to me with your result please. Take care of yourself Best, Tom P.S.: SQL Keywords on W3schools https://www.w3schools.com/sql/sql_ref_keywords.asp
1st Apr 2020, 9:54 PM
Tom BWDEV 🇺🇦
Tom BWDEV 🇺🇦 - avatar
+ 1
Thanks Tom BWDEV.
2nd Apr 2020, 3:57 PM
Pyae Sone Kyaw
Pyae Sone Kyaw - avatar