Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.OutputStream;
- import java.net.Socket;
- import javafx.application.Application;
- import javafx.geometry.Insets;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.PasswordField;
- import javafx.scene.control.TextArea;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.GridPane;
- import javafx.stage.Stage;
- public class myClient extends Application{
- String responso;
- InputStream iStream;
- OutputStream oStream;
- ObjectInputStream sIn;
- ObjectOutputStream sOut;
- String responsoInserisci;
- //Grafica inserisci e modifica dati soggetto (interf. amministratore)
- Label inserisciSoggetto = new Label (" INSERISCI UN NUOVO SOGGETTO ");
- Label lNominativo = new Label("Nominativo :");
- TextField tNominativo = new TextField();
- Label lVia = new Label("Via :");
- TextField tVia = new TextField();
- Label lCivico = new Label("Civico :");
- TextField tCivico = new TextField();
- Label lCap = new Label("Cap :");
- TextField tCap = new TextField();
- Label lCitta = new Label("Città :");
- TextField tCitta = new TextField();
- Label lCF = new Label("Codice fiscale :");
- TextField tCF = new TextField();
- Label lPartitaIva = new Label("Partita Iva :");
- TextField tPartitaIva = new TextField();
- Label lNomeRapp = new Label("Nome rappresentante :");
- TextField tNomeRapp = new TextField();
- Label lCognomeRapp = new Label("Cognome rappresentante :");
- TextField tCognomeRapp = new TextField();
- Label lCfRapp = new Label("Codice Fiscale rappresentante :");
- TextField tCfRapp = new TextField();
- Label lIdAttivita = new Label("Id attività");
- TextField tIdAttivita = new TextField();
- Button bInserisci = new Button("Inserisci");
- Button bModifica = new Button("Modifica");
- //LOGIN
- Label lUser = new Label(" Username");
- TextField tUser = new TextField();
- Label lPass = new Label(" Password");
- PasswordField tPass = new PasswordField();
- Button bLogin = new Button("Login");
- ///FINE LOGIN
- ///REGISTRATI
- Label lNome = new Label("Nome");
- TextField tNome = new TextField();
- Label lCognome = new Label("Cognome");
- TextField tCognome = new TextField();
- Label lData = new Label("Data di nascita");
- TextField tData = new TextField();
- Label lSesso = new Label ("Sesso");
- TextField tSesso = new TextField();
- Button btnRegistrati = new Button("Vuoi registrarti?");
- Button btnIscrivi = new Button ("Registrati");
- Label lblLog = new Label("Console");
- TextArea txtLog = new TextArea();
- private Socket soc;
- GridPane p = new GridPane();
- public void start(Stage f){
- bLogin.setOnAction(e -> Login());
- btnRegistrati.setOnAction(e -> registrati());
- //Label/Textfield per registrazione nascoste
- lNome.setVisible(false);
- tNome.setVisible(false);
- lCognome.setVisible(false);
- tCognome.setVisible(false);
- lData.setVisible(false);
- tData.setVisible(false);
- lSesso.setVisible(false);
- tSesso.setVisible(false);
- btnIscrivi.setVisible(false);
- //Fine
- p.setPadding(new Insets(5,5,5,5));
- p.setHgap(10);
- p.setVgap(10);
- p.add(btnRegistrati, 2, 7);
- p.add(lUser, 1, 1);
- p.add(tUser, 2, 1);
- p.add(bLogin, 2, 6);
- p.add(lPass, 1, 2);
- p.add(tPass, 2, 2);
- p.add(lNome, 1, 1);
- p.add(tNome, 2, 1);
- p.add(lCognome, 1, 2);
- p.add(tCognome, 2, 2);
- p.add(lData, 1, 3);
- p.add(tData, 2, 3);
- p.add(lSesso, 1, 4);
- p.add(tSesso, 2, 4);
- p.add(btnIscrivi, 2, 6);
- p.add(lblLog, 0, 10);
- p.add(txtLog, 1, 10, 3, 1);
- Scene s = new Scene(p, 600, 500);
- f.setScene(s);
- f.show();
- }
- public void registrati() {
- btnRegistrati.setVisible(false);
- lNome.setVisible(true);
- tNome.setVisible(true);
- lCognome.setVisible(true);
- tCognome.setVisible(true);
- lData.setVisible(true);
- tData.setVisible(true);
- lSesso.setVisible(true);
- tSesso.setVisible(true);
- bLogin.setVisible(false);
- lUser.setVisible(false);
- lPass.setVisible(false);
- tUser.setVisible(false);
- tPass.setVisible(false);
- btnIscrivi.setVisible(true);
- }
- public void Login(){
- if(tUser.getText().equals("") || tPass.getText().equals("") ){
- ScriviLog("Compilare entrambi i campi");
- }else{
- try{
- ScriviLog("Connessione al Server in corso...");
- soc = new Socket("localhost", 12345);
- ScriviLog("Connessione Avvenuta");
- oStream = soc.getOutputStream();
- iStream = soc.getInputStream();
- sOut = new ObjectOutputStream (oStream);
- sIn = new ObjectInputStream(iStream);
- sOut.writeObject(tUser.getText());
- sOut.flush();
- sOut.writeObject(tPass.getText());
- sOut.flush();
- responso = (String)sIn.readObject();
- if(responso.equals("ok")){
- ScriviLog("Benvenuto");
- // aggiornamento stati interfaccia
- }else{
- ScriviLog("Credenziali errate.");
- }
- }catch(IOException | ClassNotFoundException e){
- System.out.println(e);
- ScriviLog(e.toString());
- }
- // Interfaccia per inserire soggetti
- p.getChildren().clear();
- p.add(inserisciSoggetto, 0, 0);
- p.add(lNominativo, 0, 1);
- p.add(tNominativo, 1, 1);
- p.add(lVia, 0, 2);
- p.add(tVia, 1, 2);
- p.add(lCivico, 0, 3);
- p.add(tCivico, 1, 3);
- p.add(lCap, 0, 4);
- p.add(tCap, 1, 4);
- p.add(lCitta, 0, 5);
- p.add(tCitta, 1, 5);
- p.add(lCF, 0, 6);
- p.add(tCF, 1, 6);
- p.add(lPartitaIva, 0, 7);
- p.add(tPartitaIva, 1, 7);
- p.add(lNomeRapp, 0, 8);
- p.add(tNomeRapp, 1, 8);
- p.add(lCognomeRapp, 0, 9);
- p.add(tCognomeRapp, 1, 9);
- p.add(lCfRapp, 0, 10);
- p.add(tCfRapp, 1, 10);
- p.add(lIdAttivita, 0, 11);
- p.add(tIdAttivita, 1, 11);
- p.add(bInserisci, 0, 12);
- p.add(bModifica, 1, 12);
- bInserisci.setOnAction(e-> inserisci());
- bModifica.setOnAction(e-> modifica());
- }
- }
- public void inserisci() {
- //Metodo per inserire dati oggetto
- try {
- sOut.writeObject(tNominativo.getText());
- sOut.flush();
- sOut.writeObject(tVia.getText());
- sOut.flush();
- sOut.writeObject(tCivico.getText());
- sOut.flush();
- sOut.writeObject(tCap.getText());
- sOut.flush();
- sOut.writeObject(tCitta.getText());
- sOut.flush();
- sOut.writeObject(tCF.getText());
- sOut.flush();
- sOut.writeObject(tPartitaIva.getText());
- sOut.flush();
- sOut.writeObject(tNomeRapp.getText());
- sOut.flush();
- sOut.writeObject(tCognomeRapp.getText());
- sOut.flush();
- sOut.writeObject(tCfRapp.getText());
- sOut.flush();
- sOut.writeObject(tIdAttivita.getText());
- sOut.flush();
- System.out.println("Query riuscita con successo!");
- }catch(IOException e) {
- System.out.println(e);
- }
- }
- public void modifica() {
- try {
- try {
- responso = (String)sIn.readObject();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }catch(ClassNotFoundException e) {
- System.out.println(e);
- }
- if(responso.equals("modifica")) {
- try {
- sOut.writeObject(tNominativo.getText());
- sOut.flush();
- sOut.writeObject(tVia.getText());
- sOut.flush();
- sOut.writeObject(tCivico.getText());
- sOut.flush();
- sOut.writeObject(tCap.getText());
- sOut.flush();
- sOut.writeObject(tCitta.getText());
- sOut.flush();
- sOut.writeObject(tCF.getText());
- sOut.flush();
- sOut.writeObject(tPartitaIva.getText());
- sOut.flush();
- sOut.writeObject(tNomeRapp.getText());
- sOut.flush();
- sOut.writeObject(tCognomeRapp.getText());
- sOut.flush();
- sOut.writeObject(tCfRapp.getText());
- sOut.flush();
- sOut.writeObject(tIdAttivita.getText());
- sOut.flush();
- System.out.println("Modifica riuscita con successo!");
- }catch(IOException e) {
- System.out.println(e);
- }
- }
- }
- private void ScriviLog(String s) {
- txtLog.setText(txtLog.getText() + s + "\n");
- txtLog.setScrollTop(Double.MAX_VALUE);
- }
- public static void main(String[]args) {
- launch(args);
- }
- }
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class myServer {
- public static void main(String Args[]){
- ServerSocket sSoc;
- Socket soc;
- ServerThread t;
- int porta = 12345;
- try{
- sSoc = new ServerSocket(porta);
- System.out.println("Server avviato in ascolto sulla porta 12345...");
- while(true){
- soc = sSoc.accept();
- t = new ServerThread(soc);
- t.start();
- System.out.println("Avviato thread: " + t.getName());
- }
- } catch (IOException e){
- System.out.println(e);
- }
- }
- }
- import java.io.BufferedReader;
- import java.sql.*;
- import java.io.FileReader;
- import java.io.IOException;
- import java.net.Socket;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- public class ServerThread extends Thread{
- Socket soc;
- InputStream iStream;
- OutputStream oStream;
- ObjectInputStream sIn;
- ObjectOutputStream sOut;
- //variabili login
- String userId;
- String password;
- //variabili soggetto
- String nominativo;
- String via;
- String civico;
- String CAP;
- String citta;
- String codiceFiscale;
- String partitaIva;
- String nomeRapp;
- String cognomeRapp;
- String cfRapp;
- int idAttivita;
- String riga;
- Boolean trovato;
- String[] splitRiga;
- FileReader f;
- BufferedReader daFile;
- int i;
- public ServerThread(Socket s){
- soc = s;
- }
- public void run(){
- // apertura canale di comunicazione
- System.out.println(getName() + ": apertura canale di comunicazione...");
- try{
- iStream = soc.getInputStream();
- oStream = soc.getOutputStream();
- sIn = new ObjectInputStream(iStream);
- sOut = new ObjectOutputStream(oStream);
- System.out.println(getName()+ ": ...canale di comunicazione aperto.");
- // legge username e password
- userId = (String)sIn.readObject();
- password = (String)sIn.readObject();
- System.out.println(getName() + ": Ricevute credenziali di accesso.");
- // controlliamo le credenziali
- try {
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("Caricamento Diver");
- }catch(ClassNotFoundException e) {
- }
- String url = "jdbc:mysql://localhost:3306/scrum?user=root&password=";
- System.out.println("Connessione al database in corso...");
- try (Connection connection = DriverManager.getConnection(url)) {
- System.out.println("Database connesso!");
- Statement stm = connection.createStatement();
- //controllo credenziali
- ResultSet rs = stm.executeQuery("SELECT username, password FROM studente");
- while (rs.next()) {
- if(rs.getString("username").equals(userId) && rs.getString("password").equals(password)) {
- System.out.println("Benvenuto " + userId);
- sOut.writeObject("ok");
- sOut.flush();
- }
- }
- ResultSet rs1 = stm.executeQuery("SELECT idAmministratore, password FROM amministratore");
- while (rs1.next()) {
- if(rs1.getString("idAmministratore").equals(userId) && rs1.getString("password").equals(password)) {
- System.out.println("Benvenuto " + userId);
- sOut.writeObject("ok");
- sOut.flush();
- }
- }
- // legge dati soggetto
- nominativo = (String)sIn.readObject();
- via = (String)sIn.readObject();
- civico = (String)sIn.readObject();
- CAP = (String)sIn.readObject();
- citta = (String)sIn.readObject();
- codiceFiscale = (String)sIn.readObject();
- partitaIva = (String)sIn.readObject();
- nomeRapp = (String)sIn.readObject();
- cognomeRapp = (String)sIn.readObject();
- cfRapp = (String)sIn.readObject();
- idAttivita = (int)sIn.readObject();
- // Query per inserire dati soggetto
- stm.executeUpdate("INSERT INTO soggetto (nominativo,via,civico,CAP,citta,codiceFiscale,partitaIva,nomeRapp,cognomeRapp,cfRapp,idAttivita) values('"+nominativo+"','"+via+"','"+civico+"','"+CAP+"','"+citta+"','"+codiceFiscale+"','"+partitaIva+"','"+nomeRapp+"','"+cognomeRapp+"','"+cfRapp+"',"+idAttivita+")");
- // Query modifica dati soggetto
- sOut.writeObject("modifica");
- sOut.flush();
- stm.executeUpdate("UPDATE soggetto SET nominativo = '"+nominativo+"',via = '"+via+"', civico = '"+civico+"',CAP = '"+CAP+"',citta = '"+citta+"',codiceFiscale = '"+codiceFiscale+"',partitaIva = '"+partitaIva+"',nomeRapp = '"+nomeRapp+"',cognomeRapp = '"+cognomeRapp+"',cfRapp = '"+cfRapp+"', where idAttivita = "+idAttivita);
- connection.close();
- } catch (SQLException e) {
- throw new IllegalStateException("Impossibile connettersi al database!", e);
- }
- } catch(IOException | ClassNotFoundException e){
- System.out.println(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement