2013-12-08 39 views
3

我有一個包含產品信息的JSON文檔,我想解析JSON文檔並將其放入數據庫。Java - 如何將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)); 
+1

使用'java.sql.PreparedStatement'。 –

+0

爲了使代碼有用,您確實需要添加第二條記錄,所以某些代碼會讓所有標籤都有所不同,以供您理解,並可能會改變您的編碼方式。 –

回答

6

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; 
    } 
+0

什麼是1,2,3,4 ..是某種索引或列屬性? – jsroyal

+0

它的佔位符(?在插入準備語句)索引。 https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html – Shamse

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