Browse Source

Add addMovie function

toomanysugar 4 years ago
parent
commit
63171f3319
2 changed files with 76 additions and 1 deletions
  1. 68 1
      src/com/moviesdb/DataBaseConnector.java
  2. 8 0
      src/com/moviesdb/Movie.java

+ 68 - 1
src/com/moviesdb/DataBaseConnector.java

@@ -25,7 +25,6 @@ public class DataBaseConnector {
 
         } catch (SQLException ex) {
             //exception
-
             ex.printStackTrace();
         }
     }
@@ -101,5 +100,73 @@ public class DataBaseConnector {
         return  resultedMovies;
     }
 
+    public void addMovie(Movie movie) {
+        try {
+            Statement statement = conn.createStatement();
+            String req;
+
+            String commandString;
+            String intoPart = "";
+            String valPart = "";
+
+            if (!movie.name().equals("Movie_name_undefined")) {
+                intoPart += " name,";
+                valPart += " '" + movie.name() + "',";
+            }
+
+            if (!movie.genre().equals("Movie_genre_undefined")) {
+                intoPart += " genre,";
+                valPart += " '" + movie.genre() + "',";
+            }
+
+            if (!movie.description().equals("Movie_description_undefined")) {
+                intoPart += " description,";
+                valPart += " '" + movie.description() + "',";
+            }
+
+            if (!movie.duration().equals("Movie_duration_undefined")) {
+                intoPart += " duration,";
+                valPart += " '" + movie.duration() + "',";
+            }
+
+            if (!movie.rating().equals("Movie_rating_undefined")) {
+                intoPart += " user_rating,";
+                valPart += " '" + movie.rating() + "',";
+            }
+
+            if (!movie.year().equals("Movie_year_undefined")) {
+                intoPart += " year,";
+                valPart += " '" + movie.year() + "',";
+            }
+
+            if (!movie.studio().equals("Movie_studio_undefined")) {
+                intoPart += " studio,";
+                valPart += " '" + movie.studio() + "',";
+            }
+
+            if (!movie.director().equals("Movie_director_undefined")) {
+                intoPart += " director,";
+                valPart += " '" + movie.director() + "',";
+            }
+
+            if (!movie.getImageLink().equals("Movie_ImageLink_undefined")) {
+                intoPart += " image_link,";
+                valPart += " '" + movie.getImageLink() + "',";
+            }
+
+            if (intoPart.charAt(intoPart.length() - 1) == ',')
+                intoPart = intoPart.substring(0, intoPart.length() - 1);
+            if (valPart.charAt(valPart.length() - 1) == ',')
+                valPart = valPart.substring(0, valPart.length() - 1);
 
+            //сборка текста запроса
+            req = "INSERT INTO Movies (" + intoPart + ") ";
+            req += "values (" + valPart + ");";
+
+            //выполнение запроса
+            statement.execute(req);
+        } catch (SQLException ex) {
+            ex.printStackTrace();
+        }
+    }
 }

+ 8 - 0
src/com/moviesdb/Movie.java

@@ -82,6 +82,13 @@ public class Movie {
             return "Movie_director_undefined";
     }
 
+    public String getImageLink(){
+        if (movie_ImageLink != null)
+            return movie_ImageLink;
+        else
+            return "Movie_ImageLink_undefined";
+    }
+
     public Image cover(){
         if (movie_ImageLink != null) {
             Image image = new Image(movie_ImageLink);
@@ -93,6 +100,7 @@ public class Movie {
         }
     }
 
+
     //--------Set methods
 
     public int setId(int newID){