Search code examples
javaoracle-databaseprepared-statementsqlexception

how to use prepareStatement to update data, field and value both need be set


I use prepareStatement() to update data, for several columns need to update, I wrote one procedure like this:

public boolean editSocre(String field, String newValue) 
{
... 

   updateSql = "update score set ? = ? where emp_id = ?";
   pstmt3 = conn.prepareStatement(updateSql);
   pstmt3.setString(1, field);
   pstmt3.setString(2, newValue);
   pstmt3.setString(3, userid);                 
   int row = pstmt3.executeUpdate();
   if (row <= 0) 
   {
      return result;
   }

...

}

java.sql.SQLException: ORA-01747: invalid user.table.column, table.column, or column specification

How to modify, thanks!!


Solution

  • public boolean editSocre(String field, String newValue) {
    
    ... 
    
    updateSql = "update score set "+field+" = ? where emp_id = ?";
    
    pstmt3 = conn.prepareStatement(updateSql);
    
    pstmt3.setString(1, newValue);
    
    pstmt3.setString(2, userid);          
    
    int row = pstmt3.executeUpdate();
    
    if (row <= 0) {
    
      return result;
    
    }
    

    ...