Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query

 
Roberto Favaro
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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;
}
 
Edwin Keeton
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Explore the PreparedStatement. Your life will improve.
 
Bob Rocks
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic