Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Query

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whats wrong with my String command.I cant figure out my quotes>i keep getting a syntax error


public int amendProduct(int id, String name, double price) throws DaoException
{
Connection con = null;
Statement stmt = null;
int rows = 0;
try
{
con = getConnection();
stmt = con.createStatement();
String command = "UPDATE Product SET item = " + id + " WHERE id = " + id + " name = '" + name + "'" + " price = " + price ;

System.out.println(command);
rows = stmt.executeUpdate(command);
}

catch(SQLException e)
{
throw new DaoException("amendProduct(): " + e.getMessage() );
}

finally
{
try
{
if (stmt != null)
stmt.close() ;
if (con !=null)
freeConnection(con);
}

catch(SQLException e)
{
throw new DaoException("amendProduct(): " + e.getMessage() );
}
}

System.out.println(rows);
return rows;
}
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like you need to add some ANDs to your WHERE clause.
"UPDATE Product SET item = " + id + "
WHERE id = " + id + " AND name = " + name + " AND price = " + price
 
Sheriff
Posts: 67601
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Explore the PreparedStatement. Your life will improve.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, prepared statements make life a breeze! Special characters will never give you any problems, they put less strain on the DB, they are just altogether a wonderful thing!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic