当我尝试更新如下所示的表记录时,出现错误
方法 - 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 更新表格。有人可以解决我的问题吗?谢谢。
最佳答案
您未在 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);
关于java - 使用 Java 更新 Mysql 表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15660374/