Advertisement
Guest User

Untitled

a guest
Sep 17th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. package dio1_priprema;
  2.  
  3. import java.sql.*;
  4.  
  5. public class PripremaWorld {
  6. public static void main(String[] args) throws Exception {
  7.  
  8. java.util.Scanner input = new java.util.Scanner(System.in);
  9. String query = "";
  10. getConnection();
  11.  
  12. // exception handling
  13. try {
  14. // prompting user to start search
  15. System.out
  16. .println("Enter 1 to search the information about a particular city.\nEnter 2 to see the information in a particular country.\nEnter 3 for the information about a particular language.\nEnter 4 to exit Search. ");
  17. int check = input.nextInt();
  18. // Prompting user to enter proper value
  19. if (check < 1 && check > 4) {
  20. System.out
  21. .println("You have entered too small value, try again with input bigger then 0 and smaller then 5.");
  22. check = input.nextInt();
  23. }
  24. // invoking CheckUp
  25. CheckUp(check, query);
  26.  
  27. } catch (SQLException e) {
  28. System.out.println("Exception ocured.");
  29.  
  30. } catch (Exception e) {
  31.  
  32. } finally {
  33. input.close();
  34.  
  35. }
  36.  
  37. }
  38.  
  39. public static Connection getConnection() throws Exception {
  40.  
  41. try {
  42. String driver = "com.mysql.jdbc.Driver";
  43. String url = "jdbc:mysql://localhost:3306/world";
  44. String username = "root";
  45. String password = "kada";
  46. Class.forName(driver);
  47.  
  48. Connection conn = DriverManager.getConnection(url, username,
  49. password);
  50. System.out.println("Connected");
  51. return conn;
  52.  
  53. } catch (Exception e) {
  54. System.out.println(e);
  55. }
  56.  
  57. return null;
  58.  
  59. }
  60.  
  61. // Method for inputing String line
  62. public static String nextLine() {
  63. java.util.Scanner input2 = new java.util.Scanner(System.in);
  64. String argument = input2.nextLine();
  65. input2.close();
  66. return argument;
  67. }
  68.  
  69. public static void CheckUp(int check, String query) throws SQLException {
  70. // Creating connection path
  71. Connection con = DriverManager.getConnection(
  72. "jdbc:mysql://localhost:3306/world", "root", "kada");
  73. // creating statement to execute query
  74. Statement statement = con.createStatement();
  75. if (check == 1) {
  76. // search through city table
  77. System.out
  78. .println("Enter the name of the city you are interested in or code of the state \n or area where the city is located or population:");
  79. String city = nextLine();
  80.  
  81. query = "SELECT * FROM CITY WHERE Name = '" + city
  82. + "' OR CountryCode = '" + city + "' OR District = '"
  83. + city + "' OR Population = '" + city + "';";
  84.  
  85. // Executing query
  86. ResultSet feedback = statement.executeQuery(query);
  87.  
  88. while (feedback.next()) {
  89.  
  90. System.out.println(String.format("%-26s ", "Code :")
  91. + feedback.getString("CountryCode"));
  92. System.out.println(String.format("%-26s", "Name :")
  93. + feedback.getString("Name"));
  94. System.out.println(String.format("%-26s", "District : ")
  95. + feedback.getString("District"));
  96. System.out.println(String
  97. .format("%-26s", "Total population : ")
  98. + feedback.getString("Population"));
  99.  
  100. System.out.println();
  101.  
  102. }
  103.  
  104. } else if (check == 2) {
  105.  
  106. // search through Country table
  107. System.out
  108. .println("Enter the name of the state that interests you \n or the code off thr state or the name of the continent where the state is \n or the name of the President. ");
  109. String drzava = nextLine();
  110.  
  111. query = "SELECT * FROM Country WHERE Name ='" + drzava
  112. + "' OR Code='" + drzava + "' OR Continent ='" + drzava
  113. + "' OR HeadOfState='" + drzava + "';";
  114.  
  115. // Executing query
  116. ResultSet feedback = statement.executeQuery(query);
  117.  
  118. while (feedback.next()) {
  119.  
  120. System.out.println(String.format("%-26s",
  121. "Name of the state : ") + feedback.getString("Name"));
  122. System.out.println(String.format("%-26s", "Code : ")
  123. + feedback.getString("Code"));
  124. System.out.println(String.format("%-26s", "Continent : ")
  125. + feedback.getString("Continent"));
  126. System.out.println(String.format("%-26s", "Region : ")
  127. + feedback.getString("Region"));
  128. System.out.println(String.format("%-26s", "Area : ")
  129. + feedback.getString("SurfaceArea"));
  130. System.out.println(String.format("%-26s",
  131. "Independent since : ")
  132. + feedback.getString("IndepYear"));
  133. System.out.println(String.format("%-26s", "Population : ")
  134. + feedback.getString("Population"));
  135. System.out.println(String.format("%-26s",
  136. "Average life expectancy : ")
  137. + feedback.getString("LifeExpectancy"));
  138. System.out.println(String.format("%-26s", " The President : ")
  139. + feedback.getString("HeadOfState"));
  140. System.out.println();
  141.  
  142. }
  143.  
  144. } else if (check == 3) {
  145. // search through Language table
  146. System.out
  147. .println("Enter eng. the name of the language of interest, \n or code of the state.");
  148. String language = nextLine();
  149.  
  150. query = "SELECT * FROM countrylanguage WHERE Language = '"
  151. + language + "' OR CountryCode= '" + language + "' ;";
  152.  
  153. // Executing query
  154. ResultSet feedback = statement.executeQuery(query);
  155.  
  156. while (feedback.next()) {
  157.  
  158. System.out.println(String.format("%-26s", "Country code : ")
  159. + feedback.getString("CountryCode"));
  160. System.out.println(String.format("%-26s", "Language : ")
  161. + feedback.getString("Language"));
  162. System.out.println(String.format("%-26s", " Is official: ")
  163. + feedback.getString("IsOfficial"));
  164. System.out.println(String.format("%-26s",
  165. "Percentage of representation: ")
  166. + feedback.getString("Percentage"));
  167. System.out.println();
  168.  
  169. }
  170.  
  171. } else if (check == 4) {
  172.  
  173. // exiting search
  174. System.exit(1);
  175.  
  176. }
  177.  
  178. }
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement