Advertisement
Guest User

Untitled

a guest
Sep 13th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.40 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package telkom;
  6.  
  7. // Import all necessary packages
  8. import java.sql.*;
  9. import java.util.Scanner;
  10.  
  11. /**
  12.  *
  13.  * @author Kuba Sejdak i Tomasz Figa
  14.  */
  15. public class DataBase
  16. {
  17.     public DataBase()
  18.     {
  19.         try
  20.         {
  21.             Class.forName("oracle.jdbc.driver.OracleDriver");
  22.         }
  23.         catch(ClassNotFoundException e )
  24.         {
  25.             System.err.print("ERROR: Could not load database driver -> ");
  26.             System.err.println(e.getMessage());
  27.             System.exit(1);
  28.         }
  29.        
  30.         in = new Scanner(System.in);
  31.     }
  32.    
  33.     public void connect()
  34.     {
  35.         String uri = "jdbc:oracle:thin:@//ikar.elka.pw.edu.pl:1521/elka.elka.pw.edu.pl";
  36.         String user = "jsejdak";
  37.         String password = "jsejdak";
  38.        
  39.         try
  40.         {
  41.             dbConnection = DriverManager.getConnection(uri, user, password);
  42.         }
  43.         catch(SQLException e)
  44.         {
  45.             System.err.print("ERROR: Could not connect to database -> ");
  46.             System.err.println(e.getMessage());
  47.             System.exit(1);
  48.         }
  49.     }
  50.    
  51.     public void disconnect()
  52.     {
  53.         try
  54.         {
  55.             if(dbConnection != null)
  56.                 dbConnection.close();
  57.         }
  58.         catch(SQLException e)
  59.         {
  60.             System.err.print("ERROR: could not disconnect from database -> ");
  61.             System.err.println(e.getMessage());
  62.         }
  63.     }
  64.    
  65.     public void addClient()
  66.     {
  67.         System.out.println("---- Dodawanie klientów do bazy: ----\n");
  68.        
  69.         System.out.println("Podaj imię klienta:");
  70.         String firstName = in.next();
  71.         System.out.println("Podaj nazwisko klienta:");
  72.         String lastName = in.next();
  73.         System.out.println("Podaj adres klienta:");
  74.         String address = in.next();
  75.         System.out.println("Podaj region klienta:");
  76.         String area = in.next();
  77.        
  78.         try
  79.         {
  80.             Statement s = dbConnection.createStatement();
  81.        
  82.             s.executeQuery("INSERT INTO klienci VALUES (" +
  83.                            "ID_KLIENTA_SEQ.nextval, " +
  84.                            lastName + "," +
  85.                            firstName + "," +
  86.                            address + "," +
  87.                            area + ")");
  88.        
  89.             s.close();
  90.         }
  91.         catch(SQLException e)
  92.         {
  93.             System.out.println("ERROR: cannot add client. " + e.getMessage());
  94.         }
  95.     }
  96.    
  97.     public void selectClient()
  98.     {
  99.         System.out.println();
  100.         System.out.println("Podaj imię i nazwisko klienta:");
  101.         String firstName = in.next();
  102.         String lastName = in.next();
  103.         System.out.println();
  104.        
  105.         try
  106.         {
  107.             Statement s = dbConnection.createStatement();
  108.  
  109.             ResultSet rset1 = s.executeQuery("SELECT * FROM klienci WHERE " +
  110.                             "imie = '" + firstName + "' AND nazwisko = '" + lastName +"'");
  111.            
  112.             if(rset1.next())
  113.             {
  114.                 System.out.println("---- Dane klienta: ----");
  115.                 System.out.println("Id      : " + rset1.getString(1));
  116.                 System.out.println("Imię    : " + rset1.getString(3));
  117.                 System.out.println("Nazwisko: " + rset1.getString(2));
  118.                 System.out.println("Adres   : " + rset1.getString(4));
  119.                 System.out.println("Region  : " + rset1.getString(5));
  120.             }
  121.             else
  122.             {
  123.                 rset1.close();
  124.                 s.close();
  125.                 System.out.println("Brak wyników!");
  126.                 return;
  127.             }
  128.            
  129.             ResultSet rset2 = s.executeQuery("SELECT id_umowy, poczatek, koniec " +
  130.                     "FROM klienci NATURAL JOIN umowy WHERE " +
  131.                     "imie = '" + firstName + "' AND nazwisko = '" + lastName +"'");
  132.  
  133.             if(rset2.next())
  134.             {
  135.                 System.out.println("---- Umowy klienta: ----");
  136.                 System.out.println("Id      : " + rset2.getString(1));
  137.                 System.out.println("Początek: " + rset2.getString(2));
  138.                 System.out.println("Koniec  : " + rset2.getString(3));
  139.                 System.out.println("-----------------------\n");
  140.             }
  141.             else
  142.             {
  143.                 rset2.close();
  144.                 s.close();
  145.                 System.out.println("Brak wyników!");
  146.                 return;
  147.             }
  148.            
  149.             rset1.close();
  150.             rset2.close();
  151.             s.close();
  152.         }
  153.         catch(SQLException e)
  154.         {
  155.             System.out.println("ERROR: cannot show client. " + e.getMessage());
  156.         }
  157.        
  158.         modifyClient();
  159.     }
  160.    
  161.     private void modifyClient()
  162.     {
  163.         loop:
  164.         while(true)
  165.         {
  166.             System.out.println("Co chcesz zrobić?\n");
  167.             System.out.println("1 - edytuj klienta");
  168.             System.out.println("2 - usuń klienta");
  169.             System.out.println("3 - pokaż dane klienta");
  170.             System.out.println("4 - dodaj umowę");
  171.             System.out.println("0 - powrót");
  172.  
  173.             int choice = in.nextInt();
  174.            
  175.             switch(choice)
  176.             {
  177.             case 1:
  178.                 break;
  179.             case 2:
  180.                 break;
  181.             case 0:
  182.                 break loop;
  183.             default:
  184.                 System.out.println("Niepoprawny wybór!");
  185.                 break;
  186.             } /* switch */
  187.         } /* while */
  188.     }
  189.    
  190.     private Connection dbConnection = null;
  191.     Scanner in;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement