Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- o ja ebe java
- odpowiedzi:
- Najmlodsza kobieta: Monika Szewczyk
- Najstarsza kobeita: Wiesława Tomaszewska
- Mezczyzna o najdluzszym nazwisku Janusz Andrzejewski oraz Adam Andrzejewski (dwie osoby o takim samym nazwisku)
- Ile kobiet: 48 Ile mezczyzn: 52
- Dwie wartosci srodkowe 43 42 czyli 42(wedlug kompilatora) wedlug mozgu 42.5
- Popularson miasto: Gniezno ile osob 2
- A tak naprawde nie ma jednego konkretnego jest kilka: Gniezno 2 Chorzów 2 Łochów 2 Blachownia 2 Żary 2 Sokółka 2
- // klasa pomocnicza do zliczania miast
- public class City {
- public String miasto;
- public int ile;
- public City(){
- miasto = "nic";
- ile = 0;
- }
- }
- // glowny kod
- import java.sql.*;
- public class MySQL {
- // JDBC driver oraz database URL
- static final String DB_URL = "jdbc:mysql://ricky.heliohost.org/cackoa_people";
- static final String USER = "cackoa_user";
- static final String PASS = "cackoa_password";
- public static void main(String[] args) {
- Connection conn = null;
- Statement stmt = null;
- try{
- // Połączenie z bazą danych
- System.out.println("Connecting to database...");
- conn = DriverManager.getConnection(DB_URL,USER,PASS);
- // Stworzenie zapytania oraz jego wykonanie
- System.out.println("Creating statement...");
- stmt = conn.createStatement();
- String sql;
- sql = "SELECT * FROM person JOIN parameters ON person.id = parameters.person_id";
- ResultSet rs = stmt.executeQuery(sql);
- long stara=999999;
- long mloda=0;
- char plec;
- long peselI;
- String omg;
- String najmlname = null;
- String najstname = null;
- int dlugosc =0;
- int ilek=0;
- int ilem=0;
- String najdluzszy= null;
- int[] tab = new int[100];
- int i=0;
- String lol;
- int wiek;
- int b= 0;
- City[] miasta = new City[100];
- for (int ha=0; ha <100;ha++){
- miasta[ha] = new City();
- }
- int ok =0;
- // Ekstrakcja wyników
- while(rs.next()){
- ok =0;
- //Retrieve by column name
- int id = rs.getInt("id");
- String name = rs.getString("name_surname");
- int person_id = rs.getInt("person_id");
- String pesel = rs.getString("pesel");
- String city = rs.getString("city");
- plec = pesel.charAt(9);
- if (plec == '0' || plec == '2' || plec == '4' || plec == '6' || plec == '8'){
- ilek++;
- omg = pesel.substring(0, 6);
- peselI = Integer.parseInt(omg);
- lol = pesel.substring(0, 2);
- wiek = Integer.parseInt(lol);
- if (pesel.charAt(2) == '2' || pesel.charAt(2) == '3'){
- tab[i] = 17 -wiek;
- if (peselI > mloda){
- mloda = peselI;
- najmlname = name;
- }
- }
- else if (pesel.charAt(2) == '0' || pesel.charAt(2) == '1') {
- tab[i] = 117 -wiek;
- if (peselI < stara){
- stara = peselI;
- najstname = name;
- }
- }
- }
- if (plec == '1' || plec == '3' || plec == '5' || plec == '7' || plec == '9'){
- if (pesel.charAt(2) == '2' || pesel.charAt(2) == '3'){
- lol = pesel.substring(0, 2);
- wiek = Integer.parseInt(lol);
- tab[i] = 17 - wiek;
- }
- else if (pesel.charAt(2) == '0' || pesel.charAt(2) == '1') {
- lol = pesel.substring(0, 2);
- wiek = Integer.parseInt(lol);
- tab[i] = 117 - wiek;
- }
- ilem++;
- String[] split = name.split("\\s+");
- if (split[1].length()>dlugosc){
- dlugosc = split[1].length();
- najdluzszy = name;
- }
- }
- //Wyświetlenie danych:
- System.out.print(" ID: " + id);
- System.out.print(", name: " + name);
- System.out.print(" Pesel: " + pesel);
- System.out.print(" ID_person: " + person_id);
- System.out.println(" City: " + city);
- for (int j =0; j < 100; j++){
- if (miasta[j].miasto.equals(city)){
- miasta[j].ile++;
- ok =1;
- }
- }
- if (ok==0){
- miasta[b].miasto = city;
- miasta[b].ile++;
- b++;
- }
- i++;
- }
- System.out.println("Najmlodsza kobieta: " + najmlname);
- System.out.println("Najstarsza kobeita: " + najstname);
- System.out.println("Mezczyzna o najdluzszym nazwisku " + najdluzszy);
- System.out.println("Ile kobiet: " + ilek + " Ile mezczyzn: " + ilem);
- int pom=0;
- for(int g =0; g < 100; g++){
- for (int p =99; p > 0;p--){
- if (tab[p]<tab[p-1]){
- pom = tab[p-1];
- tab[p-1] = tab[p];
- tab[p] = pom;
- }
- }
- }
- for (int t =0; t < 100; t++){
- System.out.println(miasta[t].miasto + " " + miasta[t].ile);
- }
- String popularne_miasto;
- int ilema;
- popularne_miasto = miasta[0].miasto;
- ilema = miasta[0].ile;
- for (int t =0; t< 100; t++){
- if (ilema < miasta[t].ile){
- popularne_miasto = miasta[t].miasto;
- ilema = miasta[t].ile;
- }
- }
- System.out.println("Dwie wartosci srodkowe " + tab[50] + " " + tab[49] + " czyli " + ((tab[50] + tab[49])/2));
- System.out.println("Popularson miasto: " + popularne_miasto + " ile osob " + ilema);
- // Czyszczenie po sobie
- rs.close();
- stmt.close();
- conn.close();
- }catch(SQLException se){
- //Errory JDBC
- se.printStackTrace();
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- //finally block - by pozamykać resources
- try{
- if(stmt!=null)
- stmt.close();
- }catch(SQLException se2){
- }// nothing we can do
- try{
- if(conn!=null)
- conn.close();
- }catch(SQLException se){
- se.printStackTrace();
- }//end finally try
- }//end try
- System.out.println("Koniec!");
- }//end main
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement