Guest User

Untitled

a guest
Oct 31st, 2016
122
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.sql.*;
  2.  
  3. public class GetVillainsNames {
  4. private static final String URL = "jdbc:mysql://localhost:3306/minions_db";
  5. private static final String USER = "root";
  6. private static final String PASSWORD = "1234";
  7.  
  8. public static void main(String[] args) {
  9. String query =
  10. "SELECT v.name, COUNT(vm.villain_id) AS minions_count FROM villains AS v" +
  11. "INNER JOIN villains_minions AS vm" +
  12. "ON v.id = vm.villain_id" +
  13. "GROUP BY v.id";
  14.  
  15. try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
  16. Statement statement = connection.createStatement();
  17. ResultSet result = statement.executeQuery(query)){
  18.  
  19. while (result.next()){
  20. String name = result.getString("name");
  21. int count = result.getInt("minions_count");
  22. System.out.printf("Name: %s, Count: %d\n", name, count);
  23. }
  24.  
  25. } catch (SQLException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }
RAW Paste Data