2

prepareStatement()用来更新数据,有几列需要更新,我写了一个这样的程序:

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: 无效的 user.table.column、table.column 或列规范

怎么修改,谢谢!!

4

2 回答 2

3
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;

}

...

于 2012-05-28T03:35:53.307 回答
0

正如其他答案/评论已经说过的那样:PreparedStatement 无法做到这一点。如果要更新的行确实需要是动态的,则可以使用可更新的结果集:

PreparedStatement stmt = connection.prepareStatement("SELECT * FROM score WHERE emp_id = ?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
stmt.setString(1, userId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) { // or maybe while rs.next()
    rs.updateString(field, newValue);
    rs.updateRow();
}
于 2012-05-28T13:19:26.033 回答