Advertisement
Guest User

Untitled

a guest
Jul 12th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.21 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.*;
  3. public class Query {
  4.        private Scanner t, in, k;
  5.        //Attributi privati
  6.        private String url, username, password;
  7.        //Metodo Costruttore
  8.        public Query(){
  9.  
  10.            this.url = "jdbc:mysql://localhost:3306/prova?useSSL=false";
  11.            this.username = "root";
  12.            this.password = "home";
  13.        }
  14.        public void Selezione(){
  15.  
  16.            try {
  17.                // 1. Creo la connessione al DB
  18.                Connection myConn = DriverManager.getConnection(url,username,password);
  19.                
  20.                // 2.Creo lo Statement
  21.                Statement myStmt = myConn.createStatement();
  22.                k = new Scanner(System.in);
  23.                System.out.println("/////////////////////////////////////////////////////////////");
  24.                System.out.println("Premi 1 per visualizzare tutti i dati delle tabelle");
  25.                System.out.println("Premi 2 per ricercare dati in base alle tue esigenze");
  26.                System.out.println("/////////////////////////////////////////////////////////////");
  27.                int j = k.nextInt();
  28.                System.out.println();
  29.                System.out.println("Database prova -> Tabella studenti :");
  30.                System.out.println();
  31.                switch (j) {
  32.             case 1:
  33.                 // 3. Creo la query da eseguire
  34.                    ResultSet myRs = myStmt.executeQuery("SELECT * FROM studenti");
  35.                    
  36.                   // 4. Elaboro il risultato
  37.                    System.out.println("| idStudenti |   Nome    |     Cognome    |     Età   |");
  38.                    System.out.println("-------------------------------------------------------");
  39.                    while (myRs.next()) {
  40.                         System.out.println("\t" + myRs.getInt("idStudenti") + "\t" + myRs.getString("Nome")
  41.                                          + "    \t" + myRs.getString("Cognome") + "     \t" + myRs.getInt("Età"));
  42.                         }
  43.                 break;
  44.             case 2 :
  45.                     t = new Scanner(System.in);
  46.                     System.out.println("Inserire idStudenti");
  47.                     int y = t.nextInt();
  48.                     // 3. Eseguo le Query SQL
  49.                     /**
  50.                      * Questa query permette di visualizzare se gli elementi inseriti da tastiera sono presenti nel DB
  51.                      */
  52.                     myRs = myStmt.executeQuery("SELECT * FROM studenti WHERE idStudenti = '"+ y +"'");
  53.                     System.out.println();
  54.                     System.out.println("|       Nome    |     Cognome    |     Età   |");
  55.                     System.out.println("-------------------------------------------------------");
  56.                     // 4. Elaboro il risultato
  57.                     while (myRs.next()) {
  58.                         System.out.println("\t" + myRs.getString("Nome") + "     \t " + myRs.getString("Cognome") + "     \t " + myRs.getString("Età"));
  59.                     }
  60.                 break;
  61.             }
  62.              
  63.         }catch (Exception e) {
  64.             e.printStackTrace();
  65.         }
  66.          System.out.println();
  67.          System.out.println("Lettura Database completata!");
  68.          System.out.println();
  69.      }
  70.        
  71.        public void Inserimento(){
  72.             in = new Scanner(System.in);
  73.             t = new Scanner(System.in);
  74.            
  75.             System.out.println("Inserisci idStudenti : ");
  76.             int n = in.nextInt();
  77.             if((n == 0)|| (n >= 1)){
  78.                 System.out.println("L'idStudente deve essere un numero diverso da 0 e maggiore di 1");
  79.                 System.out.println();
  80.                 System.out.println("Inserimento annullato e programma terminato!");
  81.                 System.exit(0);
  82.             }
  83.            
  84.             System.out.println("Inserisci Nome : ");
  85.             String name = t.nextLine();
  86.            
  87.             System.out.println("Inserisci Cognome : ");
  88.             String surname = t.nextLine();
  89.            
  90.             System.out.println("Inserire Età : ");
  91.             int age = in.nextInt();
  92.            
  93.             try {
  94.                 // 1. Creo una connessione al Database
  95.                 Connection myConn = DriverManager.getConnection(url,username,password);
  96.                
  97.                 // 2. Creo uno Statement
  98.                 Statement myStmt = myConn.createStatement();
  99.                
  100.                 // 3. Eseguo le Query SQL
  101.                 /**
  102.                  * Questa query permette inserire nuovi dati nel DB
  103.                  */
  104.                 String sql = "INSERT INTO studenti " + "(idStudenti,Nome,Cognome,Età)" + "VALUES ('"+n+"','"+name+"','"+surname+"','"+age+"')";
  105.                
  106.                 myStmt.executeUpdate(sql);
  107.                
  108.                 System.out.println("Inserimento Completato!");
  109.                
  110.                
  111.             }catch (Exception exc) {
  112.                 //Il metodo printStackTrace() restituisce la sequenza di metodi invocati fino al punto in cui è avvenuta l'eccezione
  113.                 exc.printStackTrace();
  114.             }
  115.            
  116.            
  117.        }
  118.        
  119.        public void Elimina(){
  120.            String url = "jdbc:mysql://localhost:3306/prova?useSSL=false";
  121.             String user = "root";
  122.             String password = "home";
  123.             in = new Scanner(System.in);
  124.             System.out.println("Inserire l'idStudente in base al quale deve essere eliminata la riga : ");
  125.             int key = in.nextInt();
  126.             if(key == 0){
  127.                 System.out.println("Non puoi eliminare la riga con idStudente uguale a 0!");
  128.             }
  129.             try {
  130.                 // 1. Creo la connessione al DB
  131.                 Connection myConn = DriverManager.getConnection(url,user, password);
  132.                
  133.                 // 2. Creo lo Statement
  134.                 Statement myStmt = myConn.createStatement();
  135.                
  136.                 // 3. Eseguo le Query SQL
  137.                 String sql = "DELETE FROM studenti WHERE idStudenti = '" + key + "' ";
  138.                 int riga = myStmt.executeUpdate(sql);
  139.                
  140.                 System.out.println();
  141.                 System.out.println("N° di righe cancellate : " + riga);
  142.                 System.out.println();
  143.                 System.out.println("Eliminazione completata");
  144.                 System.out.println();
  145.                
  146.             } catch (Exception e) {
  147.                 e.printStackTrace();
  148.             }
  149.        }
  150.        
  151.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement