Guest User

getmovies

a guest
Feb 16th, 2016
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 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 getmovies {
  21. public getmovies() {
  22. try {
  23. //connects to database using jdbc driver
  24. Class.forName("com.mysql.jdbc.Driver").newInstance();
  25. Connection conn = DriverManager.getConnection("jdbc:mysql://mysql-server-1.macs.hw.ac.uk/movielens?user=cyl1&password=abccyl1354");
  26. Statement st = conn.createStatement();
  27.  
  28. //query for getting data from mysql
  29. String query = "SELECT * FROM movies";
  30. ResultSet rs = st.executeQuery(query);
  31.  
  32. //string for data for json
  33. String json = "";
  34.  
  35. //creating json file
  36. File file = new File("movies.json");
  37.  
  38. //creates new file if file doesnt exists
  39. if (!file.exists()){
  40. file.createNewFile();
  41. }
  42.  
  43. //Writing code for json
  44. FileWriter fw = new FileWriter(file.getAbsoluteFile());
  45. BufferedWriter bw = new BufferedWriter(fw);
  46. String movies="";
  47. //loop
  48. while(rs.next()){
  49. int id = rs.getInt("id");
  50. String title = rs.getString("title");
  51. String rd = rs.getString("release_date");
  52. String video = rs.getString("video");
  53. String IMDBURL = rs.getString("IMDBURL");
  54.  
  55. //System.out.format("%s %s\n", id,genre);
  56. json = "{\"_id\" :"+id+", "
  57. + "\"title\" :\""+title+"\", "
  58. +"\"release_date\" :\""+rd+"\", "
  59. +"\"video\" : \""+video+"\" , "
  60. +"\"IMDBURL\" : \""+IMDBURL+"\"}\n";
  61. bw.write(json);
  62.  
  63. }
  64. bw.close();
  65. st.close();
  66. conn.close();
  67. System.out.println("movie.json saved");
  68. } catch (Exception e) {
  69. System.out.println("ERROR!! " +e);
  70. }
  71. }
  72. }
Add Comment
Please, Sign In to add comment