0

当我尝试更新如下表记录时出现错误

方法 - 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 更新表格。有人可以解决我的问题吗?谢谢。

4

3 回答 3

1

您没有在 SQL 中包含带有“”的 JSON,请使用以下内容。您还需要转义 JSON 中的 " 标记并在 String 声明中添加分号

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);
于 2013-03-27T13:45:53.913 回答
1

有一些缺失的引号:'"+jsonText+"'

st3.executeUpdate("UPDATE comment SET c_wt ="+c_newwt+",c_Jsonwt ='"+jsonText+"' WHERE c_id="+c_id);
于 2013-03-27T13:46:17.600 回答
0

I would suggest something like

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

It looks like a lot of quotes need escaping.

于 2013-03-27T13:48:18.847 回答