Advertisement
Guest User

3 Destinatii Turistice

a guest
May 18th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.52 KB | None | 0 0
  1. package preg;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10.  
  11. import com.mysql.jdbc.Statement;
  12.  
  13. public class Ex3SQL {
  14.  
  15.     public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException,
  16.             SQLException, IOException {
  17.  
  18.         Class.forName("com.mysql.jdbc.Driver").newInstance();
  19.         String url = "jdbc:mysql://localhost:3306/test";
  20.         Connection con = DriverManager.getConnection(url, "root", null);
  21.         // probabil va fi (url, "root", "root") la test, eu n-am parola la
  22.         // server
  23.  
  24.         Statement sql;
  25.         sql = (Statement) con.createStatement();
  26.         ResultSet rs;
  27.         // ResultSet folosit doar la comenzile de Select. Comanda
  28.         // executeUpdateQuery returneaza int si poate fi apelata pur si simplu
  29.  
  30.         int o = 0;
  31.  
  32.         BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
  33.         do {
  34.             System.out.println("------MENIU------");
  35.             System.out.println("1. Afisare");
  36.             System.out.println("2. Adaugare destinatie");
  37.             System.out.println("3. Actualizare nr obiective pt o statiune data");
  38.             System.out.println("4. Iesire");
  39.             System.out.println("Optiune: ");
  40.             try {
  41.                 o = Integer.parseInt(f.readLine());
  42.             } catch (NumberFormatException e) {
  43.                 System.out.println("Introduceti un numar! Eroare " + e);
  44.                 e.printStackTrace();
  45.             } catch (IOException e) {
  46.                 e.printStackTrace();
  47.             }
  48.             System.out.println();
  49.  
  50.             switch (o) {
  51.             case 1:
  52.                 rs = sql.executeQuery("select * from destinatii_turistice");
  53.                 while (rs.next())
  54.                     System.out.println(rs.getString("tara") + " " + rs.getString("statiune") + " "
  55.                             + rs.getInt("nr_obiective"));
  56.                 System.out.println();
  57.                 break;
  58.             case 2:
  59.                 String tara,
  60.                 statiune;
  61.  
  62.                 int nr = 0;
  63.                 System.out.println("Tara: ");
  64.                 tara = f.readLine();
  65.                 System.out.println("Statiunea: ");
  66.                 statiune = f.readLine();
  67.                 System.out.println("Numar obiective: ");
  68.                 try {
  69.                     nr = Integer.valueOf(f.readLine());
  70.                 } catch (NumberFormatException e) {
  71.                     System.out.println("EROARE: Introduceti un numar! " + e);
  72.                     e.printStackTrace();
  73.                 }
  74.  
  75.                 if (nr != 0)
  76.                     // executeUpdateQuery returneaza int cu nr de inregistrari
  77.                     // modificate asa ca am pus o
  78.                     // verificare:
  79.                     if (sql.executeUpdate("insert into destinatii_turistice (id, tara, statiune, nr_obiective) values (NULL, \""
  80.                             + tara + "\", \"" + statiune + "\", \"" + nr + "\")") != 0)
  81.                         System.out.println("Destinatia a fost introdusa in tabel!");
  82.                     else
  83.                         // Daca nr ramane 0 cum a fost declarat mai sus...
  84.                         System.out.println("Atentie! Destinatia NU a fost introdusa in tabel!");
  85.                 break;
  86.             case 3:
  87.                 int nr1 = 0;
  88.  
  89.                 System.out.println("Statiunea: ");
  90.                 statiune = f.readLine();
  91.                 System.out.println("Numar obiective: ");
  92.                 try {
  93.                     nr1 = Integer.valueOf(f.readLine());
  94.                 } catch (NumberFormatException e) {
  95.                     System.out.println("EROARE: Introduceti un numar! " + e);
  96.                     e.printStackTrace();
  97.                 }
  98.  
  99.                 if (nr1 != 0)
  100.                     if (sql.executeUpdate("update destinatii_turistice set nr_obiective=" + nr1 + " where statiune=\""
  101.                             + statiune + "\"") != 0)
  102.                         System.out.println("Numarul de obiective turistice pentru statiunea " + statiune
  103.                                 + " a fost modificat la " + nr1 + "!");
  104.                 break;
  105.             case 4:
  106.                 break;
  107.             default:
  108.                 System.out.println("Optiune invalida.");
  109.             }
  110.         } while (o != 4);
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement