blooming8

Main autori

May 6th, 2022 (edited)
1,068
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. import java.io.FileNotFoundException;
  2. import java.io.IOException;
  3. import java.sql.*;
  4. import java.util.*;
  5.  
  6. public class Main
  7. {
  8.     public static void main(String[] args)
  9.     {
  10.         ArrayList<Autore> autori = new ArrayList();
  11.         Scanner scan = new Scanner(System.in);
  12.         HashMap<String, Libro> raccolta = new HashMap();
  13.        
  14.         try
  15.         {
  16.             Class.forName("com.mysql.jdbc.Driver");
  17.             Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/authors","root","");
  18.             Statement stmt = con.createStatement();  
  19.             ResultSet rs;
  20.             PreparedStatement ps;
  21.  
  22.             // LETTURA
  23.             String querySelect = "select * from Author";
  24.             rs = stmt.executeQuery(querySelect);
  25.             System.out.println("\n*** LISTA COMPLETA ***");
  26.             while (rs.next())
  27.             {  
  28.                 Autore autore = new Autore(rs.getInt("id"), rs.getString("Nome"), rs.getString("Cognome"));
  29.                 autori.add(autore);
  30.             }  
  31.            
  32.             // STAMPA AUTORI
  33.             for (Autore autore : autori)
  34.                 System.out.println(autore.toString());
  35.            
  36.             System.out.println("\n*** INSERIMENTO ***");
  37.            
  38.             if (!rs.next())
  39.             {
  40.                 // INSERIMENTO LIBRI
  41.                 for (Autore autore : autori)
  42.                 {
  43.                     System.out.printf("%s", autore.toString());
  44.                     System.out.println("\nInserire un libro per quest'autore? [s/n]");
  45.                     char scelta = scan.nextLine().charAt(0);
  46.                     if (scelta == 's')
  47.                     {
  48.                         do
  49.                         {
  50.                             System.out.println("\nTitolo: ");
  51.                             String titolo = scan.nextLine();
  52.                             System.out.println("\nAnno pubblicazione: ");
  53.                             int pubblicazione = scan.nextInt();
  54.        
  55.                             // INSERIMENTO NEL DB
  56.                             String queryInsert = "insert into Book (idAuthor, Titolo, Anno_pub) values (?, ?, ?)";
  57.                             ps = con.prepareStatement(queryInsert);
  58.                             ps.setInt(1, autore.getId());
  59.                             ps.setString(2, titolo);
  60.                             ps.setInt(3, pubblicazione);
  61.                             int n = ps.executeUpdate();
  62.                             System.out.printf("%d record inseriti", n);
  63.                                
  64.                             // INSERIMENTO HASHMAP
  65.                             raccolta.put(autore.getCognome(), new Libro(titolo, pubblicazione));
  66.                             System.out.println("\nInserire un altro libro? [s/n]");
  67.                             scan.skip("\\R?");
  68.                             scelta = scan.nextLine().charAt(0);
  69.                         }
  70.                         while (scelta == 's');
  71.                     }
  72.                     else if (scelta == 'n')
  73.                     {
  74.                         continue;
  75.                     }
  76.                     else
  77.                     {
  78.                         System.out.println("\nInput non valido.");
  79.                         break;
  80.                     }
  81.                 }
  82.             }
  83.             System.out.println("\nInserimento finito.");
  84.            
  85.            
  86.             // STAMPA TOTALE
  87.             System.out.println("\n***AUTORI - LIBRI***\n");
  88.             for (String cognome : raccolta.keySet())
  89.             {
  90.                 System.out.println("\n" + cognome + raccolta.get(cognome).toString() + "\n");
  91.             }
  92.            
  93.             // SCRITTURA SU FILE
  94.             FileManager filewr = new FileManager();
  95.             filewr.scrivi(raccolta);
  96.         }
  97.         catch (SQLException e)
  98.         {
  99.             e.printStackTrace();
  100.         }
  101.         catch (ClassNotFoundException e)
  102.         {
  103.             e.printStackTrace();
  104.         }
  105.         catch (FileNotFoundException e)
  106.         {
  107.             e.printStackTrace();
  108.         }
  109.         catch (IOException e)
  110.         {
  111.             e.printStackTrace();
  112.         }
  113.     }
  114. }
  115.  
  116.  
  117.  
Advertisement
Add Comment
Please, Sign In to add comment