Advertisement
Guest User

Untitled

a guest
Jun 4th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.99 KB | None | 0 0
  1. o ja ebe java
  2.  
  3. odpowiedzi:
  4. Najmlodsza kobieta: Monika Szewczyk
  5. Najstarsza kobeita: Wiesława Tomaszewska
  6. Mezczyzna o najdluzszym nazwisku Janusz Andrzejewski oraz Adam Andrzejewski (dwie osoby o takim samym nazwisku)
  7. Ile kobiet: 48 Ile mezczyzn: 52
  8. Dwie wartosci srodkowe 43 42 czyli 42(wedlug kompilatora) wedlug mozgu 42.5
  9. Popularson miasto: Gniezno ile osob 2
  10. A tak naprawde nie ma jednego konkretnego jest kilka: Gniezno 2 Chorzów 2 Łochów 2 Blachownia 2 Żary 2 Sokółka 2
  11.  
  12. // klasa pomocnicza do zliczania miast
  13.  
  14. public class City {
  15. public String miasto;
  16. public int ile;
  17. public City(){
  18. miasto = "nic";
  19. ile = 0;
  20. }
  21.  
  22. }
  23.  
  24. // glowny kod
  25. import java.sql.*;
  26.  
  27. public class MySQL {
  28. // JDBC driver oraz database URL
  29. static final String DB_URL = "jdbc:mysql://ricky.heliohost.org/cackoa_people";
  30. static final String USER = "cackoa_user";
  31. static final String PASS = "cackoa_password";
  32.  
  33. public static void main(String[] args) {
  34. Connection conn = null;
  35. Statement stmt = null;
  36. try{
  37. // Połączenie z bazą danych
  38. System.out.println("Connecting to database...");
  39. conn = DriverManager.getConnection(DB_URL,USER,PASS);
  40. // Stworzenie zapytania oraz jego wykonanie
  41. System.out.println("Creating statement...");
  42. stmt = conn.createStatement();
  43. String sql;
  44. sql = "SELECT * FROM person JOIN parameters ON person.id = parameters.person_id";
  45. ResultSet rs = stmt.executeQuery(sql);
  46. long stara=999999;
  47. long mloda=0;
  48. char plec;
  49. long peselI;
  50. String omg;
  51. String najmlname = null;
  52. String najstname = null;
  53. int dlugosc =0;
  54. int ilek=0;
  55. int ilem=0;
  56. String najdluzszy= null;
  57. int[] tab = new int[100];
  58. int i=0;
  59. String lol;
  60. int wiek;
  61. int b= 0;
  62. City[] miasta = new City[100];
  63. for (int ha=0; ha <100;ha++){
  64. miasta[ha] = new City();
  65. }
  66. int ok =0;
  67. // Ekstrakcja wyników
  68. while(rs.next()){
  69. ok =0;
  70. //Retrieve by column name
  71. int id = rs.getInt("id");
  72. String name = rs.getString("name_surname");
  73. int person_id = rs.getInt("person_id");
  74. String pesel = rs.getString("pesel");
  75. String city = rs.getString("city");
  76. plec = pesel.charAt(9);
  77. if (plec == '0' || plec == '2' || plec == '4' || plec == '6' || plec == '8'){
  78. ilek++;
  79. omg = pesel.substring(0, 6);
  80. peselI = Integer.parseInt(omg);
  81. lol = pesel.substring(0, 2);
  82. wiek = Integer.parseInt(lol);
  83. if (pesel.charAt(2) == '2' || pesel.charAt(2) == '3'){
  84. tab[i] = 17 -wiek;
  85. if (peselI > mloda){
  86. mloda = peselI;
  87. najmlname = name;
  88. }
  89. }
  90. else if (pesel.charAt(2) == '0' || pesel.charAt(2) == '1') {
  91. tab[i] = 117 -wiek;
  92. if (peselI < stara){
  93. stara = peselI;
  94. najstname = name;
  95. }
  96. }
  97.  
  98. }
  99. if (plec == '1' || plec == '3' || plec == '5' || plec == '7' || plec == '9'){
  100. if (pesel.charAt(2) == '2' || pesel.charAt(2) == '3'){
  101. lol = pesel.substring(0, 2);
  102. wiek = Integer.parseInt(lol);
  103. tab[i] = 17 - wiek;
  104. }
  105. else if (pesel.charAt(2) == '0' || pesel.charAt(2) == '1') {
  106. lol = pesel.substring(0, 2);
  107. wiek = Integer.parseInt(lol);
  108. tab[i] = 117 - wiek;
  109. }
  110. ilem++;
  111. String[] split = name.split("\\s+");
  112. if (split[1].length()>dlugosc){
  113. dlugosc = split[1].length();
  114. najdluzszy = name;
  115. }
  116. }
  117. //Wyświetlenie danych:
  118. System.out.print(" ID: " + id);
  119. System.out.print(", name: " + name);
  120. System.out.print(" Pesel: " + pesel);
  121. System.out.print(" ID_person: " + person_id);
  122. System.out.println(" City: " + city);
  123. for (int j =0; j < 100; j++){
  124. if (miasta[j].miasto.equals(city)){
  125. miasta[j].ile++;
  126. ok =1;
  127. }
  128. }
  129. if (ok==0){
  130. miasta[b].miasto = city;
  131. miasta[b].ile++;
  132. b++;
  133. }
  134. i++;
  135. }
  136. System.out.println("Najmlodsza kobieta: " + najmlname);
  137. System.out.println("Najstarsza kobeita: " + najstname);
  138. System.out.println("Mezczyzna o najdluzszym nazwisku " + najdluzszy);
  139. System.out.println("Ile kobiet: " + ilek + " Ile mezczyzn: " + ilem);
  140. int pom=0;
  141. for(int g =0; g < 100; g++){
  142. for (int p =99; p > 0;p--){
  143. if (tab[p]<tab[p-1]){
  144. pom = tab[p-1];
  145. tab[p-1] = tab[p];
  146. tab[p] = pom;
  147. }
  148. }
  149. }
  150. for (int t =0; t < 100; t++){
  151. System.out.println(miasta[t].miasto + " " + miasta[t].ile);
  152. }
  153. String popularne_miasto;
  154. int ilema;
  155. popularne_miasto = miasta[0].miasto;
  156. ilema = miasta[0].ile;
  157. for (int t =0; t< 100; t++){
  158. if (ilema < miasta[t].ile){
  159. popularne_miasto = miasta[t].miasto;
  160. ilema = miasta[t].ile;
  161. }
  162. }
  163. System.out.println("Dwie wartosci srodkowe " + tab[50] + " " + tab[49] + " czyli " + ((tab[50] + tab[49])/2));
  164. System.out.println("Popularson miasto: " + popularne_miasto + " ile osob " + ilema);
  165. // Czyszczenie po sobie
  166. rs.close();
  167. stmt.close();
  168. conn.close();
  169. }catch(SQLException se){
  170. //Errory JDBC
  171. se.printStackTrace();
  172. }catch(Exception e){
  173. e.printStackTrace();
  174. }finally{
  175. //finally block - by pozamykać resources
  176. try{
  177. if(stmt!=null)
  178. stmt.close();
  179. }catch(SQLException se2){
  180. }// nothing we can do
  181. try{
  182. if(conn!=null)
  183. conn.close();
  184. }catch(SQLException se){
  185. se.printStackTrace();
  186. }//end finally try
  187. }//end try
  188. System.out.println("Koniec!");
  189.  
  190. }//end main
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement