Java 如何在数据库中插入Json对象
我有一个类似于这种类型的json对象Java 如何在数据库中插入Json对象,java,php,mysql,arrays,json,Java,Php,Mysql,Arrays,Json,我有一个类似于这种类型的json对象 [{"productName":"ddsf","productPrice":234234,"quantity":23}][{"productName":"ddsf","productPrice":234234,"quantity":23}]Array ( [0] => Array ( [productName] => ddsf [productPrice] => 234
[{"productName":"ddsf","productPrice":234234,"quantity":23}][{"productName":"ddsf","productPrice":234234,"quantity":23}]Array
(
[0] => Array
(
[productName] => ddsf
[productPrice] => 234234
[quantity] => 23
)
)
如何一次插入此对象数组。我的数据库列为id、productname、productprice、quantity,因此我希望一次插入多行数据库连接示例:
//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$hostname="localhost";
$database="dbname";
$username="username";
$password="password";
//DO NOT EDIT BELOW THIS LINE
$link = mysql_connect($hostname, $username, $password);
mysql_select_db($database) or die('Could not select database');
数据库中插入数组的示例:
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
//Database Connection
require_once 'db.php';
/* insert data into DB */
foreach($obj as $item) {
mysql_query("INSERT INTO `database product`.`table name` (productName, productPrice, quantity)
VALUES ('".$item['productName']."', '".$item['productPrice']."', '".$item['quantity']."')");
}
//database connection close
mysql_close($con);
//}
您可以使用提供JSON类型的PostgresSQL数据库。
因此,您可以直接将JSON字符串插入到JSON类型的列中 请自己尝试并问一个更具体的问题:创建一个
Product
对象,使用您拥有的json对象设置其属性,然后将其保存在数据库中。花点精力研究一下会告诉你如何做这些事情。你的标签包括Java和php。。。不清楚你在问什么。请阅读