JennyDi

addMinionEx4

Oct 27th, 2020 (edited)
797
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     public void addMinionEx4() throws IOException, SQLException {
  2.         System.out.println("Enter minion info (copy the input):");
  3.         String[] minionsInfo = reader.readLine().split("\\s+");
  4.         String minionName = minionsInfo[1];
  5.         int minionAge = Integer.parseInt(minionsInfo[2]);
  6.         String townName = minionsInfo[3];
  7.  
  8.         System.out.println("Enter villain name:");
  9.         String villainName = reader.readLine().split("\\s+")[1];
  10.  
  11.         int townId = getEntityIdByName(townName, "towns");
  12.  
  13.         if (townId < 0) {
  14.             insertIntoTowns(townName);
  15.             townId =  getEntityIdByName(townName, "towns");
  16.             System.out.printf("Town %s was added to the database.%n", townName);
  17.         }
  18.  
  19.         insertMinionIntoMinions(minionName, minionAge, townId);
  20.  
  21.  
  22.         int villainId = getEntityIdByName(villainName, "villains");
  23.  
  24.         if (villainId < 0) {
  25.             createVillain(villainName);
  26.             villainId = getEntityIdByName(villainName, "villains");
  27.             System.out.printf("Villain %s was added to the database.%n", villainName);
  28.         }
  29.  
  30.         addMinionToVillain(minionName, villainId);
  31.  
  32.         System.out.printf("Successfully added %s to be minion of %s.%n", minionName, villainName);
  33.  
  34.     }
  35.  
  36.     private void addMinionToVillain(String minionName, int villainId) throws SQLException {
  37.         int minionId = getEntityIdByName(minionName, "minions");
  38.         String query = "INSERT INTO minions_villains VALUES (?, ?)";
  39.         connection.setAutoCommit(false);
  40.         PreparedStatement statement = connection.prepareStatement(query);
  41.         statement.setInt(1, minionId);
  42.         statement.setInt(2, villainId);
  43.         statement.execute();
  44.         connection.commit();
  45.     }
  46.  
  47.     private void createVillain(String villainName) throws SQLException {
  48.         String query = "INSERT INTO villains(name, evilness_factor) VALUES (?, ?)";
  49.         connection.setAutoCommit(false);
  50.         PreparedStatement statement = connection.prepareStatement(query);
  51.         statement.setString(1, villainName);
  52.         statement.setString(2, "evil");
  53.         statement.execute();
  54.         connection.commit();
  55.     }
  56.  
  57.     private void insertMinionIntoMinions(String minionName, int minionAge, int townId) throws SQLException {
  58.         String query = "INSERT INTO minions(name, age, town_id) VALUES (?, ?, ?)";
  59.         connection.setAutoCommit(false);
  60.         PreparedStatement statement = connection.prepareStatement(query);
  61.         statement.setString(1, minionName);
  62.         statement.setInt(2, minionAge);
  63.         statement.setInt(3, townId);
  64.         statement.execute();
  65.         connection.commit();
  66.     }
  67.  
  68.     private void insertIntoTowns(String townName) throws SQLException {
  69.         String query = "INSERT INTO towns(name) VALUES (?)";
  70.         connection.setAutoCommit(false);
  71.         PreparedStatement statement = connection.prepareStatement(query);
  72.         statement.setString(1, townName);
  73.         statement.execute();
  74.         connection.commit();
  75.     }
  76.  
  77.     private int getEntityIdByName(String entityName, String tableName) throws SQLException {
  78.         String query = String.format("SELECT id FROM %s WHERE name = ?", tableName);
  79.         PreparedStatement statement = connection.prepareStatement(query);
  80.         statement.setString(1, entityName);
  81.         ResultSet resultSet = statement.executeQuery();
  82.  
  83.         return resultSet.next() ? resultSet.getInt(1) : -1;
  84.     }
RAW Paste Data