Java - 如何将JSON文档解析为数据库

时间:2013-12-08 03:17:53

标签: java mysql json parsing

我有一个包含产品信息的JSON文档,我想解析JSON文档并将其放入数据库中。

示例JSON文档:

{
"itemize": {
   "pr": "2583",

       "n": "Chocolate donut",

       "yst": "A beautiful, premium chocolate donut"

       "wh": 2.99

}

这是我到目前为止的代码:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class q1 {
public static void addProduct()
{
    JSONParser parser=new JSONParser();

    try{

        Object obj = parser.parse(new FileReader("c.\\itemize.json"));
        JSONObject jsonObject = (JSONObject) obj;

        String pr = (String) jsonObject.get("Pr");
        //Put pr into database

        String n = (String) jsonObject.get("n");
        //Put n into database

        String yst = (String) jsonObject.get("yst");
        //Put yst into database

        String wh = (String) jsonObject.get("wh");
        //Put wh into database

    }
}
}

数据库在MySQL中,并且已经包含了所有这些列。我只需要用将把字符串放入数据库的行替换java代码中的注释行。这就是数据库的样子:

Pr VARCHAR(30) NOT NULL,
n VARCHAR(30) NOT NULL,
yst VARCHAR(30) NOT NULL,
wh VARCHAR(30) NOT NULL,
Primary Key (Product_ID));

3 个答案:

答案 0 :(得分:7)

用于访问数据库的Java接口是Java数据库连接(JDBC)。使用JDBC,您可以创建与数据库的连接,发出数据库查询和更新并接收结果。尝试以下代码

  private Connection connect = null;
  PreparedStatement preparedStatement = null;

 public int save() throws Exception {
   int status = 0;  
    try {
      // Load the MySQL driver, each DB has its own driver
      Class.forName("com.mysql.jdbc.Driver");

      // DB connection setup 
      connect = DriverManager.getConnection("jdbc:mysql://dbhost/database?" + "user=sqluser&password=sqluserpw");

      // PreparedStatements 
      preparedStatement = connect
          .prepareStatement("insert into  Table_Name values (?, ?, ?, ? )");

      Object obj = parser.parse(new FileReader("c.\\itemize.json"));
      JSONObject jsonObject = (JSONObject) obj;

      String pr = (String) jsonObject.get("Pr");
      // Parameters start with 1
      preparedStatement.setString(1, pr);

      String n = (String) itemize.get("n");
      preparedStatement.setString(2, n);

      String yst = (String) jsonObject.get("yst");
      preparedStatement.setString(3, yst);

      String wh = (String) itemize.get("wh");
      preparedStatement.setString(4, wh);

      status = preparedStatement.executeUpdate();

    } catch (Exception e) {
      throw e;
    } finally {
      try {
          if (connect != null) {
             connect.close();
           }

         } catch (Exception e) {

         }
    }
    return status;
  }

答案 1 :(得分:2)

这样做。

    Object obj = parser.parse(new FileReader("c.\\itemize.json"));
    JSONObject jsonObject = (JSONObject ) obj;
    JSONObject   itemize = (JSONObject) jsonObject.get("itemize");
    String pr = (String) itemize.get("Pr");
    String n = (String) itemize.get("n");
    String yst = (String) itemize.get("yst");
    String wh = (String) itemize.get("wh");

答案 2 :(得分:1)

首先,我建议您在代码和数据库中使用限定名称作为变量。    在您的所有代码之后,使用这些行来创建插入

  //use DriverManager to getConnection for your mySQL
    conObj = getConnection();
    String preQueryStatement = "INSERT  INTO  <TABLENAME>  VALUES  (?,?,?,?)";
    pStmnt = conObj.prepareStatement(preQueryStatement);
    pStmnt.setString(1, Pr );
    pStmnt.setString(2, n);
    pStmnt.setString(3, yst );
    pStmnt.setInt(4, wh );
 // execute insert SQL stetement
    preparedStatement .executeUpdate();