Guest User

Untitled

a guest
Jan 21st, 2018
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.29 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.ArrayList;
  8. import java.util.Scanner;
  9.  
  10. public class TestDB {
  11.     public static void main(String[] args) throws SQLException, IOException {
  12.        
  13.         Scanner scanner = new Scanner(System.in);
  14.        
  15.         Connection connection = null;
  16.         ArrayList<String> re = null;
  17.         ResultSet Rs = null;
  18.         PreparedStatement stmt = null;
  19.          
  20.         try {
  21.             Class.forName("org.postgresql.Driver");
  22.             connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "admin");
  23.         } catch(Exception ex) {
  24.             System.out.println(ex.getMessage());
  25.         }
  26.        
  27.         Integer command;
  28.         Integer id = 133;
  29.        
  30.         do {  
  31.             // Wyswietlanie menu wyboru
  32.             System.out.println("---- MENU ----\n");  
  33.             System.out.println("1. Wyswietl wybrana tabele");
  34.             System.out.println("2. Wsywietl widok INFO_PRACOWNICY");
  35.             System.out.println("3. Dodaj pracownika");
  36.             System.out.println("4. Usun pracownika");
  37.             System.out.println("5. Wyjdz");
  38.             System.out.print("Wprowadz numer operacji: ");
  39.             command = scanner.nextInt();
  40.             // Przyjmowanie komendy od uzytkownika i wykonywanie odpowiedniej akcji
  41.             switch(command) {
  42.                 case 1: //Wyswietla wybrana tabele
  43.                     Integer command2 = null;
  44.                     System.out.println("Wybierz tabele do wyswietlenia");  
  45.                     System.out.println("1. Klienci");  
  46.                     System.out.println("2. Pracownicy");  
  47.                     System.out.println("3. Terminarz");
  48.                     System.out.println("4. Transport");
  49.                     System.out.println("5. Oddzialy");
  50.                     System.out.println("6. Cofnij");
  51.                     System.out.print("Wprowadz numer operacji: ");
  52.                     command2 = scanner.nextInt();
  53.                     switch(command2) {
  54.                         case 1:
  55.                             stmt = connection.prepareStatement("select * from klienci");
  56.                             Rs = stmt.executeQuery();
  57.                             System.out.println("KLIENCI");
  58.                             while(Rs.next()) {
  59.                                 System.out.println(Rs.getString(1) + " " + Rs.getString(2) + " " + Rs.getString(3) + " " + Rs.getString(4) + " " + Rs.getString(5) + " " + Rs.getString(6) + " " + Rs.getString(7));
  60.                             }
  61.                             System.out.println("");
  62.                             System.in.read();
  63.                             break;
  64.                         case 2:
  65.                             stmt = connection.prepareStatement("select * from pracownicy");
  66.                             Rs = stmt.executeQuery();
  67.                             System.out.println("PRACOWNICY");
  68.                             while(Rs.next()) {
  69.                                 System.out.println(Rs.getString(1) + " " + Rs.getString(2) + " " + Rs.getString(3) + " " + Rs.getString(4) + " " + Rs.getString(5) + " " + Rs.getString(6));
  70.                             }
  71.                             System.out.println("");
  72.                             System.in.read();
  73.                             break;
  74.                         case 3:
  75.                             stmt = connection.prepareStatement("select * from terminarz");
  76.                             Rs = stmt.executeQuery();
  77.                             System.out.println("TERMINARZ");
  78.                             while(Rs.next()) {
  79.                                 System.out.println(Rs.getString(1) + " " + Rs.getString(2) + " " + Rs.getString(3) + " " + Rs.getString(4) + " " + Rs.getString(5) + " " + Rs.getString(6));
  80.                             }
  81.                             System.out.println("");
  82.                             System.in.read();
  83.                             break;
  84.                         case 4:
  85.                             stmt = connection.prepareStatement("select * from transport");
  86.                             Rs = stmt.executeQuery();
  87.                             System.out.println("TRANSPORT");
  88.                             while(Rs.next()) {
  89.                                 System.out.println(Rs.getString(1) + " " + Rs.getString(2) + " " + Rs.getString(3));
  90.                             }
  91.                             System.out.println("");
  92.                             System.in.read();
  93.                             break;
  94.                         case 5:
  95.                             stmt = connection.prepareStatement("select * from oddzialy");
  96.                             Rs = stmt.executeQuery();
  97.                             System.out.println("ODDZIALY");
  98.                             while(Rs.next()) {
  99.                                 System.out.println(Rs.getString(1) + " " + Rs.getString(2));
  100.                             }
  101.                             System.out.println("");
  102.                             System.in.read();
  103.                             break;
  104.                         case 6:
  105.                             break;
  106.                     }
  107.                     break;
  108.                    
  109.                 case 2: // Wyswietla widok
  110.                     stmt = connection.prepareStatement("Select * from INFO_PRACOWNICY");
  111.                     System.out.println("INFO_PRACOWNICY");
  112.                     Rs = stmt.executeQuery();
  113.                     while(Rs.next()) {
  114.                         System.out.println(Rs.getString(1) + " " + Rs.getString(2) + " " + Rs.getString(3) + " " + Rs.getString(4) + " " + Rs.getString(5) + " " + Rs.getString(6));
  115.                     }
  116.                     System.out.println("");
  117.                     System.in.read();
  118.                     break;
  119.                    
  120.                 case 3: //Dodaje wiersz do tabeli pracownicy oraz dane_osobowe_pracownicy
  121.                     String imie, nazwisko, kontakt, stanowisko, data_urodzenia;
  122.                     int pesel, pensja, id_oddzial, kierownik_oddzialu, id2;
  123.                     Scanner terminalInput = new Scanner(System.in);
  124.                     System.out.println("Podaj imie:");
  125.                     imie = terminalInput.nextLine();
  126.                     System.out.println("Podaj nazwisko:");
  127.                     nazwisko = terminalInput.nextLine();
  128.                     System.out.println("Podaj date urodzenia (rrrr-mm-dd):");
  129.                     data_urodzenia = terminalInput.nextLine();
  130.                     System.out.println("Podaj numer telefonu:");
  131.                     kontakt = terminalInput.nextLine();
  132.                     System.out.println("Podaj stanowisko:");
  133.                     stanowisko = terminalInput.nextLine();
  134.                     System.out.println("Podaj pensje:");
  135.                     pensja = terminalInput.nextInt();
  136.                     System.out.println("Podaj id oddzialu (1-7):");
  137.                     id_oddzial = terminalInput.nextInt();
  138.                     System.out.println("Podaj id kierownika:");
  139.                     kierownik_oddzialu = terminalInput.nextInt();
  140.                     System.out.println("Podaj pesel:");
  141.                     pesel = terminalInput.nextInt();
  142.                     System.out.println("Podaj id:");
  143.                     id2 = terminalInput.nextInt();
  144.                     stmt = connection.prepareStatement("select add_pracownicy(?, ?, CAST(? AS DATE), ?, ?, ?, ?, ?, ?, ?)");
  145.                     stmt.setString(1, imie);
  146.                     stmt.setString(2, nazwisko);
  147.                     stmt.setString(3, data_urodzenia);
  148.                     stmt.setInt(4, pesel);
  149.                     stmt.setString(5, kontakt);
  150.                     stmt.setInt(6, id_oddzial);
  151.                     stmt.setInt(7, kierownik_oddzialu);
  152.                     stmt.setString(8, stanowisko);
  153.                     stmt.setInt(9, pensja);
  154.                     stmt.setInt(10, id2);
  155.                     stmt.executeQuery();
  156.                     System.out.println("Pracownik zostal dodany");
  157.                     PreparedStatement stmt2 = connection.prepareStatement("select * from pracownicy");
  158.                     Rs = stmt2.executeQuery();
  159.                     while(Rs.next()) {
  160.                         System.out.println(Rs.getString(1) + " " + Rs.getString(2) + " " + Rs.getString(3) + " " + Rs.getString(4) + " " + Rs.getString(5) + " " + Rs.getString(6));
  161.                     }
  162.                     System.in.read();
  163.                     break;
  164.  
  165.                 case 4: //usuwa pracownika o podanym id
  166.                     int id3;
  167.                     Scanner terminalInput2 = new Scanner(System.in);
  168.                     stmt = connection.prepareStatement("select delete_pracownicy(?)");
  169.                     id3 = terminalInput2.nextInt();
  170.                     stmt.setInt(1, id3);
  171.                     Rs = stmt.executeQuery();
  172.                     break;
  173.            
  174.                 case 5: // Wyjscie z aplikacji
  175.                     break;
  176.                    
  177.                    
  178.             }
  179.            
  180.            
  181.         }while(!command.equals(5));
  182.        
  183.        
  184.        
  185.         try {
  186.             connection.close();
  187.         } catch (Exception e) {
  188.             System.err.println("The connection can not be closed.");
  189.             e.printStackTrace();
  190.         }
  191.        
  192.     }
  193. }
Add Comment
Please, Sign In to add comment