我有一個特殊的情況,我無法使用prepareStatement
。無法使用SQL中的prepareStatement
我正在寫一個更新函數在Java中,我需要更新一組行的字段。我的問題是我不知道有多少字段需要更新。我從JSONArray
動態確定這個。在這種情況下,我可以如何使用prepareStatement
來制定SQL查詢。至於prepareStatement
該查詢是預定義的,並且假設稍後添加值。我的非功能性的示例代碼是在這裏:
DB:使用MySQL MySQL驅動jar文件
public static int updateUser(String user) throws SQLException, JSONException{
JSONObject jobj= new JSONObject(user) ;
String pQuery= " UPDATE Presence SET "+(jobj.has("lat")?" lat =?,":",")+(jobj.has("lng")?" lng = ?,":",")+(jobj.has("description")? "description = ?,":",")+(jobj.has("gcm_id")?" gcm_id = ?,":",")+" WHERE id = ?";
pst = con.prepareStatement(pQuery);
pst.setDouble(1, jobj.getDouble("lat"));
pst.setDouble(2, jobj.getDouble("lng"));
pst.setString(3, jobj.getJSONObject("description").toString());
pst.setString(4, jobj.get("id").toString());
if(jobj.has("gcm_id"))
pst.setString(5, jobj.getString("gcm_id"));
int rs = pst.executeUpdate();
return rs;
}
這是什麼樣的dbms,即'con'有什麼類型?請添加您正在使用的庫/ API。 – runDOSrun
編輯並添加了信息 –