Advertisement
Guest User

Untitled

a guest
Jan 5th, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.*;
  3.  
  4. /*
  5. *
  6. *
  7. */
  8.  
  9.  
  10.  
  11. public class DBLogic {
  12.  
  13. Connection myConn;
  14. ArrayList<String> types = new ArrayList<String>();
  15. ArrayList<Integer> genreIds = new ArrayList<Integer>();
  16. ArrayList<String> actorNames = new ArrayList<String>();
  17. ArrayList<Integer> actorIds = new ArrayList<Integer>();
  18. ArrayList<String> directorNames = new ArrayList<String>();
  19. ArrayList<Integer> directorIds = new ArrayList<Integer>();
  20. ArrayList<String> movieTitles = new ArrayList<String>();
  21. ArrayList<Integer> movieYears = new ArrayList<Integer>();
  22. ArrayList<String> movieLength = new ArrayList<String>();
  23. ArrayList<String> movieDescrip = new ArrayList<String>();
  24. ArrayList<String>searchResults = new ArrayList<String>();
  25. ArrayList<String>selectionList = new ArrayList<String>();
  26.  
  27.  
  28. public DBLogic() throws SQLException{
  29. // Connection statement to the database
  30. myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/movies","root",".");
  31. }
  32.  
  33. // Performs the operations for showing all the entries in the actors table
  34. public void showActors() throws SQLException{
  35. String actorName = null;
  36. int actorBirthDate;
  37.  
  38. Statement stmt = myConn.createStatement();
  39. String query = "SELECT * FROM actor";
  40. ResultSet rs = stmt.executeQuery(query);
  41.  
  42. while(rs.next()){
  43. actorBirthDate = rs.getInt("id");
  44. actorName = rs.getString("Name");
  45. actorNames.add(actorName);
  46. actorIds.add(actorBirthDate);
  47. }
  48. }
  49.  
  50. // Performs the operations for showing all the entries in the directors table
  51. public void showDirector() throws SQLException{
  52. String directorName = null;
  53. int directorBirthDate;
  54.  
  55. Statement stmt = myConn.createStatement();
  56. String query = "SELECT * FROM director";
  57. ResultSet rs = stmt.executeQuery(query);
  58.  
  59. while(rs.next()){
  60. directorBirthDate = rs.getInt("id");
  61. directorName = rs.getString("Name");
  62. directorNames.add(directorName);
  63. directorIds.add(directorBirthDate);
  64. }
  65. }
  66.  
  67. // Performs the operations for showing all the entries in the Genre table
  68. public void showGenre() throws SQLException{
  69. String genre = null;
  70. int id;
  71.  
  72. Statement stmt = myConn.createStatement();
  73. String query = "SELECT * FROM genre";
  74. ResultSet rs = stmt.executeQuery(query);
  75.  
  76. while(rs.next()){
  77. id = rs.getInt("id");
  78. genre = rs.getString("type");
  79. types.add(genre);
  80. genreIds.add(id);
  81. }
  82.  
  83. }
  84.  
  85. // Performs the operations for showing all the entries in the Movies table
  86. public void showMovies() throws SQLException{
  87. String movieTitle = null;
  88. int movieYear;
  89. String movieTime = null;
  90. String movieComment = null;
  91.  
  92. Statement stmt = myConn.createStatement();
  93. String query = "SELECT * FROM movie";
  94. ResultSet rs = stmt.executeQuery(query);
  95.  
  96. while(rs.next()){
  97. movieTitle = rs.getString("Title");
  98. movieYear = rs.getInt("Year");
  99. movieTime = rs.getString("RunningTime");
  100. movieComment = rs.getString("Description");
  101. movieTitles.add(movieTitle);
  102. movieYears.add(movieYear);
  103. movieLength.add(movieTime);
  104. movieDescrip.add(movieComment);
  105. }
  106. }
  107.  
  108. // Insertion logic for inserting movies into the Movie table
  109. public void insertMovie(String movieTitle, int movieYear, String movieTime, String movieComment) throws SQLException{
  110.  
  111. Statement stmt = myConn.createStatement();
  112. String query = "INSERT INTO movie (Title, Year, RunningTime, Description) VALUES ('" + movieTitle + "', '" + movieYear + "' , '" + movieTime + "', '" + movieComment + "')";
  113. stmt.executeUpdate(query);
  114.  
  115. }
  116.  
  117. // Deletes movies from the Movies table
  118. public void deleteMovie(String movieTitle, int movieYear) throws SQLException{
  119.  
  120. Statement stmt = myConn.createStatement();
  121. String query = "DELETE FROM movie WHERE Title = '" + movieTitle + "'AND Year ='" + movieYear +"'";
  122. stmt.executeUpdate(query);
  123. }
  124.  
  125. // A selection function which shows all the movies a director has either directed
  126. // an actor acted in or is included in a genre
  127. public void selection(String type, String name) throws SQLException{
  128. String result = null;
  129.  
  130. if(type.equals("director")){
  131. PreparedStatement stmt = myConn.prepareStatement("SELECT directs.movie_name FROM directs, director, movie WHERE director.Name = ? AND director.id = directs.director_id AND directs.movie_name = movie.Title");
  132. stmt.setString(1, name);
  133. ResultSet rs = stmt.executeQuery();
  134. while(rs.next()){
  135. result = rs.getString("movie_name");
  136. selectionList.add(result);
  137. }
  138. }
  139. else if(type.equals("actor")){
  140. PreparedStatement stmt = myConn.prepareStatement("SELECT startsin.movie_name FROM startsin, actor, movie WHERE actor.Name = ? AND actor.id = startsin.actor_id AND startsin.movie_name = movie.Title ");
  141. stmt.setString(1, name);
  142. ResultSet rs = stmt.executeQuery();
  143. while(rs.next()){
  144. result = rs.getString("movie_name");
  145. selectionList.add(result);
  146. }
  147. }
  148. else if(type.equals("genre")){
  149. PreparedStatement stmt = myConn.prepareStatement("SELECT istypeof.movie_namel FROM istypeof, genre WHERE genre.type = ? AND genre.id = istypeof.genre_id");
  150. stmt.setString(1, name);
  151. ResultSet rs = stmt.executeQuery();
  152. while(rs.next()){
  153. result = rs.getString("movie_namel");
  154. selectionList.add(result);
  155. }
  156.  
  157. }
  158.  
  159. }
  160.  
  161.  
  162. // A function for editing different columns in the different tables
  163. public void editData(String tableName, String columnName,String newValue, int id) throws SQLException{
  164.  
  165. if(tableName.equals("director")){
  166. PreparedStatement stmt = myConn.prepareStatement("UPDATE director SET Name = ? WHERE id = ?");
  167. stmt.setString(1, newValue);
  168. stmt.setInt(2, id);
  169. stmt.executeUpdate();
  170. }
  171. else if(tableName.equals("actor")){
  172. PreparedStatement stmt = myConn.prepareStatement("UPDATE actor SET Name = ? WHERE id = ?");
  173. stmt.setString(1, newValue);
  174. stmt.setInt(2, id);
  175. stmt.executeUpdate();
  176. }
  177. else if(tableName.equals("genre")){
  178. PreparedStatement stmt = myConn.prepareStatement("UPDATE genre SET type = ? WHERE id = ?");
  179. stmt.setString(1, newValue);
  180. stmt.setInt(2, id);
  181. stmt.executeUpdate();
  182. }
  183. }
  184.  
  185.  
  186. //Searches for all the actors and directors with the methods argument in their name
  187. public void searchData(String searchString) throws SQLException{
  188. String results = null;
  189. PreparedStatement stmt = myConn.prepareStatement("SELECT Name FROM actor WHERE Name LIKE ? UNION SELECT Name FROM director WHERE Name LIKE ? ORDER BY Name ");
  190. stmt.setString(1, "%" + searchString + "%");
  191. stmt.setString(2, "%" + searchString + "%");
  192. ResultSet rs = stmt.executeQuery();
  193. while(rs.next()){
  194. results = rs.getString("Name");
  195. searchResults.add(results);
  196. }
  197. }
  198.  
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement