Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // *********************************************************************
- // *********************** ESERCIZIO CIMINO ****************************
- // *********************************************************************
- // *********************************************************************
- // *********************************************************************
- //XXXXXXXXXXXXXXXXXXXXXXX TIPOLOGIA SERVER ANELLO XXXXXXXXXXXXXXXXXXXXXX
- // *********************FUNZIONE INVIA (SOCKET)*************************
- // CASO NON SERIALIZABLE (HO IL METODO ToString() E il cotruttore X(String Xml)
- public static void invia(int portaInvio, Domanda c){
- try( Socket s = new Socket( "localhost" , portaInvio);
- DataOutputStream dos = new DataOutputStream (s.getOutputStream());
- ){
- dos.writeUTF(c.toString());
- }catch(IOException e){ e.printStackTrace(); }
- visualizzaSulLog("Invia", c.toString());
- }
- // CASO SERIALIZABLE
- public static void inviaProposta(int porta, Proposta pr){
- try ( Socket s = new Socket( "localhost" , porta);
- ObjectOutputStream oout = new ObjectOutputStream (s.getOutputStream());
- ) { oout.writeObject( pr );
- } catch (IOException e) { e.printStackTrace(); }
- }
- // *********************FUNZIONE RICEVI (SOCKET)***************************
- // CASO NON SERIALIZABLE
- public static Domanda ricevi(){
- Domanda d = null;
- try( ServerSocket servs = new ServerSocket(portaRicezione);
- Socket s = servs.accept();
- DataInputStream dis = new DataInputStream(s.getInputStream());
- ){
- d = new Domanda(dis.readUTF()); //inizializzazione fatta con il costruttore
- visualizzaSulLog("Ricevi",d.toString());
- } catch(IOException e){ e.printStackTrace(); }
- return d;
- }
- // CASO SERIALIZABLE
- public static Proposta riceviProposta(int porta){
- Proposta result = null;
- try ( ServerSocket servs = new ServerSocket(porta); // (02)
- Socket s = servs.accept(); // (03)
- ) { ObjectInputStream oin = new ObjectInputStream(s.getInputStream());
- result = ( Proposta ) oin.readObject(); //Notare il convertitore esplicito
- } catch (IOException e) {e.printStackTrace();}
- return result;
- }
- // *********************FUNZIONE STAMPA SU LOG***************************
- //E' UN INSERIMENTO IN UNA TABELLA LOG CON PREPARED STATEMENT
- public static void visualizzaSulLog(String operazione, String risultato){
- try ( Connection co = DriverManager.getConnection("jdbc:mysql://localhost:3306/banca", "root","");
- PreparedStatement ps = co.prepareStatement("INSERT INTO opinionista.log VALUES (?, ?, ?)"); //10
- ){
- ps.setInt(1,portaRicezione);
- ps.setString(2,operazione);
- ps.setString(3,risultato);
- ps.executeUpdate();
- }catch(SQLException e) { System.err.println(e.getMessage()); }
- }
- // *********************************************************************
- // *********************************************************************
- // XXXXXXXXXXXXXXXXXXXXXXX TIPOLOGIA TABLEVIEW XXXXXXXXXXXXXXXXXXXXXXXXX
- // CLASSE BEAN
- // * USARE SimpleStringProperty
- // * nel costruttore new SimpleStringProperty(String s) (ESEMPIO)
- // * Inserire metodi get()
- // CLASSE TABELLA (ESTENDE TABLEVIEW)
- // A seguire tutti i passaggi da fare nel costruttore
- // * Creare le colonne
- TableColumn emailCol = new TableColumn("EMAIL");
- TableColumn depositoCol = new TableColumn("DEPOSITO");
- // * Associare ad ogni colonna la sua rispettiva colonna del dbms
- emailCol.setCellValueFactory(new PropertyValueFactory<>("email"));
- depositoCol.setCellValueFactory(new PropertyValueFactory<>("deposito"));
- // * Inizializza la observableList
- listaOsservabileClienti = FXCollections.observableArrayList();
- // * Impostala come lista per la tableView
- setItems(listaOsservabileClienti);
- // * Aggiungi le colonne alla tabella
- getColumns().addAll(emailCol, depositoCol);
- //Classe GestoreDBMS
- //Per ogni metodo devo stabilire una connessione e fare uno statement
- //Può essere fatta in un metodo direttamente o nella classe stessa tramite un
- //blocco static. Nel secondo caso non devo farla di nuovo nei metodi
- try(
- Connection co = DriverManager.getConnection("jdbc:mysql://localhost:3306/", "root","");
- PreparedStatement ps = co.prepareStatement("SELECT email,deposito FROM depositoclienti WHERE deposito > ? ");
- ) {
- //Blocco interno
- }
- catch(SQLException e){ System.err.println(e.getMessage());
- }
- // Le prossime istruzioni sono da inserire nel blocco interno
- // settare eventuali parametri del PreparedStatement
- ps.setInt(1,depositoMinimo);
- // Se ho bisogno di prendere il result set della query uso
- ResultSet rs = ps.executeQuery();
- while (rs.next())
- listaClienti.add(new Cliente(rs.getString("email"), rs.getInt("deposito")));
- // Altrimenti eseguo solo la query
- ps.executeQuery(); // query di selezione
- ps.executeUpdate(); //in caso di inserimenti/update/rimozioni
- // Classe Main (estende Application)
- //I prossimi passaggi sono da inserire nel metodo start()
- //Inserire tutto in una vbox, sia la tableview che eventuali label
- //o inputText
- VBox vbox = new VBox();
- vbox.getChildren().addAll(tabellaVisuale,campoDepositoMinimo,labelDepositoMinimo);
- //Inserire eventuali eventi in questo modo
- bottoneSettaDeposito.setOnAction((ActionEvent ev) -> {
- depositoMinimo = Integer.parseInt(campoDepositoMinimo.getText());
- tabellaVisuale.aggiornaListaClienti( DataBaseDepositiClienti.CaricaClientiConDepositoMinimo(depositoMinimo));
- });
- //Setting Stage
- Scene scene = new Scene(new Group(vbox));
- stage.setTitle("Depositi Bancari");
- stage.setScene(scene);
- stage.show();
- // *********************************************************************
- // *********************************************************************
- // XXXXXXXXXXXXXXXXXXXXXXX TIPOLOGIA PCLIST XXXXXXXXXXXXXXXXXXXXXXXXX
- //Classe PCStatement
- public class PCStatement {
- public final int senderPort;
- public final String statement;
- public final double item;
- public PCStatement(int sp, String st, double it){
- senderPort = sp;
- statement = st;
- item = it;
- }
- }
- //Classe PCList (Contiene il main)
- public class PCList {
- private static int receivePort;
- private static int sendPort;
- //Configura le variabili di PCList (tramite gli args del main)
- public static void configure(int rp, int sp){
- receivePort = rp;
- sendPort = sp;
- }
- //Funzione invio classica (non serializable)
- public static void send(PCStatement o){
- try(
- Socket s = new Socket("localhost",sendPort);
- DataOutputStream dos = new DataOutputStream(s.getOutputStream());
- ){
- dos.writeUTF( (new XStream()).toXML(o));
- }catch(IOException e) { e.printStackTrace(); }
- System.out.println("- invio a" + sendPort);
- }
- //Funzione ricevi classica (non serializable)
- public static PCStatement receive(){
- PCStatement p = null;
- try(
- ServerSocket servs = new ServerSocket(receivePort);
- Socket s = servs.accept();
- DataInputStream dis = new DataInputStream(s.getInputStream());
- ){
- p = (PCStatement) (new XStream()).fromXML(dis.readUTF());
- }catch(IOException e) { e.printStackTrace();}
- System.out.println("- ricevo");
- return p;
- }
- // Preleva una stringa xml dal db (di un record) e la converte in arraylist
- // Devo convertire la stringa in xml in un arraylist tramite XStream
- // Notare il convertitore esplicito
- public static ArrayList read(){
- try(
- Connection co = DriverManager.getConnection("jdbc:mysql://localhost:3306/pclist","root", "");
- PreparedStatement ps = co.prepareStatement("SELECT xmlarraylist FROM pclist WHERE receiveport = ? ");
- ){
- ps.setInt(1, receivePort);
- ResultSet rs = ps.executeQuery();
- if(rs.next()){
- return (ArrayList) (new XStream()).fromXML(rs.getString("xmlarraylist"));
- }
- }catch(SQLException e){ System.err.println(e.getMessage()); }
- return null;
- }
- //Aggiorna un record stringa xml del database con un arraylist
- //L'arraylist va convertita in una stringa xml tramite XStream
- public static void write(ArrayList al){
- try(
- Connection co = DriverManager.getConnection("jdbc:mysql://localhost:3306/pclist","root", "");
- PreparedStatement ps = co.prepareStatement("UPDATE pclist SET xmlarraylist = ? WHERE receiveport = ? ");
- ){
- String x = (new XStream()).toXML(al);
- ps.setString(1, x); ps.setInt(2, receivePort);
- ps.executeUpdate();
- }catch(SQLException e){ System.err.println(e.getMessage()); }
- }
- //Funzione Create come da testo
- //ricevo solo nel caso in cui ritorno alla "radice"
- public static void create(PCStatement o){
- write(new ArrayList<>());
- System.out.println( (new XStream()).toXML(o) + "\n Creato \n" + read());
- send(o);
- if(o.senderPort == receivePort){ receive(); }
- }
- //Funzione Create come da testo
- //Tutte le volte che altero in qualche modo la mia arraylist devo fare write()
- //Invio e ricevo in caso non sono la lista minima
- public static void add(PCStatement o){
- ArrayList al = read();
- int dimensioneDesiderata = 0;
- while(al.size() > dimensioneDesiderata){
- send(o);
- o = receive();
- dimensioneDesiderata++;
- }
- al.add(o.item);
- write(al);
- System.out.println( (new XStream()).toXML(o) + " Aggiunto" + read());
- }
- //Rimuovo un elemento dalla lista
- //Se non lo trovo invio a quello dopo, ricevo solo se ritorno alla "radice"
- //Tutte le volte che aggiorno l'arraylist devo fare la write;
- public static void remove(PCStatement o){
- ArrayList al = read();
- if(!al.remove(o.item)){
- send(o);
- if(o.senderPort == receivePort) receive();
- } else{
- write(al);
- System.out.println( (new XStream()).toXML(o) + " \n Rimosso \n" + read());
- }
- }
- //Funzione Main
- //Controllo se ho uno statement e nel caso lo creo (args.length > 2)
- //altrimenti lo riceverò da qualcun'altro.
- //In base alla stringa "statement" vedo che funzione svolgere
- public static void main(String[] args) {
- PCStatement p;
- configure(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
- if(args.length > 2) p = new PCStatement(Integer.parseInt(args[0]),args[2], Double.parseDouble(args[3]));
- else p = receive();
- switch (p.statement){
- case "create": create(p); break;
- case "add": add(p); break;
- case "remove": remove(p); break;
- }
- }
- }
- // *********************************************************************
- // *********************** ESERCIZIO VECCHIO ***************************
- // *********************************************************************
- // *********************************************************************
- // *********************************************************************
- //Creazione classe per le eccezioni;
- public class ElementoNonTrovatoException extends Exception {
- public ElementoNonTrovatoException() {
- super();
- }
- public ElementoNonTrovatoException(String m){
- super(m);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement