db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bryan Pendleton <bpendle...@amberpoint.com>
Subject Re: Problems with updating database
Date Tue, 01 Sep 2009 13:40:10 GMT
> database) and this attribute's name along with the new value of the
> attribute and the actor id will be passed to an "updateDatabase"function.
> But how do i write a statement or preparedStatement when I don't know
> beforehand what column is supposed to get changed?
> If I knew the attribute I would write
> update actor set attributeName = newValue where actor_id = ?

Seems like you can write something like:

   public void updateDatabase(String aName, String aValue, int actorId)
   throws SQLException
   {
     PreparedStatement ps = conn.prepareStatement(
         "update actor set " + aName + "=? where actor_id = ?");
     ps.setString(1, aValue);
     ps.setInt(2, actorId);
     ps.executeUpdate();
     ps.close();
     conn.commit();
   }

Note that by using a prepared statement and a '?' for the attribute
value, you don't have to hassle with quotation marks around the value.

thanks,

bryan



Mime
View raw message