Results 1 to 5 of 5
  1. #1
    Dcalladi is offline Member
    Join Date
    Oct 2011
    Posts
    27
    Rep Power
    0

    Default Something Wrong With My SQL Statement

    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());
            }
    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?

    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

  2. #2
    sasi040 is offline Member
    Join Date
    Mar 2011
    Location
    Hyderabad,Andhra Pradesh, India
    Posts
    2
    Rep Power
    0

    Default 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+"";

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Something Wrong With My SQL Statement

    Or use a PreparedStatement, illustrated in Oracle's Tutorial.

  4. #4
    Anveshan is offline Member
    Join Date
    May 2011
    Posts
    22
    Rep Power
    0

    Default Re: Something Wrong With My SQL Statement

    try using single quotes....

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Something Wrong With My SQL Statement

    Quote Originally Posted by Anveshan View Post
    try using single quotes....
    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

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. Need Help For doing if else statement
    By juvenchan in forum New To Java
    Replies: 11
    Last Post: 02-27-2011, 05:22 AM
  3. method return statement is wrong
    By ftrengnr in forum New To Java
    Replies: 1
    Last Post: 10-19-2010, 04:15 AM
  4. for statement help
    By helpisontheway in forum New To Java
    Replies: 5
    Last Post: 11-14-2009, 04:14 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •