Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Person implements Comparable<Person>{
- public int id;
- public int person_id;
- public String pesel;
- public String city;
- public String name_surname;
- public Person(int i, int pi, String pes, String c, String nam) {
- id=i;
- person_id=pi;
- pesel=pes;
- city=c;
- name_surname=nam;
- }
- public int compareTo(Person o) {
- return pesel.compareTo(o.pesel);
- }
- }
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.StringTokenizer;
- public class Main {
- public static void main(String[] args) {
- List<Person> lista = new ArrayList<Person>();
- Connection connection = null;
- java.sql.Statement stmt = null;
- String DB_URL = "jdbc:mysql://ricky.heliohost.org/cackoa_people";
- String USER = "cackoa_user";
- String PASS = "cackoa_password";
- String driverName = "com.mysql.jdbc.Driver";
- String miasto = null;
- try {
- Class.forName(driverName);
- connection = DriverManager.getConnection(DB_URL, USER, PASS);
- stmt = connection.createStatement();
- String sql;
- ResultSet rs;
- sql = "SELECT p.id, p.person_id, p.pesel, p.city, c.name_surname FROM parameters p JOIN person c ON c.id=p.person_id";
- rs = stmt.executeQuery(sql);
- while (rs.next()) {
- // Retrieve by column name
- int id = rs.getInt("id");
- int person_id = rs.getInt("person_id");
- String pesel = rs.getString("pesel");
- String city = rs.getString("city");
- String name_surname = rs.getString("name_surname");
- // Wyświetlenie danych:
- // System.out.print(id + "\t" + person_id + "\t" + pesel + "\t"
- // + city + "\t\t\t" + name_surname + "\n");
- lista.add(new Person(id, person_id, pesel, city, name_surname));
- }
- sql = "SELECT p.city, count(*) AS liczba FROM parameters p GROUP BY p.city ORDER BY liczba DESC LIMIT 1";
- rs = stmt.executeQuery(sql);
- rs.next();
- miasto = rs.getString("city");
- rs.close();
- stmt.close();
- connection.close();
- } catch (ClassNotFoundException e) {
- System.out.println("brak sterownika\n" + e.getMessage());
- } catch (SQLException e) {
- System.out.println("Nie można się polaczyc\n" + e.getMessage());
- }
- Collections.sort(lista);
- int maxw=1;
- Person max_wiek = lista.get(0);
- while(max_wiek.pesel.charAt(max_wiek.pesel.length()-2)!='0' &&
- max_wiek.pesel.charAt(max_wiek.pesel.length()-2)!='2' &&
- max_wiek.pesel.charAt(max_wiek.pesel.length()-2)!='4' &&
- max_wiek.pesel.charAt(max_wiek.pesel.length()-2)!='6' &&
- max_wiek.pesel.charAt(max_wiek.pesel.length()-2)!='8')
- {
- max_wiek = lista.get(maxw);
- maxw++;
- }
- Person min_wiek = lista.get(lista.size() - 1);
- int minw = lista.size()-2;
- while(min_wiek.pesel.charAt(min_wiek.pesel.length()-2)!='0' &&
- min_wiek.pesel.charAt(min_wiek.pesel.length()-2)!='2' &&
- min_wiek.pesel.charAt(min_wiek.pesel.length()-2)!='4' &&
- min_wiek.pesel.charAt(min_wiek.pesel.length()-2)!='6' &&
- min_wiek.pesel.charAt(min_wiek.pesel.length()-2)!='8')
- {
- min_wiek=lista.get(minw);
- minw--;
- }
- int kobiaty = 0;
- int mezczyzni = 0;
- for (Person a : lista) {
- String pom = a.pesel;
- char pom2 = pom.charAt(pom.length() - 1);
- if (pom2 == '0' || pom2 == '2' || pom2 == '4' || pom2 == '6' || pom2 == '8') {
- kobiaty++;
- } else {
- mezczyzni++;
- }
- // System.out.print(a.id + "\t" + a.person_id + "\t" + a.pesel +
- // "\t" + a.city + "\t\t\t" + a.name_surname + "\n");
- }
- //////////////////////////////////////////////////////////////////////////////////
- Person z = lista.get(0);
- int pom = 1;
- while(z.pesel.charAt(z.pesel.length()-2)!='1' &&
- z.pesel.charAt(z.pesel.length()-2)!='3' &&
- z.pesel.charAt(z.pesel.length()-2)!='5' &&
- z.pesel.charAt(z.pesel.length()-2)!='7' &&
- z.pesel.charAt(z.pesel.length()-2)!='9' )
- {
- z=lista.get(pom);
- pom++;
- }
- StringTokenizer stringTokenizer = new StringTokenizer(z.name_surname);
- String pimie=stringTokenizer.nextToken();
- String pnazwisko=stringTokenizer.nextToken();
- int maxnaz=pnazwisko.length();
- for(Person a : lista)
- {
- if(a.pesel.charAt(a.pesel.length()-2)=='1' ||
- a.pesel.charAt(a.pesel.length()-2)=='3' ||
- a.pesel.charAt(a.pesel.length()-2)=='5' ||
- a.pesel.charAt(a.pesel.length()-2)=='7' ||
- a.pesel.charAt(a.pesel.length()-2)=='9' )
- {
- StringTokenizer Tokenizer = new StringTokenizer(a.name_surname);
- String pim=Tokenizer.nextToken();
- String pnaz=Tokenizer.nextToken();
- if(pnaz.length()>maxnaz)
- {
- //System.out.println(pnaz);
- z=a;
- maxnaz=pnaz.length();
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////
- System.out.println("Najmłodsza kobieta:\t" + min_wiek.name_surname + "\t" + min_wiek.pesel);
- System.out.println("Najstarsza kobieta:\t" + max_wiek.name_surname + "\t" + max_wiek.pesel);
- System.out.println("Mężczyzna z najdłuższym nazwiskiem:\t"+z.name_surname+" [ " + maxnaz +" ]");
- System.out.println("Liczba mezczyzn:\t" + mezczyzni);
- System.out.println("Liczba kobiet:\t" + kobiaty);
- int mediana;
- String z1 = lista.get((lista.size() / 2) - 1).pesel;
- String z2 = lista.get(lista.size() / 2).pesel;
- z1 = z1.substring(0, 2);
- z2 = z2.substring(0, 2);
- int x1 = (int) Double.parseDouble(z1);
- int x2 = (int) Double.parseDouble(z2);
- x1 = 117 - x1;
- x2 = 117 - x2;
- mediana = (x1 + x2) / 2;
- System.out.println("Mediana wieku: " + mediana);
- System.out.println("Najwięcej osób pochodzi z miasta: " + miasto);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement