-2
如何將JSON文件導入到MySQL數據庫。如果我能得到一個存儲過程或者一個能夠自動將所需文件推送到MySQL DB的java程序,那將是一件好事。使用Java將json文件導入到mysql數據庫?
如何將JSON文件導入到MySQL數據庫。如果我能得到一個存儲過程或者一個能夠自動將所需文件推送到MySQL DB的java程序,那將是一件好事。使用Java將json文件導入到mysql數據庫?
假設你的JSON看起來是這樣的:
{
"employee":
{
"id": "100",
"name": "ABC",
"address": "New York"
}
}
我們可以使用JSON解析器解析JSON。在我的代碼一看:
public int insertJSONtoDB() throws Exception {
int status = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "root");
PreparedStatement preparedStatement = con.prepareStatement("insert into employee values (?, ?, ?)");
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("c.\\employee.json"));
JSONObject jsonObject = (JSONObject) obj;
String id = (String) jsonObject.get("id"); // from JSON tag
preparedStatement.setString(1, id); // to the Database table
String name = (String) itemize.get("name");
preparedStatement.setString(2, name);
String address = (String) itemize.get("address");
preparedStatement.setString(3, address);
status = preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (con != null) {
con.close();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
return status;
}
是的,那麼我們就可以去得到[使用Java從JSON文件中導入數據到MySQL數據庫]的啤酒 –
可能的複製(https://stackoverflow.com/questions/ 42838994 /導入數據從 - JSON-文件到MySQL數據庫-使用-java的) – Companjo