Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.02 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.sql.*;
  5. import java.util.*;
  6.  
  7. public class Main {
  8.  
  9.     private static final String CONNECTION_STRING = "jdbc:mysql://localhost:3306/";
  10.     private static final String DATABASE_NAME = "minions_db";
  11.     private static Connection connection;
  12.     private static String query;
  13.     private static PreparedStatement statement;
  14.     private static BufferedReader reader;
  15.     private static int released_minions;
  16.     public static String minionName;
  17.     public static int minionAge;
  18.     public static String minionTown;
  19.     public static String villainName;
  20.     public static int townId;
  21.  
  22.     public static void main(String[] args) throws SQLException, IOException {
  23.         reader = new BufferedReader(new InputStreamReader(System.in));
  24.  
  25.         Properties props = new Properties();
  26.  
  27.         props.setProperty("user", "root");
  28.         props.setProperty("password", "12345");
  29.  
  30.         connection = DriverManager.getConnection(CONNECTION_STRING + DATABASE_NAME, props);
  31.  
  32.         //2. Get Villains Names
  33.         //getVillainsNamesAndCountOfMinions();
  34.  
  35.         //3. Get Minion Names
  36.         //getMinionNameEx();
  37.  
  38.         //4. Add Minion
  39.         //addMinion();
  40.  
  41.         //5. Change Town Names Casing
  42.         //changeTownNamesCasing("Bulgaria");
  43.  
  44.         //6. *Remove Villain
  45.         //removeVillainEX();
  46.  
  47.         //7. Print All Minion Names
  48.         //printAllMinionNames();
  49.  
  50.         //8. Increase Minions Age
  51.         //increaseMinionsAge();
  52.  
  53.         // 9. Increase Age Stored Procedure
  54.         //increaseAgeStoredProcedure();
  55.     }
  56.  
  57.     private static void addMinion() throws IOException, SQLException {
  58.  
  59.             reader = new BufferedReader(
  60.                     new InputStreamReader(
  61.                             System.in
  62.                     )
  63.             );
  64.  
  65.             System.out.println("Enter minions data: ");
  66.             //Enter data in format: Minion: Robert 14 Berlin
  67.             String[] minionsInfo = reader.readLine().split("\\s+");
  68.  
  69.             System.out.println("Enter villains data: ");
  70.             //Enter data in format: Villain: Gru
  71.             String[] villainsName = reader.readLine().split("\\s+");
  72.  
  73.             minionName = minionsInfo[1];
  74.             minionAge = Integer.parseInt(minionsInfo[2]);
  75.             minionTown = minionsInfo[3];
  76.  
  77.             villainName = villainsName[1];
  78.  
  79.             checkIfTownExists(minionTown);
  80.  
  81.             checkIfVillainExists(villainName);
  82.  
  83.             insertAllDataInToDatabase();
  84.  
  85.             connection.close();
  86.         }
  87.  
  88.         private static void checkIfVillainExists(String villainName) throws SQLException {
  89.             query = "SELECT name FROM minions_db.villains";
  90.             statement = connection.prepareStatement(query);
  91.             ResultSet resultSet = statement.executeQuery();
  92.  
  93.             List<String> villains = new ArrayList<>();
  94.  
  95.             while (resultSet.next()) {
  96.                 villains.add(resultSet.getString("name"));
  97.             }
  98.  
  99.             if (!villains.contains(villainName)) {
  100.                 insertVillainInToTable(villainName);
  101.             }
  102.         }
  103.  
  104.         private static void insertAllDataInToDatabase() throws SQLException {
  105.             getTownId(minionTown);
  106.  
  107.             query = "INSERT INTO minions_db.minions(name, age, town_id) " +
  108.                     "VALUES (?, ?, ?)";
  109.  
  110.             statement = connection.prepareStatement(query);
  111.             statement.setString(1, minionName);
  112.             statement.setInt(2, minionAge);
  113.             statement.setInt(3, townId);
  114.  
  115.             statement.execute();
  116.  
  117.             System.out.println(String.format("Successfully added %s to be minion of %s.",
  118.                     minionName, villainName));
  119.         }
  120.  
  121.         private static void insertVillainInToTable(String villainName) throws SQLException {
  122.             query = "INSERT INTO minions_db.villains(name, evilness_factor) " +
  123.                     "VALUE (?, ?);";
  124.  
  125.             statement = connection.prepareStatement(query);
  126.             statement.setString(1, villainName);
  127.             statement.setString(2, "evil");
  128.  
  129.             statement.execute();
  130.  
  131.             System.out.println(String.format("Villain %s was added to the database.",
  132.                     villainName));
  133.         }
  134.  
  135.         private static void checkIfTownExists(String minionTown) throws SQLException {
  136.             query = "SELECT name FROM minions_db.towns;";
  137.             statement = connection.prepareStatement(query);
  138.             ResultSet resultSet = statement.executeQuery();
  139.  
  140.             List<String> minionsNames = new ArrayList<>();
  141.  
  142.             while (resultSet.next()) {
  143.                 minionsNames.add(resultSet.getString("name"));
  144.             }
  145.  
  146.             if(!minionsNames.contains(minionTown)) {
  147.                 insertTownInToTable(minionTown);
  148.             }
  149.         }
  150.  
  151.         private static void insertTownInToTable(String minionTown) throws SQLException {
  152.             query = "INSERT INTO minions_db.towns(name) " +
  153.                     "VALUE (?);";
  154.  
  155.             statement = connection.prepareStatement(query);
  156.             statement.setString(1, minionTown);
  157.  
  158.             statement.execute();
  159.             System.out.println(String.format("Town %s was added to the database.",
  160.                     minionTown));
  161.         }
  162.  
  163.         private static void getTownId(String minionTown) throws SQLException {
  164.             query = "SELECT id FROM minions_db.towns WHERE name = ?";
  165.             statement = connection.prepareStatement(query);
  166.             statement.setString(1, minionTown);
  167.             ResultSet resultSet = statement.executeQuery();
  168.  
  169.             if (resultSet.next()) {
  170.                 townId = resultSet.getInt("id");
  171.             }
  172.         }
  173.  
  174.  
  175.  
  176.     private static void increaseAgeStoredProcedure() throws SQLException, IOException {
  177.             BufferedReader reader =
  178.                     new BufferedReader(
  179.                             new InputStreamReader(
  180.                                     System.in
  181.                             )
  182.                     );
  183.  
  184.             System.out.println("Enter minion id to be updated it's age: ");
  185.  
  186.             int id = Integer.parseInt(reader.readLine());
  187.  
  188.             updateMinionAge(id);
  189.  
  190.             printNameAndAgeOfUpdatedMinion(id);
  191.  
  192.             connection.close();
  193.         }
  194.  
  195.         private static void printNameAndAgeOfUpdatedMinion(int id) throws SQLException {
  196.             query = "SELECT name, age " +
  197.                     "FROM minions_db.minions " +
  198.                     "WHERE id = ?";
  199.  
  200.             statement = connection.prepareStatement(query);
  201.             statement.setInt(1, id);
  202.  
  203.             ResultSet resultSet = statement.executeQuery();
  204.  
  205.             if (resultSet.next()) {
  206.                 System.out.println(String.format(
  207.                         "%s %d",
  208.                         resultSet.getString("name"),
  209.                         resultSet.getInt("age")
  210.                 ));
  211.             }
  212.         }
  213.  
  214.         private static void updateMinionAge(int id) throws SQLException {
  215.  
  216.             query = "CALL minions_db.usp_get_older(?)";
  217.  
  218.             statement = connection.prepareStatement(query);
  219.             statement.setInt(1, id);
  220.  
  221.             statement.executeUpdate();
  222.         }
  223.  
  224.  
  225.     private static void increaseMinionsAge() throws SQLException, IOException {
  226.         BufferedReader reader =
  227.                 new BufferedReader(
  228.                         new InputStreamReader(
  229.                                 System.in
  230.                         )
  231.                 );
  232.  
  233.  
  234.         System.out.println("Enter minions id's, separated by single spaces: ");
  235.  
  236.         int[] minionId = Arrays
  237.                 .stream(reader.readLine().split("\\s+"))
  238.                 .mapToInt(Integer::parseInt)
  239.                 .toArray();
  240.  
  241.         modifyMinionNamesAndAges(minionId);
  242.  
  243.         connection.close();
  244.     }
  245.  
  246.         private static void modifyMinionNamesAndAges(int[] minionId) throws SQLException {
  247.             for (int i : minionId) {
  248.                 query = "UPDATE minions_db.minions m " +
  249.                         "SET m.name = LOWER(m.name)," +
  250.                         " m.age = m.age + 1 " +
  251.                         "WHERE m.id = ?";
  252.  
  253.                 statement = connection.prepareStatement(query);
  254.                 statement.setInt(1, i);
  255.                 statement.executeUpdate();
  256.             }
  257.  
  258.             printMinionsAndTheirAges();
  259.         }
  260.  
  261.         private static void printMinionsAndTheirAges() throws SQLException {
  262.             query = "SELECT name, age " +
  263.                     "FROM minions_db.minions";
  264.  
  265.             statement = connection.prepareStatement(query);
  266.             ResultSet resultSet = statement.executeQuery();
  267.  
  268.             while (resultSet.next()) {
  269.                 System.out.println(String.format(
  270.                         "%s %d",
  271.                         resultSet.getString("name"),
  272.                         resultSet.getInt("age")
  273.                 ));
  274.             }
  275.         }
  276.  
  277.     private static void printAllMinionNames() throws SQLException {
  278.  
  279.             executeMinionNames();
  280.  
  281.             connection.close();
  282.     }
  283.     private static void executeMinionNames() throws SQLException {
  284.         query = "SELECT name FROM minions_db.minions";
  285.         statement = connection.prepareStatement(query);
  286.         ResultSet resultSet = statement.executeQuery();
  287.  
  288.         Deque<String> minionNames = new ArrayDeque<>();
  289.  
  290.         while (resultSet.next()) {
  291.             minionNames.add(resultSet.getString(1));
  292.         }
  293.  
  294.         printMinions(minionNames);
  295.     }
  296.  
  297.     private static void printMinions(Deque<String> minionNames) {
  298.         int counter = 0;
  299.         while (!minionNames.isEmpty()) {
  300.             counter++;
  301.             if (counter % 2 == 0) {
  302.                 String minion = minionNames.removeLast();
  303.                 System.out.println(minion);
  304.             } else {
  305.                 String minion = minionNames.removeFirst();
  306.                 System.out.println(minion);
  307.             }
  308.         }
  309.     }
  310.  
  311.     private static void removeVillainEX() throws SQLException, IOException {
  312.         System.out.println("Enter villain Id: ");
  313.         int villainId = Integer.parseInt(reader.readLine());
  314.  
  315.         findOutput(villainId);
  316.         releaseAllMinionsByGivenVillainId(villainId);
  317.         removeVillainFromDatabase(villainId);
  318.  
  319.         connection.close();
  320.  
  321.     }
  322.         private static boolean checkIfVillainExists(int villainId) throws SQLException {
  323.             query = "SELECT id " +
  324.                     "FROM minions_db.villains " +
  325.                     "WHERE id = ?";
  326.  
  327.             statement = connection.prepareStatement(query);
  328.             statement.setInt(1, villainId);
  329.  
  330.             ResultSet resultSet = statement.executeQuery();
  331.  
  332.             return resultSet.next();
  333.         }
  334.  
  335.         private static void findOutput(int villainId) throws SQLException {
  336.             query = "SELECT COUNT(m.name) released_minions_count " +
  337.                     "FROM minions_db.minions m " +
  338.                     "JOIN minions_db.minions_villains mv ON m.id = mv.minion_id " +
  339.                     "JOIN minions_db.villains v ON mv.villain_id = v.id " +
  340.                     "WHERE v.id = ?";
  341.  
  342.             statement = connection.prepareStatement(query);
  343.             statement.setInt(1, villainId);
  344.             ResultSet resultSet = statement.executeQuery();
  345.             if (resultSet.next()) {
  346.                  released_minions = resultSet.getInt(
  347.                         "released_minions_count");
  348.             }
  349.         }
  350.  
  351.         private static void releaseAllMinionsByGivenVillainId(int villainId) throws SQLException {
  352.             query = "DELETE minions_db.minions_villains " +
  353.                     "FROM minions_db.minions_villains " +
  354.  
  355.                     "WHERE villain_id = ?;";
  356.  
  357.             statement = connection.prepareStatement(query);
  358.             statement.setInt(1, villainId);
  359.  
  360.             statement.execute();
  361.         }
  362.  
  363.         private static void removeVillainFromDatabase(int villainId) throws SQLException {
  364.  
  365.             if(!checkIfVillainExists(villainId)) {
  366.                 System.out.println("No such villain was found");
  367.                 return;
  368.             } else {
  369.                 printVillainNameInOutput(villainId);
  370.             }
  371.  
  372.             query = "DELETE FROM minions_db.villains " +
  373.                     "WHERE id = ?";
  374.  
  375.  
  376.             statement = connection.prepareStatement(query);
  377.             statement.setInt(1, villainId);
  378.             statement.execute();
  379.  
  380.         }
  381.  
  382.         private static void printVillainNameInOutput(int villainId) throws SQLException {
  383.             query = "SELECT name " +
  384.                     "FROM minions_db.villains " +
  385.                     "WHERE id = ?";
  386.  
  387.             statement = connection.prepareStatement(query);
  388.             statement.setInt(1, villainId);
  389.  
  390.             ResultSet resultSet = statement.executeQuery();
  391.  
  392.             if (resultSet.next()) {
  393.                 System.out.printf("%s was deleted\r\n", resultSet.getString("name"));
  394.                 System.out.println(String.format(
  395.                         "%d minions released",
  396.                         released_minions
  397.                 ));
  398.             }
  399.         }
  400.  
  401.     private static void changeTownNamesCasing(String country) throws SQLException {
  402.         String query = "select name from towns where country = ?;";
  403.         PreparedStatement statement = connection.prepareStatement(query);
  404.         statement.setString(1, country);
  405.         ResultSet rs;
  406.         rs = statement.executeQuery();
  407.         List<String> changedWords = new ArrayList<>();
  408.  
  409.         while (rs.next()) {
  410.             String town = rs.getString(1);
  411.  
  412.             for (int i = 0; i < town.length(); i++) {
  413.                 if (Character.isLowerCase(town.charAt(i))) {
  414.  
  415.                     town = town.toUpperCase();
  416.                     changedWords.add(town);
  417.                     String townQuery = "update towns " +
  418.                             "set name = ?" +
  419.                             "where name = ?";
  420.  
  421.                     PreparedStatement townUppercaseChange = connection.prepareStatement(townQuery);
  422.                     townUppercaseChange.setString(1, town);
  423.                     townUppercaseChange.setString(2, rs.getString(1));
  424.                     townUppercaseChange.executeUpdate();
  425.                     break;
  426.                 }
  427.             }
  428.         }
  429.  
  430.         String result = changedWords.isEmpty() ? "No town names were affected." :
  431.                 String.format("%d town names were affected.%n[%s]", changedWords.size(),
  432.                         String.join(", ", changedWords)
  433.                 );
  434.         System.out.println(
  435.                 result
  436.         );
  437.     }
  438.  
  439.     private static void insertEntityInTowns(String minionTown) throws SQLException {
  440.         query = "INSERT INTO towns (name, country) value(?, NULL)";
  441.  
  442.         statement = connection.prepareStatement(query);
  443.         statement.setString(1, minionTown);
  444.  
  445.         statement.execute();
  446.     }
  447.  
  448.     private static boolean checkIfEntityExistsByName(String entityName, String tableName) throws SQLException {
  449.         query = "SELECT * FROM " + tableName + " WHERE name = ?";
  450.  
  451.         statement = connection.prepareStatement(query);
  452.         statement.setString(1, entityName);
  453.  
  454.         ResultSet resultSet = statement.executeQuery();
  455.  
  456.         return resultSet.next();
  457.     }
  458.  
  459.     private static void getMinionNameEx() throws IOException, SQLException {
  460.         System.out.println("Enter villain id: ");
  461.  
  462.         int villain_id = Integer.parseInt(reader.readLine());
  463.  
  464.         if (!checkIfEntityExists(villain_id, "villains")) {
  465.             System.out.printf("No villain with ID %d exists in the database.", villain_id);
  466.             return;
  467.         }
  468.         System.out.printf("Villain: %s%n", getEntityNameById(villain_id, "villains"));
  469.  
  470.         getAllMinionNamesAndAgeFroVillain(villain_id);
  471.     }
  472.  
  473.     private static void getAllMinionNamesAndAgeFroVillain(int villain_id) throws SQLException {
  474.         query = "SELECT m.name, m.age FROM Minions AS m\n" +
  475.                 "                              JOIN minions_villains mv\n" +
  476.                 "                                   ON m.id = mv.minion_id\n" +
  477.                 "WHERE mv.villain_id = ?";
  478.  
  479.         statement = connection.prepareStatement(query);
  480.         statement.setInt(1, villain_id);
  481.  
  482.         ResultSet resultSet = statement.executeQuery();
  483.  
  484.         int counter = 0;
  485.  
  486.         while (resultSet.next()) {
  487.             counter++;
  488.  
  489.             System.out.printf("%d. %s %d%n", counter, resultSet.getString(1), resultSet.getInt(2));
  490.         }
  491.     }
  492.  
  493.     private static String getEntityNameById(int villain_id, String tableName) throws SQLException {
  494.         query = "SELECT name FROM " + tableName + " WHERE id = ?";
  495.  
  496.         statement = connection.prepareStatement(query);
  497.         statement.setInt(1, villain_id);
  498.  
  499.         ResultSet resultSet = statement.executeQuery();
  500.  
  501.         return resultSet.next() ? resultSet.getString(1) : null;
  502.     }
  503.  
  504.     private static boolean checkIfEntityExists(int villain_id, String villains) throws SQLException {
  505.         query = "SELECT * FROM " + villains + " WHERE id = ?";
  506.  
  507.         statement = connection.prepareStatement(query);
  508.         statement.setInt(1, villain_id);
  509.  
  510.         ResultSet resultSet = statement.executeQuery();
  511.  
  512.         return resultSet.next();
  513.     }
  514.  
  515.     private static void getVillainsNamesAndCountOfMinions() throws SQLException {
  516.         query = "SELECT v.name, COUNT(mv.minion_id) AS 'count'\n" +
  517.                 "FROM villains AS v\n" +
  518.                 "JOIN minions_villains mv on v.id = mv.villain_id\n" +
  519.                 "GROUP BY v.name\n" +
  520.                 "HAVING count > 15\n" +
  521.                 "ORDER BY count DESC";
  522.  
  523.         statement = connection.prepareStatement(query);
  524.  
  525.         ResultSet resultSet = statement.executeQuery();
  526.  
  527.         while (resultSet.next()) {
  528.             System.out.printf("%s %d%n", resultSet.getString(1), resultSet.getInt(2));
  529.         }
  530.     }
  531.  
  532.  
  533. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement