{ "F_NUMBER": "", "VER": "", "BOM_DATA": [{ "COMPS": "", "burnDesc": "", }], "BOM_STATUS": "", "itemLines": [{ "item_number": "3.7.01.10.00212", "lifecyclephase": "Development", "rev_number": "03" }] }, "ECN_DATA": { "ecnBefore": "", "ecnAfter": "", "ecnSwitch": "", } "zxbz": "" }解析分别插入oracle表中java代码
时间: 2023-08-11 12:03:03 浏览: 102
kbar_snippets:Kbar按钮友好的ExtendScript片段的随机分类
假设你已经连接到了Oracle数据库,有以下两个表需要插入数据:
- BOM_DATA表:字段有F_NUMBER、VER、COMPS、burnDesc、BOM_STATUS;
- ITEM_LINES表:字段有ITEM_NUMBER、LIFECYCLEPHASE、REV_NUMBER;
- ECN_DATA表:字段有ECN_BEFORE、ECN_AFTER、ECN_SWITCH;
- ZX_BZ表:字段有ZX_BZ_DATA。
那么可以使用以下Java代码将JSON字符串解析后插入各自的表中:
```java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class OracleInsertDemo {
// 获取数据库连接
private Connection conn = getConnection();
// 解析JSON字符串,插入各自的表中
public void insertData(String jsonStr) throws SQLException {
JSONObject jsonObj = JSONObject.parseObject(jsonStr);
insertBomData(jsonObj.getJSONArray("BOM_DATA"));
insertItemLines(jsonObj.getJSONArray("itemLines"));
insertEcnData(jsonObj.getJSONObject("ECN_DATA"));
insertZxBzData(jsonObj.getString("zxbz"));
}
// 插入BOM_DATA表
private void insertBomData(JSONArray bomDataArr) throws SQLException {
String sql = "INSERT INTO BOM_DATA(F_NUMBER, VER, COMPS, burnDesc, BOM_STATUS) VALUES(?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
for (int i = 0; i < bomDataArr.size(); i++) {
JSONObject bomDataObj = bomDataArr.getJSONObject(i);
pstmt.setString(1, bomDataObj.getString("F_NUMBER"));
pstmt.setString(2, bomDataObj.getString("VER"));
pstmt.setString(3, bomDataObj.getString("COMPS"));
pstmt.setString(4, bomDataObj.getString("burnDesc"));
pstmt.setString(5, bomDataObj.getString("BOM_STATUS"));
pstmt.addBatch();
}
pstmt.executeBatch();
pstmt.close();
}
// 插入ITEM_LINES表
private void insertItemLines(JSONArray itemLinesArr) throws SQLException {
String sql = "INSERT INTO ITEM_LINES(ITEM_NUMBER, LIFECYCLEPHASE, REV_NUMBER) VALUES(?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
for (int i = 0; i < itemLinesArr.size(); i++) {
JSONObject itemLinesObj = itemLinesArr.getJSONObject(i);
pstmt.setString(1, itemLinesObj.getString("item_number"));
pstmt.setString(2, itemLinesObj.getString("lifecyclephase"));
pstmt.setString(3, itemLinesObj.getString("rev_number"));
pstmt.addBatch();
}
pstmt.executeBatch();
pstmt.close();
}
// 插入ECN_DATA表
private void insertEcnData(JSONObject ecnDataObj) throws SQLException {
String sql = "INSERT INTO ECN_DATA(ECN_BEFORE, ECN_AFTER, ECN_SWITCH) VALUES(?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, ecnDataObj.getString("ecnBefore"));
pstmt.setString(2, ecnDataObj.getString("ecnAfter"));
pstmt.setString(3, ecnDataObj.getString("ecnSwitch"));
pstmt.executeUpdate();
pstmt.close();
}
// 插入ZX_BZ表
private void insertZxBzData(String zxbzData) throws SQLException {
String sql = "INSERT INTO ZX_BZ(ZX_BZ_DATA) VALUES(?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, zxbzData);
pstmt.executeUpdate();
pstmt.close();
}
// 获取数据库连接
private Connection getConnection() {
// TODO:获取数据库连接的代码
}
}
```
需要注意的是,这里使用了阿里巴巴的fastjson库来解析JSON字符串。如果你没有使用过该库,需要先将其添加到项目的依赖中。另外,这里没有考虑到异常处理等情况,需要你自行添加。
阅读全文