Advertisement
Guest User

Untitled

a guest
Feb 16th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package bigdatamangementcs1;
  7.  
  8. import java.io.BufferedWriter;
  9. import java.io.File;
  10. import java.io.FileWriter;
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.ResultSet;
  14. import java.sql.Statement;
  15.  
  16. /**
  17. *
  18. * @author cyl1
  19. */
  20. public class getmovies2 {
  21.  
  22. public getmovies2() {
  23. int first = 0;
  24. int tempid = 0;
  25.  
  26. try {
  27. Class.forName("com.mysql.jdbc.Driver").newInstance();
  28. Connection conn = DriverManager.getConnection("jdbc:mysql://mysql-server-1.macs.hw.ac.uk/movielens?user=cyl1&password=abccyl1354");
  29. Statement st = conn.createStatement();
  30. String query = "SELECT movies.id, movies.title, movies.release_date, "
  31. + "movies.video, movies.IMDBURL, ratings.user, ratings.rating, "
  32. + "ratings.timestamp FROM movies INNER JOIN ratings ON movies.id = ratings.movie";
  33. ResultSet rs = st.executeQuery(query);
  34.  
  35. String json = "";
  36.  
  37. File file = new File("test.json");
  38. if (!file.exists()){
  39. file.createNewFile();
  40. } else {
  41. file.delete();
  42. file.createNewFile();
  43. }
  44.  
  45. FileWriter fw = new FileWriter(file.getAbsoluteFile());
  46. BufferedWriter bw = new BufferedWriter(fw);
  47.  
  48.  
  49. while(rs.next()){
  50.  
  51.  
  52. int id = rs.getInt("id");
  53. String title = "\""+rs.getString("title")+"\"";
  54. String release_date ="\""+ rs.getString("release_date")+"\"";
  55. String video = "\""+rs.getString("video")+"\"";
  56. String IMDBURL = "\""+rs.getString("IMDBURL")+"\"";
  57. int user = rs.getInt("user");
  58. int rating = rs.getInt("rating");
  59. String timestamp = "\""+rs.getString("timestamp")+"\"";
  60. //System.out.format("%s %s\n", id,genre);
  61.  
  62.  
  63.  
  64. if (first == 0) {
  65. json = "{\"id\" :"+id+", "
  66. + "\"title\" :"+title+", "
  67. + "\"release_date\" :"+release_date+", "
  68. + "\"video\" :"+video+", "
  69. + "\"IMDBURL\" :"+IMDBURL+", "
  70. + "ratings :["+"{\"user\" :"+user+", "
  71. + "\"rating\" :"+rating+", "
  72. + "\"timestamp\" :"+timestamp+"}]}";
  73. first = 1;
  74. tempid = id;
  75. continue;
  76. } else if (id==tempid & first==1) {
  77. json = json.replace("]}", ", ");
  78. json += "{\"user\" :"+user+", "
  79. + "\"rating\" :"+rating+", "
  80. + "\"timestamp\" :"+timestamp+"}]}";
  81. } else if (!(id ==tempid) & first==1) {
  82. json = "\n{\"id\" :"+id+", "
  83. + "\"title\" :"+title+", "
  84. + "\"release_date\" :"+release_date+", "
  85. + "\"video\" :"+video+", "
  86. + "\"IMDBURL\" :"+IMDBURL+", "
  87. + "ratings :["+"{\"user\" :"+user+", "
  88. + "\"rating\" :"+rating+", "
  89. + "\"timestamp\" :"+timestamp+"}]}";
  90. }
  91.  
  92.  
  93. bw.write(json);
  94. tempid = id;
  95.  
  96.  
  97.  
  98. }
  99. bw.close();
  100. st.close();
  101. conn.close();
  102. System.out.println("test.json saved");
  103. } catch (Exception e) {
  104. System.out.println("ERROR!! " +e);
  105. }
  106. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement