2013-03-27 51 views
0

當我嘗試更新像下面我得到一個錯誤更新使用Java

方法的表格記錄一個MySQL錶行 - 1:

String jsonText = "{"id":100,"nickname":"yash","name":"Rahul"}" 
Statement st3 = con.createStatement(); 
st3.executeUpdate("UPDATE comment SET c_wt ="+c_newwt+",c_Jsonwt ="+jsonText+" WHERE c_id="+c_id); 

,當我這樣做它獲取更新

方法 - 2:

Statement st3 = con.createStatement(); 
st3.executeUpdate("UPDATE comment SET c_wt ="+c_newwt+",c_Jsonwt ="+"'{"id":100,"nickname":"yash","name":"Rahul"}'"+" WHERE c_id="+c_id); 

我需要使用方法1.任何一個可以請發表更新olve我的問題?謝謝。

+2

「*當我嘗試更新像下面的表格記錄ig等錯誤*「。那麼,請包括完整的堆棧跟蹤! – mre 2013-03-27 13:44:59

+2

您的字符串在第一種方法中格式不正確。 – 2013-03-27 13:45:01

+0

這可能聽起來很愚蠢,但是你打算在第一行的末尾加上分號嗎?另外如上所述,它格式不正確。 Java並不是我的特長。 – 2013-03-27 13:45:44

回答

1

您不在SQL中包含帶「」的JSON,請使用以下內容。您還需要逃避在JSON的「標記和一個分號添加到字符串聲明

String jsonText = "{\"id\":100,\"nickname\":\"yash\",\"name\":\"Rahul\"}"; 
Statement st3 = con.createStatement(); 
st3.executeUpdate("UPDATE comment SET c_wt ="+c_newwt+",c_Jsonwt ='"+jsonText+"' WHERE c_id="+c_id); 
+0

那個力氣! – yAsH 2013-03-27 13:49:00

+0

你是指「dint work」 - 你有錯誤嗎? 「 – david99world 2013-03-27 13:49:59

+0

」「+ jsonText +」「 - >這個工作,但''+ jsonText +''工作:) – yAsH 2013-03-27 13:55:14

1

有一些缺失報價:'"+jsonText+"'

st3.executeUpdate("UPDATE comment SET c_wt ="+c_newwt+",c_Jsonwt ='"+jsonText+"' WHERE c_id="+c_id); 
0

我建議像

String jsonText = "{\"id\":100,\"nickname\":\"yash\",\"name\":\"Rahul\"}"; 
Statement st3 = con.createStatement(); 
st3.executeUpdate("UPDATE comment SET c_wt ="+c_newwt+",c_Jsonwt =\""+jsonText+"\" WHERE c_id="+c_id); 

看起來好像很多引號需要轉義