Advertisement
Guest User

Untitled

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