Advertisement
Guest User

Untitled

a guest
Jul 14th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. {
  2. "employee":
  3. {
  4. "id": "100",
  5.  
  6. "name": "ABC",
  7.  
  8. "address": "New York"
  9. }
  10. }
  11.  
  12. public int insertJSONtoDB() throws Exception {
  13. int status = 0;
  14. try {
  15. Class.forName("com.mysql.jdbc.Driver");
  16. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "root");
  17. PreparedStatement preparedStatement = con.prepareStatement("insert into employee values ( ?, ?, ? )");
  18. JSONParser parser = new JSONParser();
  19. Object obj = parser.parse(new FileReader("c.\employee.json"));
  20. JSONObject jsonObject = (JSONObject) obj;
  21.  
  22. String id = (String) jsonObject.get("id"); // from JSON tag
  23. preparedStatement.setString(1, "id"); // to the Database table
  24.  
  25. String name = (String) itemize.get("name");
  26. preparedStatement.setString(2, "name");
  27.  
  28. String address = (String) itemize.get("address");
  29. preparedStatement.setString(3, "address");
  30.  
  31. status = preparedStatement.executeUpdate();
  32.  
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. } finally {
  36. try {
  37. if (con != null) {
  38. con.close();
  39. }
  40.  
  41. } catch (Exception e1) {
  42. e1.printStackTrace();
  43. }
  44. }
  45. return status;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement