Advertisement
Guest User

Untitled

a guest
Mar 4th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package databaseopg2;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.Statement;
  12. import javax.swing.JOptionPane;
  13.  
  14. /**
  15. *
  16. * @author Mikkel
  17. */
  18. public class Databaseopg2 {
  19.  
  20.  
  21. public static String url;
  22. public static String username;
  23. public static String password;
  24. public static void main(String[] args) {
  25.  
  26. try {
  27. Class.forName("org.postgresql.Driver");
  28. } catch (java.lang.ClassNotFoundException e) {
  29. System.out.println(e);
  30. }
  31.  
  32.  
  33. url = "jdbc:postgresql://stampy.db.elephantsql.com:5432/zcqmkjrl";
  34. username = "zcqmkjrl";
  35. password = "waeDTsPXtvSV-zkz-S-mNlCV371Beryl";
  36.  
  37. String answer = JOptionPane.showInputDialog(null, "Press 1 display your options");
  38. if(Integer.parseInt(answer) == 1){
  39. System.out.println("Press a for: List of all names of coaches and the team they belong to.");
  40. System.out.println("Press b for: List of all names of people (players and coachers) who are on a team, which has won at least one tournament.");
  41. System.out.println("Press c for: List of all names of teams and the number of players on that team.");
  42. System.out.println("Input any positive integer to get a list of the names of all tournaments, with at least that many participating teams");
  43. }
  44. String answer2 = JOptionPane.showInputDialog(null, "Press 1 display your options");
  45. if(answer2.equals("a")){
  46. System.out.println("Coaches | teams");
  47. String statement = "SELECT people.name, teams.name as teamnament FROM people INNER JOIN coaches On(people.id = people_id) INNER JOIN teams On(coaches.team_id = teams.id)";
  48. getData(statement,2);
  49.  
  50. }
  51. else if(answer2.equals("b")){
  52. System.out.println("players/coaches | teams_who_won");
  53. String statement = "Select name FROM people INNER JOIN playsOn On(id = playsOn.people_id) WHERE(playsOn.team_id IN (SELECT teams.id FROM teams,winners WHERE teams.id = winners.team_id)) UNION Select name FROM people INNER JOIN coaches On(id = coaches.people_id) WHERE(coaches.team_id IN (SELECT teams.id FROM teams,winners WHERE teams.id = winners.team_id))";
  54. getData(statement,1);
  55. }
  56. else if(answer2.equals("c")){
  57. System.out.println("team_name | number_of_players_on_team" );
  58. String statement = "SELECT name, count(*) FROM teams INNER JOIN playsOn On(id = team_id) GROUP BY NAME";
  59. getData(statement,2);
  60. }
  61. else if(Integer.parseInt(answer2) >= 0){
  62. System.out.println("tournament_name | number_of_participating_teams");
  63. String statement = "SELECT name,count(team_id) FROM participatesin INNER JOIN tournaments On(tournament_id = tournaments.id) GROUP BY name HAVING count(team_id) >=" + answer2;
  64. System.out.println(statement);
  65. getData(statement,2);
  66. }
  67.  
  68. }
  69. private static void getData(String statement,int col){
  70. try {
  71. Connection db = DriverManager.getConnection(url, username, password);
  72.  
  73.  
  74. Statement st = db.createStatement();
  75. ResultSet rs = st.executeQuery(statement);
  76. while (rs.next()) {
  77.  
  78. if (col == 2){
  79. System.out.print(rs.getString(1) + " | ");
  80. }
  81. System.out.println(rs.getString(col) + " ");
  82. }
  83. rs.close();
  84. st.close();
  85.  
  86.  
  87. } catch (Exception e) {
  88. System.out.println(e);
  89. }
  90. }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement