Results 1 to 5 of 5
- 10-21-2011, 12:06 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 27
- Rep Power
- 0
Something Wrong With My SQL Statement
i am using this code to try and update a SQL database but get an error. all the variables are correct at i used breakpoints stepping through to check them. i am getting this error can anyone help?Java Code:try { DriverManager.registerDriver(new com.mysql.jdbc.Driver()); Connection conn = DriverManager.getConnection("jdbc:mysql://samp.inf.brad.ac.uk/dcalladi", "dcalladi", "Dannys21"); conn.createStatement().executeUpdate("UPDATE Test_items SET Barcode =" + changeBarcode + ", Name =" + changeName + " , Cost =" + changePrice + " where Id ="+id+""); } catch (Exception ex) { System.err.println("SQLException: " + ex.getMessage()); }
SQLException: 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 'Chilli Sauce, Cost =1.75 where Id =1' at line 1
- 10-21-2011, 07:54 AM #2
Member
- Join Date
- Mar 2011
- Location
- Hyderabad,Andhra Pradesh, India
- Posts
- 2
- Rep Power
- 0
Re: Something Wrong With My SQL Statement
hi,
try this query
String q = "UPDATE Test_items SET Barcode ='"+ changeBarcode +"', Name ='"+ changeName +"',Cost =" + changePrice +"where Id ="+id+"";
- 10-21-2011, 09:12 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Something Wrong With My SQL Statement
Or use a PreparedStatement, illustrated in Oracle's Tutorial.
- 10-21-2011, 09:44 AM #4
Member
- Join Date
- May 2011
- Posts
- 22
- Rep Power
- 0
Re: Something Wrong With My SQL Statement
try using single quotes....
- 10-21-2011, 10:52 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Something Wrong With My SQL Statement
That's what sasi was getting at (#2).
It looks like the sort of sql statement that is going to be executed often as item details change. And things like changeName could be given evil values. So why not a PreparedStatement?
Similar Threads
-
Need Help For doing if else statement
By juvenchan in forum New To JavaReplies: 11Last Post: 02-27-2011, 05:22 AM -
method return statement is wrong
By ftrengnr in forum New To JavaReplies: 1Last Post: 10-19-2010, 04:15 AM -
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 2Last Post: 11-22-2009, 03:48 PM -
for statement help
By helpisontheway in forum New To JavaReplies: 5Last Post: 11-14-2009, 04:14 PM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks