Advertisement
Guest User

Untitled

a guest
Feb 16th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.37 KB | None | 0 0
  1.  
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutputStream;
  6. import java.io.OutputStream;
  7. import java.net.Socket;
  8. import javafx.application.Application;
  9. import javafx.geometry.Insets;
  10. import javafx.scene.Scene;
  11. import javafx.scene.control.Button;
  12. import javafx.scene.control.Label;
  13. import javafx.scene.control.PasswordField;
  14. import javafx.scene.control.TextArea;
  15. import javafx.scene.control.TextField;
  16. import javafx.scene.layout.GridPane;
  17. import javafx.stage.Stage;
  18.  
  19.  
  20.  
  21.  
  22. public class myClient extends Application{
  23.  
  24. String responso;
  25.  
  26. InputStream iStream;
  27. OutputStream oStream;
  28. ObjectInputStream sIn;
  29. ObjectOutputStream sOut;
  30. String responsoInserisci;
  31.  
  32. //Grafica inserisci e modifica dati soggetto (interf. amministratore)
  33.  
  34. Label inserisciSoggetto = new Label (" INSERISCI UN NUOVO SOGGETTO ");
  35. Label lNominativo = new Label("Nominativo :");
  36. TextField tNominativo = new TextField();
  37. Label lVia = new Label("Via :");
  38. TextField tVia = new TextField();
  39. Label lCivico = new Label("Civico :");
  40. TextField tCivico = new TextField();
  41. Label lCap = new Label("Cap :");
  42. TextField tCap = new TextField();
  43. Label lCitta = new Label("Città :");
  44. TextField tCitta = new TextField();
  45. Label lCF = new Label("Codice fiscale :");
  46. TextField tCF = new TextField();
  47. Label lPartitaIva = new Label("Partita Iva :");
  48. TextField tPartitaIva = new TextField();
  49. Label lNomeRapp = new Label("Nome rappresentante :");
  50. TextField tNomeRapp = new TextField();
  51. Label lCognomeRapp = new Label("Cognome rappresentante :");
  52. TextField tCognomeRapp = new TextField();
  53. Label lCfRapp = new Label("Codice Fiscale rappresentante :");
  54. TextField tCfRapp = new TextField();
  55. Label lIdAttivita = new Label("Id attività");
  56. TextField tIdAttivita = new TextField();
  57. Button bInserisci = new Button("Inserisci");
  58. Button bModifica = new Button("Modifica");
  59.  
  60.  
  61.  
  62. //LOGIN
  63.  
  64. Label lUser = new Label(" Username");
  65. TextField tUser = new TextField();
  66. Label lPass = new Label(" Password");
  67. PasswordField tPass = new PasswordField();
  68. Button bLogin = new Button("Login");
  69.  
  70. ///FINE LOGIN
  71.  
  72. ///REGISTRATI
  73.  
  74. Label lNome = new Label("Nome");
  75. TextField tNome = new TextField();
  76. Label lCognome = new Label("Cognome");
  77. TextField tCognome = new TextField();
  78. Label lData = new Label("Data di nascita");
  79. TextField tData = new TextField();
  80. Label lSesso = new Label ("Sesso");
  81. TextField tSesso = new TextField();
  82. Button btnRegistrati = new Button("Vuoi registrarti?");
  83. Button btnIscrivi = new Button ("Registrati");
  84. Label lblLog = new Label("Console");
  85. TextArea txtLog = new TextArea();
  86. private Socket soc;
  87. GridPane p = new GridPane();
  88.  
  89.  
  90. public void start(Stage f){
  91.  
  92. bLogin.setOnAction(e -> Login());
  93. btnRegistrati.setOnAction(e -> registrati());
  94.  
  95. //Label/Textfield per registrazione nascoste
  96.  
  97. lNome.setVisible(false);
  98. tNome.setVisible(false);
  99. lCognome.setVisible(false);
  100. tCognome.setVisible(false);
  101. lData.setVisible(false);
  102. tData.setVisible(false);
  103. lSesso.setVisible(false);
  104. tSesso.setVisible(false);
  105. btnIscrivi.setVisible(false);
  106.  
  107. //Fine
  108.  
  109.  
  110. p.setPadding(new Insets(5,5,5,5));
  111. p.setHgap(10);
  112. p.setVgap(10);
  113. p.add(btnRegistrati, 2, 7);
  114. p.add(lUser, 1, 1);
  115. p.add(tUser, 2, 1);
  116. p.add(bLogin, 2, 6);
  117. p.add(lPass, 1, 2);
  118. p.add(tPass, 2, 2);
  119. p.add(lNome, 1, 1);
  120. p.add(tNome, 2, 1);
  121. p.add(lCognome, 1, 2);
  122. p.add(tCognome, 2, 2);
  123. p.add(lData, 1, 3);
  124. p.add(tData, 2, 3);
  125. p.add(lSesso, 1, 4);
  126. p.add(tSesso, 2, 4);
  127. p.add(btnIscrivi, 2, 6);
  128. p.add(lblLog, 0, 10);
  129. p.add(txtLog, 1, 10, 3, 1);
  130. Scene s = new Scene(p, 600, 500);
  131. f.setScene(s);
  132. f.show();
  133. }
  134. public void registrati() {
  135.  
  136.  
  137. btnRegistrati.setVisible(false);
  138. lNome.setVisible(true);
  139. tNome.setVisible(true);
  140. lCognome.setVisible(true);
  141. tCognome.setVisible(true);
  142. lData.setVisible(true);
  143. tData.setVisible(true);
  144. lSesso.setVisible(true);
  145. tSesso.setVisible(true);
  146. bLogin.setVisible(false);
  147. lUser.setVisible(false);
  148. lPass.setVisible(false);
  149. tUser.setVisible(false);
  150. tPass.setVisible(false);
  151. btnIscrivi.setVisible(true);
  152. }
  153.  
  154. public void Login(){
  155.  
  156.  
  157. if(tUser.getText().equals("") || tPass.getText().equals("") ){
  158. ScriviLog("Compilare entrambi i campi");
  159. }else{
  160. try{
  161. ScriviLog("Connessione al Server in corso...");
  162. soc = new Socket("localhost", 12345);
  163. ScriviLog("Connessione Avvenuta");
  164. oStream = soc.getOutputStream();
  165. iStream = soc.getInputStream();
  166. sOut = new ObjectOutputStream (oStream);
  167. sIn = new ObjectInputStream(iStream);
  168. sOut.writeObject(tUser.getText());
  169. sOut.flush();
  170. sOut.writeObject(tPass.getText());
  171. sOut.flush();
  172. responso = (String)sIn.readObject();
  173. if(responso.equals("ok")){
  174. ScriviLog("Benvenuto");
  175. // aggiornamento stati interfaccia
  176.  
  177.  
  178.  
  179. }else{
  180. ScriviLog("Credenziali errate.");
  181. }
  182. }catch(IOException | ClassNotFoundException e){
  183. System.out.println(e);
  184. ScriviLog(e.toString());
  185. }
  186.  
  187. // Interfaccia per inserire soggetti
  188.  
  189. p.getChildren().clear();
  190. p.add(inserisciSoggetto, 0, 0);
  191. p.add(lNominativo, 0, 1);
  192. p.add(tNominativo, 1, 1);
  193. p.add(lVia, 0, 2);
  194. p.add(tVia, 1, 2);
  195. p.add(lCivico, 0, 3);
  196. p.add(tCivico, 1, 3);
  197. p.add(lCap, 0, 4);
  198. p.add(tCap, 1, 4);
  199. p.add(lCitta, 0, 5);
  200. p.add(tCitta, 1, 5);
  201. p.add(lCF, 0, 6);
  202. p.add(tCF, 1, 6);
  203. p.add(lPartitaIva, 0, 7);
  204. p.add(tPartitaIva, 1, 7);
  205. p.add(lNomeRapp, 0, 8);
  206. p.add(tNomeRapp, 1, 8);
  207. p.add(lCognomeRapp, 0, 9);
  208. p.add(tCognomeRapp, 1, 9);
  209. p.add(lCfRapp, 0, 10);
  210. p.add(tCfRapp, 1, 10);
  211. p.add(lIdAttivita, 0, 11);
  212. p.add(tIdAttivita, 1, 11);
  213. p.add(bInserisci, 0, 12);
  214. p.add(bModifica, 1, 12);
  215. bInserisci.setOnAction(e-> inserisci());
  216. bModifica.setOnAction(e-> modifica());
  217. }
  218. }
  219. public void inserisci() {
  220.  
  221.  
  222. //Metodo per inserire dati oggetto
  223.  
  224.  
  225. try {
  226.  
  227. sOut.writeObject(tNominativo.getText());
  228. sOut.flush();
  229. sOut.writeObject(tVia.getText());
  230. sOut.flush();
  231. sOut.writeObject(tCivico.getText());
  232. sOut.flush();
  233. sOut.writeObject(tCap.getText());
  234. sOut.flush();
  235. sOut.writeObject(tCitta.getText());
  236. sOut.flush();
  237. sOut.writeObject(tCF.getText());
  238. sOut.flush();
  239. sOut.writeObject(tPartitaIva.getText());
  240. sOut.flush();
  241. sOut.writeObject(tNomeRapp.getText());
  242. sOut.flush();
  243. sOut.writeObject(tCognomeRapp.getText());
  244. sOut.flush();
  245. sOut.writeObject(tCfRapp.getText());
  246. sOut.flush();
  247. sOut.writeObject(tIdAttivita.getText());
  248. sOut.flush();
  249. System.out.println("Query riuscita con successo!");
  250. }catch(IOException e) {
  251. System.out.println(e);
  252. }
  253.  
  254. }
  255. public void modifica() {
  256. try {
  257. try {
  258. responso = (String)sIn.readObject();
  259. } catch (IOException e) {
  260. e.printStackTrace();
  261. }
  262. }catch(ClassNotFoundException e) {
  263. System.out.println(e);
  264. }
  265.  
  266. if(responso.equals("modifica")) {
  267. try {
  268. sOut.writeObject(tNominativo.getText());
  269. sOut.flush();
  270. sOut.writeObject(tVia.getText());
  271. sOut.flush();
  272. sOut.writeObject(tCivico.getText());
  273. sOut.flush();
  274. sOut.writeObject(tCap.getText());
  275. sOut.flush();
  276. sOut.writeObject(tCitta.getText());
  277. sOut.flush();
  278. sOut.writeObject(tCF.getText());
  279. sOut.flush();
  280. sOut.writeObject(tPartitaIva.getText());
  281. sOut.flush();
  282. sOut.writeObject(tNomeRapp.getText());
  283. sOut.flush();
  284. sOut.writeObject(tCognomeRapp.getText());
  285. sOut.flush();
  286. sOut.writeObject(tCfRapp.getText());
  287. sOut.flush();
  288. sOut.writeObject(tIdAttivita.getText());
  289. sOut.flush();
  290. System.out.println("Modifica riuscita con successo!");
  291. }catch(IOException e) {
  292. System.out.println(e);
  293. }
  294. }
  295. }
  296.  
  297. private void ScriviLog(String s) {
  298. txtLog.setText(txtLog.getText() + s + "\n");
  299. txtLog.setScrollTop(Double.MAX_VALUE);
  300. }
  301. public static void main(String[]args) {
  302. launch(args);
  303. }
  304. }
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313. import java.io.IOException;
  314. import java.net.ServerSocket;
  315. import java.net.Socket;
  316.  
  317. public class myServer {
  318. public static void main(String Args[]){
  319. ServerSocket sSoc;
  320. Socket soc;
  321. ServerThread t;
  322. int porta = 12345;
  323. try{
  324. sSoc = new ServerSocket(porta);
  325. System.out.println("Server avviato in ascolto sulla porta 12345...");
  326.  
  327. while(true){
  328. soc = sSoc.accept();
  329. t = new ServerThread(soc);
  330. t.start();
  331. System.out.println("Avviato thread: " + t.getName());
  332. }
  333. } catch (IOException e){
  334. System.out.println(e);
  335. }
  336. }
  337. }
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345. import java.io.BufferedReader;
  346. import java.sql.*;
  347. import java.io.FileReader;
  348. import java.io.IOException;
  349. import java.net.Socket;
  350.  
  351. import java.io.InputStream;
  352. import java.io.OutputStream;
  353. import java.io.ObjectInputStream;
  354. import java.io.ObjectOutputStream;
  355.  
  356. public class ServerThread extends Thread{
  357.  
  358. Socket soc;
  359. InputStream iStream;
  360. OutputStream oStream;
  361. ObjectInputStream sIn;
  362. ObjectOutputStream sOut;
  363.  
  364.  
  365.  
  366.  
  367. //variabili login
  368. String userId;
  369. String password;
  370.  
  371. //variabili soggetto
  372. String nominativo;
  373. String via;
  374. String civico;
  375. String CAP;
  376. String citta;
  377. String codiceFiscale;
  378. String partitaIva;
  379. String nomeRapp;
  380. String cognomeRapp;
  381. String cfRapp;
  382. int idAttivita;
  383.  
  384.  
  385. String riga;
  386. Boolean trovato;
  387. String[] splitRiga;
  388. FileReader f;
  389. BufferedReader daFile;
  390. int i;
  391.  
  392. public ServerThread(Socket s){
  393. soc = s;
  394. }
  395.  
  396. public void run(){
  397. // apertura canale di comunicazione
  398. System.out.println(getName() + ": apertura canale di comunicazione...");
  399.  
  400. try{
  401. iStream = soc.getInputStream();
  402. oStream = soc.getOutputStream();
  403. sIn = new ObjectInputStream(iStream);
  404. sOut = new ObjectOutputStream(oStream);
  405. System.out.println(getName()+ ": ...canale di comunicazione aperto.");
  406.  
  407. // legge username e password
  408. userId = (String)sIn.readObject();
  409. password = (String)sIn.readObject();
  410.  
  411.  
  412.  
  413.  
  414.  
  415. System.out.println(getName() + ": Ricevute credenziali di accesso.");
  416. // controlliamo le credenziali
  417. try {
  418. Class.forName("com.mysql.jdbc.Driver");
  419. System.out.println("Caricamento Diver");
  420. }catch(ClassNotFoundException e) {
  421.  
  422. }
  423.  
  424. String url = "jdbc:mysql://localhost:3306/scrum?user=root&password=";
  425.  
  426. System.out.println("Connessione al database in corso...");
  427. try (Connection connection = DriverManager.getConnection(url)) {
  428. System.out.println("Database connesso!");
  429. Statement stm = connection.createStatement();
  430. //controllo credenziali
  431.  
  432. ResultSet rs = stm.executeQuery("SELECT username, password FROM studente");
  433.  
  434. while (rs.next()) {
  435. if(rs.getString("username").equals(userId) && rs.getString("password").equals(password)) {
  436. System.out.println("Benvenuto " + userId);
  437. sOut.writeObject("ok");
  438.  
  439. sOut.flush();
  440. }
  441. }
  442.  
  443.  
  444. ResultSet rs1 = stm.executeQuery("SELECT idAmministratore, password FROM amministratore");
  445.  
  446. while (rs1.next()) {
  447. if(rs1.getString("idAmministratore").equals(userId) && rs1.getString("password").equals(password)) {
  448. System.out.println("Benvenuto " + userId);
  449. sOut.writeObject("ok");
  450. sOut.flush();
  451.  
  452. }
  453. }
  454. // legge dati soggetto
  455. nominativo = (String)sIn.readObject();
  456. via = (String)sIn.readObject();
  457. civico = (String)sIn.readObject();
  458. CAP = (String)sIn.readObject();
  459. citta = (String)sIn.readObject();
  460. codiceFiscale = (String)sIn.readObject();
  461. partitaIva = (String)sIn.readObject();
  462. nomeRapp = (String)sIn.readObject();
  463. cognomeRapp = (String)sIn.readObject();
  464. cfRapp = (String)sIn.readObject();
  465. idAttivita = (int)sIn.readObject();
  466.  
  467.  
  468. // Query per inserire dati soggetto
  469.  
  470.  
  471. 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+")");
  472.  
  473.  
  474. // Query modifica dati soggetto
  475.  
  476. sOut.writeObject("modifica");
  477. sOut.flush();
  478. 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);
  479.  
  480.  
  481. connection.close();
  482.  
  483. } catch (SQLException e) {
  484. throw new IllegalStateException("Impossibile connettersi al database!", e);
  485. }
  486.  
  487. } catch(IOException | ClassNotFoundException e){
  488. System.out.println(e);
  489. }
  490.  
  491.  
  492.  
  493. }
  494.  
  495. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement