Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.79 KB | None | 0 0
  1. import javax.annotation.Resource;
  2. import javax.sql.DataSource;
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.HashMap;
  9. import java.util.HashSet;
  10. import java.util.Map;
  11. import java.util.Iterator;
  12. import java.util.Set;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15.  
  16. import java.sql.*;
  17. import javax.xml.parsers.ParserConfigurationException;
  18. import javax.xml.parsers.SAXParser;
  19. import javax.xml.parsers.SAXParserFactory;
  20.  
  21. import org.xml.sax.Attributes;
  22. import org.xml.sax.SAXException;
  23.  
  24. import org.xml.sax.helpers.DefaultHandler;
  25.  
  26. public class XML_Parse extends DefaultHandler{
  27. // Create a dataSource which registered in web.xml
  28.  
  29. static HashMap<String, Stuff> movieInfo;
  30. static HashMap<String, Stuff> actorInfo;
  31. // static int count = 0;
  32. private String tempVal;
  33. private String curMovie;
  34. private Stuff tempMovie;
  35. private Stuff tempActor;
  36. private String parsing;
  37. private String curDir;
  38. private String temp;
  39. private String mid;
  40.  
  41. static {
  42. try { Class.forName("com.mysql.jdbc.Driver"); }
  43. catch(ClassNotFoundException ex) {
  44. System.err.println("Driver not found: " + ex.getMessage());
  45. }
  46. };
  47.  
  48. public XML_Parse() {
  49. movieInfo = new HashMap<String, Stuff> ();
  50. actorInfo = new HashMap<String, Stuff> ();
  51. }
  52.  
  53. public void parseCasts() {
  54. SAXParserFactory spf = SAXParserFactory.newInstance();
  55. parsing = "p";
  56. try {
  57.  
  58. //get a new instance of parser
  59. SAXParser sp = spf.newSAXParser();
  60.  
  61. //parse the file and also register this class for call backs
  62. sp.parse("casts124.xml", this);
  63.  
  64. } catch (SAXException se) {
  65. se.printStackTrace();
  66. } catch (ParserConfigurationException pce) {
  67. pce.printStackTrace();
  68. } catch (IOException ie) {
  69. ie.printStackTrace();
  70. }
  71. }
  72.  
  73. public void parseMovies() {
  74. SAXParserFactory spf = SAXParserFactory.newInstance();
  75. parsing = "m";
  76. try {
  77.  
  78. //get a new instance of parser
  79. SAXParser sp = spf.newSAXParser();
  80.  
  81. //parse the file and also register this class for call backs
  82. sp.parse("mains243.xml", this);
  83.  
  84. } catch (SAXException se) {
  85. se.printStackTrace();
  86. } catch (ParserConfigurationException pce) {
  87. pce.printStackTrace();
  88. } catch (IOException ie) {
  89. ie.printStackTrace();
  90. }
  91. }
  92.  
  93. public void parseActors() {
  94. SAXParserFactory spf = SAXParserFactory.newInstance();
  95. parsing = "a";
  96. try {
  97.  
  98. //get a new instance of parser
  99. SAXParser sp = spf.newSAXParser();
  100.  
  101. //parse the file and also register this class for call backs
  102. sp.parse("actors63.xml", this);
  103.  
  104. } catch (SAXException se) {
  105. se.printStackTrace();
  106. } catch (ParserConfigurationException pce) {
  107. pce.printStackTrace();
  108. } catch (IOException ie) {
  109. ie.printStackTrace();
  110. }
  111. }
  112.  
  113. public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
  114. //reset
  115. tempVal = "";
  116. if (parsing == "p") {
  117. if (qName.equalsIgnoreCase("filmc"))
  118. tempMovie = new Stuff();
  119. if (qName.equalsIgnoreCase("m"))
  120. tempActor = new Stuff();
  121. }
  122.  
  123. if (parsing == "m") {
  124. if (qName.equalsIgnoreCase("director"))
  125. tempMovie = new Stuff();
  126. if (qName.equalsIgnoreCase("film"))
  127. tempMovie = new Stuff();
  128. }
  129.  
  130. if (parsing == "a") {
  131. if (qName.equalsIgnoreCase("actor"))
  132. tempActor = new Stuff();
  133. }
  134. }
  135.  
  136. public void characters(char[] ch, int start, int length) throws SAXException {
  137. tempVal = new String(ch, start, length);
  138. }
  139.  
  140. public void endElement(String uri, String localName, String qName) throws SAXException {
  141. if (parsing == "p") {
  142. if (qName.equalsIgnoreCase("filmc")) {
  143. //add it to the list
  144. movieInfo.put(curMovie, tempMovie);
  145. //System.out.println(curMovie);
  146. //System.out.println(tempMovie.printStuff());
  147. }
  148. else if (qName.equalsIgnoreCase("f")) {
  149. curMovie = tempVal;
  150. tempActor.Movie_id.add(tempVal);
  151. if (!tempMovie.Movie_id.contains(tempVal))
  152. tempMovie.Movie_id.add(tempVal);
  153. }
  154. else if (qName.equalsIgnoreCase("t")) {
  155. tempMovie.Movie_name = tempVal;
  156. }
  157. else if (qName.equalsIgnoreCase("a")) {
  158. temp = tempVal.replaceAll("\\s+", "");
  159. if (!temp.equalsIgnoreCase("sa")) {
  160. if (!actorInfo.containsKey(tempVal))
  161. actorInfo.put(tempVal, tempActor);
  162. else
  163. if (!actorInfo.get(tempVal).Movie_id.contains(tempActor.Movie_id.get(0)))
  164. actorInfo.get(tempVal).Movie_id.add(tempActor.Movie_id.get(0));
  165. }
  166. }
  167. }
  168. if (parsing == "m") {
  169. if (qName.equalsIgnoreCase("dirname")) {
  170. curDir = tempVal;
  171. }
  172. if (qName.equalsIgnoreCase("fid")) {
  173. if (!tempMovie.Movie_id.contains(tempVal))
  174. tempMovie.Movie_id.add(tempVal);
  175. mid = tempVal;
  176. }
  177. if (qName.equalsIgnoreCase("t")) {
  178. tempMovie.Movie_name = tempVal;
  179. }
  180. if (qName.equalsIgnoreCase("year")) {
  181. tempMovie.Movie_year = tempVal;
  182. }
  183. if (qName.equalsIgnoreCase("cat")) {
  184. if (!tempMovie.Genres.contains(tempVal))
  185. tempMovie.Genres.add(tempVal);
  186. }
  187. if (qName.equalsIgnoreCase("film")) {
  188. tempMovie.Director_name = curDir;
  189. //System.out.println(mid + " " + tempMovie.printStuff());
  190. if (movieInfo.containsKey(mid)) {
  191. movieInfo.get(mid).Movie_year = tempMovie.Movie_year;
  192. movieInfo.get(mid).Genres = tempMovie.Genres;
  193. movieInfo.get(mid).Director_name = curDir;
  194. }
  195. // System.out.println(mid + " " + movieInfo.get(mid).printStuff());
  196. }
  197. }
  198. if (parsing == "a") {
  199. if (qName.equalsIgnoreCase("dob"))
  200. tempActor.Birth_year = tempVal;
  201. if (qName.equalsIgnoreCase("stagename"))
  202. tempActor.Star_name = tempVal;
  203. if (qName.equalsIgnoreCase("actor")) {
  204. if (!actorInfo.containsKey(tempActor.Star_name))
  205. actorInfo.put(tempActor.Star_name, tempActor);
  206. else
  207. actorInfo.get(tempActor.Star_name).Birth_year = tempActor.Birth_year;
  208. }
  209. }
  210. }
  211.  
  212. public static String genID(int id) {
  213. id++;
  214. return "nm" + Integer.toString(id);
  215. }
  216.  
  217. public static void main(String[] args) throws Exception {
  218. XML_Parse xp = new XML_Parse();
  219. xp.parseCasts();
  220. xp.parseMovies();
  221. xp.parseActors();
  222. // System.out.println(movieInfo.size());
  223.  
  224. // for (String key : actorInfo.keySet()) {
  225. // if (actorInfo.get(key).Movie_id.size()> 1)
  226. // System.out.println(key + " " + actorInfo.get(key).printStuff());
  227. // }
  228.  
  229. HashSet<String> genres = new HashSet<String>();
  230. int maxstarid = 0;
  231. HashMap<String, String> genreids = new HashMap<String, String>();
  232. try {
  233. Class.forName("com.mysql.jdbc.Driver");
  234. Connection con=DriverManager.getConnection(
  235. "jdbc:mysql://localhost:3306/moviedb","mytestuser","mypassword");
  236. String query = "select genres.name, convert(substring(max(stars.id), 3), unsigned integer) as maxid from genres, stars group by genres.name;";
  237. PreparedStatement ps = con.prepareStatement(query);
  238. ResultSet grs = ps.executeQuery();
  239. while (grs.next()) {
  240. maxstarid = grs.getInt("maxid");
  241. genres.add(grs.getString("name"));
  242. }
  243. String genre_query = "CALL add_genre(?);";
  244. PreparedStatement genre_ps = con.prepareStatement(genre_query);
  245. String movie_query = "INSERT INTO movies(id, title, year, director) VALUES(?, ?, ?, ?);";
  246. PreparedStatement movie_ps = con.prepareStatement(movie_query);
  247. String star_query = "CALL add_star(?, ?);";
  248. PreparedStatement star_ps = con.prepareStatement(star_query);
  249. String star_in_movies_query = "INSERT INTO stars_in_movies(starId, movieId) VALUES(?, ?);";
  250. PreparedStatement sim_ps = con.prepareStatement(star_in_movies_query);
  251. String gim_query = "INSERT INTO genres_in_movies(genreId, movieId) VALUES(?, ?);";
  252. PreparedStatement gim_ps = con.prepareStatement(gim_query);
  253. String r_query = "INSERT INTO ratings(movieId, rating, numVotes) VALUES(?, 0.0, 0);";
  254. PreparedStatement r_ps = con.prepareStatement(r_query);
  255.  
  256. for (String key : movieInfo.keySet()) {
  257. movie_ps.setString(1, key);
  258. r_ps.setString(1, key);
  259. r_ps.addBatch();
  260. movie_ps.setString(2, movieInfo.get(key).Movie_name);
  261. movie_ps.setString(3, movieInfo.get(key).Movie_year);
  262. movie_ps.setString(4, movieInfo.get(key).Director_name);
  263. movie_ps.addBatch();
  264. for (String t : movieInfo.get(key).Genres) {
  265. if (!genres.contains(t)) {
  266. genre_ps.setString(1, t);
  267. genres.add(t);
  268. genre_ps.addBatch();
  269. }
  270. }
  271. }
  272.  
  273. for (String key : actorInfo.keySet()) {
  274. String tempID = genID(maxstarid);
  275. star_ps.setString(1, key);
  276. if (actorInfo.get(key).Birth_year == "")
  277. star_ps.setString(2, "NULL");
  278. else
  279. star_ps.setString(2, actorInfo.get(key).Birth_year);
  280. star_ps.addBatch();
  281. for (String t : actorInfo.get(key).Movie_id) {
  282. sim_ps.setString(1, tempID);
  283. sim_ps.setString(2, t);
  284. sim_ps.addBatch();
  285. }
  286. }
  287.  
  288. query = "Select * from genres;";
  289. ps = con.prepareStatement(query);
  290. grs = ps.executeQuery();
  291. while (grs.next()) {
  292. genreids.put(grs.getString("name"), grs.getString("id"));
  293. }
  294. for (String key : movieInfo.keySet()) {
  295. for(String t : movieInfo.get(key).Genres) {
  296. String tempgid = genreids.get(t);
  297. gim_ps.setString(1, tempgid);
  298. gim_ps.setString(2, key);
  299. gim_ps.addBatch();
  300. }
  301. }
  302.  
  303. movie_ps.executeBatch();
  304. star_ps.executeBatch();
  305. sim_ps.executeBatch();
  306. genre_ps.executeBatch();
  307. gim_ps.executeBatch();
  308. r_ps.executeBatch();
  309.  
  310.  
  311. } catch (SQLException e) {
  312. // TODO Auto-generated catch block
  313. e.printStackTrace();
  314. }
  315. catch (Exception e) {
  316. throw e;
  317. }
  318.  
  319. for (String s : genres) {
  320. System.out.println(s);
  321. }
  322.  
  323.  
  324. }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement