Advertisement
Guest User

Untitled

a guest
Dec 1st, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. package populationdemo;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.Scanner;
  9.  
  10. public class PopulationDemo
  11. {
  12.  
  13. public static void main(String[] args)
  14. {
  15. String sql;
  16. char choice;
  17. final String DB_URL = "jdbc:mysql://localhost:3306/CityDB";
  18.  
  19. do
  20. {
  21. choice = menu();
  22. switch(choice)
  23. {
  24. case 'A':
  25. a(DB_URL);
  26. break;
  27. case 'a':
  28. a(DB_URL);
  29. break;
  30. case 'B':
  31. b(DB_URL);
  32. break;
  33. case 'b':
  34. b(DB_URL);
  35. break;
  36. case 'C':
  37. c(DB_URL);
  38. break;
  39. case 'c':
  40. c(DB_URL);
  41. break;
  42. }
  43. } while(choice != '0');
  44. }
  45.  
  46. public static char menu()
  47. {
  48. System.out.println();
  49. System.out.println("A. Sort the list of cities by population, in ascending order");
  50. System.out.println("B. Get the total population of all the cities");
  51. System.out.println("C. Get the lowest population");
  52. System.out.println("Choose an option: ");
  53.  
  54. Scanner keyboard = new Scanner(System.in);
  55. char choice = keyboard.nextLine().charAt(0);
  56. return choice;
  57. }
  58.  
  59. public static void a(String DB_URL)
  60. {
  61. String sql = "Select * from city order by population desc";
  62. try
  63. {
  64. Connection conn = DriverManager.getConnection(DB_URL,"root","");
  65. Statement stmt = conn.createStatement();
  66. ResultSet rs;
  67. rs = stmt.executeQuery(sql);
  68.  
  69. System.out.printf("%-20s %-20s\n",
  70. "City", "Population");
  71. while(rs.next())
  72. {
  73. System.out.printf("%-20s %-20s\n",
  74. rs.getString("CityName"),
  75. rs.getString("Population"));
  76. }
  77. }
  78. catch(SQLException ex)
  79. {
  80. System.out.println("ERROR: " + ex.getMessage());
  81. }
  82. }
  83.  
  84. public static void b(String DB_URL)
  85. {
  86. String sql = "Select population, count(population) FROM city";
  87. try
  88. {
  89. Connection conn = DriverManager.getConnection(DB_URL,"root","");
  90. Statement stmt = conn.createStatement();
  91. ResultSet rs;
  92. rs = stmt.executeQuery(sql);
  93.  
  94. System.out.printf("%-20s\n",
  95. "Total Population");
  96. while(rs.next())
  97. {
  98. System.out.printf("%-20s\n",
  99. rs.getString("Population"));
  100. }
  101. }
  102. catch(SQLException ex)
  103. {
  104. System.out.println("ERROR: " + ex.getMessage());
  105. }
  106. }
  107.  
  108. public static void c(String DB_URL)
  109. {
  110. String sql = "Select CityName, Min(Population) AS Population from City";
  111. try
  112. {
  113. Connection conn = DriverManager.getConnection(DB_URL,"root","");
  114. Statement stmt = conn.createStatement();
  115. ResultSet rs;
  116. rs = stmt.executeQuery(sql);
  117.  
  118. System.out.printf("%-20s %-20s\n",
  119. "City", "Population");
  120. while(rs.next())
  121. {
  122. System.out.printf("%-20s %-20s\n",
  123. rs.getString("CityName"),
  124. rs.getString("Population"));
  125. }
  126. }
  127. catch(SQLException ex)
  128. {
  129. System.out.println("ERROR: " + ex.getMessage());
  130. }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement