Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSON对象中的引号无法写入数据库_Java_Javascript_Json - Fatal编程技术网

Java JSON对象中的引号无法写入数据库

Java JSON对象中的引号无法写入数据库,java,javascript,json,Java,Javascript,Json,我目前有一个案例,我想在银行中记录一个JSON对象,以便在需要时取回它 此对象由JavaScript对象JSON操作 但我遇到了一个我从未遇到过的问题,当我在一个字符串对象中记录引号并试图保存银行引号时,无法避免字符串再次变为json对象 实际上: 发送的JSON字符串: {"footer":"this is a double quote \"","header":""} 写入数据库的字符串: {"footer":"this is a double quote "","header":""

我目前有一个案例,我想在银行中记录一个JSON对象,以便在需要时取回它 此对象由JavaScript对象JSON操作

但我遇到了一个我从未遇到过的问题,当我在一个字符串对象中记录引号并试图保存银行引号时,无法避免字符串再次变为json对象

实际上: 发送的JSON字符串:

 {"footer":"this is a double quote \"","header":""}
写入数据库的字符串:

 {"footer":"this is a double quote "","header":""}
因此,返回JSON时会出现错误

在守则中:

// My SQL
String sql = "UPDATE table SET val='%s' WHERE 1";

// The JSON string to be recorded ( This value comes within a variable (not literal ) )
String jsonString = // {"footer":"this is a double quote \"","header":""};

// Create statement in JDBC
con.prepareStatement(String.format(sql, jsonString)).execute();

// The following SQL statement generates the toString() ;
UPDATE table SET val='{"footer":"this is a double quote \"","header":""}' WHERE 1

// In the database the following String is recorded
{"footer":"this is a double quote "","header":""}
我的桌子是这样布置的: utf8 utf8\u概述\u ci

为什么会发生这种情况?我能做些什么来解决这个问题


非常感谢。

您需要使用事先准备好的声明:

示例代码段:

PreparedStatement statement = null;
String sql = "UPDATE table SET val=? WHERE 1";

statement = con.prepareStatement(sql);
statement.setString(1, jsonString);
statement.executeUpdate();
con.commit();