Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.97 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.  
  9. import javafx.application.Application;
  10. import javafx.geometry.Insets;
  11. import javafx.scene.Scene;
  12. import javafx.scene.control.Button;
  13. import javafx.scene.control.ComboBox;
  14. import javafx.scene.control.Label;
  15. import javafx.scene.control.PasswordField;
  16. import javafx.scene.control.TextArea;
  17. import javafx.scene.control.TextField;
  18. import javafx.scene.layout.GridPane;
  19. import javafx.stage.Stage;
  20.  
  21.  
  22.  
  23.  
  24. public class myClient extends Application{
  25. String responsoCredenziali;
  26. String metodo;
  27. String comboSogg;
  28. String comboClass;
  29. String daServerSogg;
  30. String daServerClasse;
  31. String richiesta;
  32.  
  33. InputStream iStream;
  34. OutputStream oStream;
  35. ObjectInputStream sIn;
  36. ObjectOutputStream sOut;
  37.  
  38. //Login
  39. Label lUser = new Label(" Username");
  40. TextField tUser = new TextField();
  41. Label lPass = new Label(" Password");
  42. PasswordField tPass = new PasswordField();
  43. Button bLogin = new Button(" Login");
  44.  
  45. //Registrazione
  46. Label lIdStud = new Label("Inserisci l'username");
  47. TextField tIdStud = new TextField();
  48. Label lPassStud = new Label("Inserisci la password");
  49. PasswordField tPassStud = new PasswordField();
  50. Label lNome = new Label(" Nome");
  51. TextField tNome = new TextField();
  52. Label lCognome = new Label(" Cognome");
  53. TextField tCognome = new TextField();
  54. Label lData = new Label(" Data di nascita");
  55. TextField tData = new TextField();
  56. Label lSesso = new Label (" Sesso");
  57. TextField tSesso = new TextField();
  58. Button btnRegistrati = new Button(" Non sei resgistrato?");
  59. Button btnIscrivi = new Button (" Registrati");
  60. Label lblLog = new Label(" Log");
  61. TextArea txtLog = new TextArea();
  62. Button btnConfReg = new Button("Conferma registrazione");
  63. // Inserimento e modifica soggeto
  64.  
  65. Label lNom = new Label ("Nome Azienda");
  66. TextField tNom = new TextField();
  67. Label lVia = new Label ("Via Azienda");
  68. TextField tVia = new TextField();
  69. Label lCiv = new Label ("Civico Azienda");
  70. TextField tCiv = new TextField();
  71. Label lCap = new Label ("CAP Azienda");
  72. TextField tCap = new TextField();
  73. Label lCitta = new Label ("Via Azienda");
  74. TextField tCitta = new TextField();
  75. Label lCod = new Label ("Codice Fiscale Azienda");
  76. TextField tCod = new TextField();
  77. Label lPiva = new Label ("Partita Iva Azienda");
  78. TextField tPiva = new TextField();
  79. Label lNomRap = new Label ("Nome Rappresentante");
  80. TextField tNomRap = new TextField();
  81. Label lCognRap = new Label ("Cognome Rappresentante");
  82. TextField tCognRap = new TextField();
  83. Label lCfRap = new Label ("Codice Fiscale Rappresentante");
  84. TextField tCfRap = new TextField();
  85. Label lIdAtt = new Label ("Seleziona il soggetto da modificare");
  86. TextField tIdAtt = new TextField();
  87. ComboBox cIdAtt = new ComboBox();
  88. ComboBox cRichieste = new ComboBox();
  89. Label lRichieste = new Label("Registrazioni");
  90. Button bInsSogg = new Button("Inserisci dati");
  91. Button bModSogg = new Button ("Modifica dati");
  92. Button bLogout = new Button("Logout");
  93.  
  94. //inserimento classe
  95. Label lIdClasse = new Label ("iDClasse");
  96. ComboBox cIdClasse = new ComboBox();
  97. Label lAnno = new Label ("Anno");
  98. TextField tAnno = new TextField ();
  99. Label lNumero = new Label ("Numero");
  100. TextField tNumero = new TextField();
  101. Label lSezione = new Label ("Sezione");
  102. TextField tSezione = new TextField();
  103. Button bInsClasse = new Button ("Inserisci classe");
  104. Button bModClasse = new Button("Modifica classe");
  105.  
  106. GridPane p = new GridPane();
  107.  
  108. private Socket soc;
  109. public void start(Stage f){
  110. bLogin.setOnAction(e -> Login());
  111. btnRegistrati.setOnAction(e -> registrati());
  112. btnIscrivi.setOnAction(e -> Iscrivi());
  113. btnConfReg.setOnAction(e -> confReg());
  114. lIdStud.setVisible(false);
  115. tIdStud.setVisible(false);
  116. lPassStud.setVisible(false);
  117. tPassStud.setVisible(false);
  118. lNome.setVisible(false);
  119. tNome.setVisible(false);
  120. lCognome.setVisible(false);
  121. tCognome.setVisible(false);
  122. lData.setVisible(false);
  123. tData.setVisible(false);
  124. lSesso.setVisible(false);
  125. tSesso.setVisible(false);
  126. btnIscrivi.setVisible(false);
  127.  
  128. p.setPadding(new Insets(10,10,10,10));
  129. p.setHgap(10);
  130. p.setVgap(10);
  131. p.add(btnRegistrati, 0, 0);
  132. p.add(lUser, 0, 1);
  133. p.add(tUser, 1, 1);
  134. p.add(bLogin, 2, 1);
  135. p.add(lPass, 0, 2);
  136. p.add(tPass, 1, 2);
  137. p.add(lIdStud, 0, 4);
  138. p.add(tIdStud, 1, 4);
  139. p.add(lPassStud, 0, 5);
  140. p.add(tPassStud, 1, 5);
  141. p.add(lNome, 0, 6);
  142. p.add(tNome, 1, 6);
  143. p.add(lCognome, 2, 4);
  144. p.add(tCognome, 3, 4);
  145. p.add(lData, 2, 5);
  146. p.add(tData, 3, 5);
  147. p.add(lSesso, 2, 6);
  148. p.add(tSesso, 3, 6);
  149. p.add(btnIscrivi, 2, 8);
  150. p.add(lblLog, 0, 10);
  151. p.add(txtLog, 1, 10, 3, 1);
  152. Scene s = new Scene(p, 650, 600);
  153. f.setScene(s);
  154. f.show();
  155. }
  156. public void registrati() {
  157. lIdStud.setVisible(true);
  158. tIdStud.setVisible(true);
  159. lPassStud.setVisible(true);
  160. tPassStud.setVisible(true);
  161. lNome.setVisible(true);
  162. tNome.setVisible(true);
  163. lCognome.setVisible(true);
  164. tCognome.setVisible(true);
  165. lData.setVisible(true);
  166. tData.setVisible(true);
  167. lSesso.setVisible(true);
  168. tSesso.setVisible(true);
  169. bLogin.setVisible(false);
  170. lUser.setVisible(false);
  171. lPass.setVisible(false);
  172. tUser.setVisible(false);
  173. tPass.setVisible(false);
  174. btnIscrivi.setVisible(true);
  175.  
  176. }
  177. public void confReg() {
  178. String richieste;
  179. richieste = cRichieste.getValue().toString();
  180.  
  181. String conferma = tIdStud.getText() + " " + tPassStud.getText()+ " " + tNome.getText() + " " + tCognome.getText()+ " " + tData.getText() + " " + tSesso.getText();
  182.  
  183. String [] confSplit = conferma.split(" ");
  184.  
  185. if (richieste.equals(confSplit[2] + " " + confSplit[3])) {
  186. try {
  187. metodo = "confRegistrazione";
  188. sOut.writeObject(metodo);
  189. sOut.flush();
  190. String insert = "INSERT INTO studente (idStudente,password,nome,cognome,dataNascita,sesso) VALUES('"+confSplit[0]+"','"+confSplit[1]+"','"+confSplit[2]+"','"+confSplit[3]+"','"+confSplit[4]+"','"+confSplit[5] + "')";
  191.  
  192. sOut.writeObject(insert);
  193.  
  194. sOut.flush();
  195.  
  196. daServerClasse = (String) sIn.readObject();
  197.  
  198.  
  199. cIdClasse.getItems().add(daServerClasse);
  200. daServerClasse = (String) sIn.readObject();
  201.  
  202.  
  203. }catch(IOException | ClassNotFoundException e){
  204. System.out.println(e);
  205. ScriviLog(e.toString());
  206. }
  207.  
  208. }
  209. }
  210. public void Iscrivi() {
  211.  
  212. bLogin.setVisible(true);
  213. lUser.setVisible(true);
  214. lPass.setVisible(true);
  215. tUser.setVisible(true);
  216. tPass.setVisible(true);
  217. richiesta = tNome.getText() + " " + tCognome.getText();
  218. cRichieste.getItems().add(richiesta);
  219. }
  220.  
  221. public void Login(){
  222.  
  223.  
  224. if(tUser.getText().equals("") || tPass.getText().equals("") ){
  225. ScriviLog("Compilare entrambi i campi");
  226. }else{
  227. try{
  228. ScriviLog("Connessione al Server in corso...");
  229. soc = new Socket("localhost", 12345);
  230. ScriviLog("Connessione Avvenuta");
  231. oStream = soc.getOutputStream();
  232. iStream = soc.getInputStream();
  233. sOut = new ObjectOutputStream (oStream);
  234. sIn = new ObjectInputStream(iStream);
  235. sOut.writeObject(tUser.getText());
  236. sOut.flush();
  237. sOut.writeObject(tPass.getText());
  238. sOut.flush();
  239. responsoCredenziali = (String)sIn.readObject();
  240. if(responsoCredenziali.equals("stud")){
  241. ScriviLog("Benvenuto");
  242. p.getChildren().clear();
  243. Label lOre = new Label("Ore Alternanza");
  244. TextField tOre = new TextField();
  245. Label lDescAtt = new Label("Descrivi brevemente l'attività");
  246. TextArea tDescAtt = new TextArea();
  247. Label lUt = new Label("Inserisci l'username");
  248. TextField tUt = new TextField();
  249. Label lData = new Label("Data Alternanza");
  250. TextField tData = new TextField();
  251. Button bRichiedi = new Button("Conferma");
  252. Button bLogout = new Button("Logout");
  253. bRichiedi.setOnAction(e -> RichiediOre());
  254.  
  255. bLogout.setOnAction(e -> LogoutStud());
  256. p.setPadding(new Insets(10,10,10,10));
  257. p.setHgap(10);
  258. p.setVgap(10);
  259. p.add(lOre, 0, 0);
  260. p.add(tOre, 1, 0);
  261. p.add(lDescAtt, 0, 1);
  262. p.add(tDescAtt, 1, 1, 3, 1);
  263. p.add(lUt, 0, 2);
  264. p.add(tUt, 1, 2);
  265. p.add(lData, 0, 3);
  266. p.add(tData, 1, 3);
  267. p.add(bRichiedi, 1, 4);
  268. p.add(bLogout, 1, 5);
  269.  
  270.  
  271. }else{
  272. ScriviLog("Non sei uno studente");
  273. }
  274. if(responsoCredenziali.equals("amm")){
  275. ScriviLog("Benvenuto");
  276. p.getChildren().clear();
  277. bModSogg.setOnAction(e -> modSogg());
  278. bInsSogg.setOnAction(e -> insSogg());
  279. bInsClasse.setOnAction(e -> insClasse());
  280. bModClasse.setOnAction(e -> modClasse());
  281. bLogout.setOnAction(e -> LogoutAdmin());
  282. p.add(lNom, 0, 0);
  283. p.add(tNom, 1, 0);
  284. p.add(lVia, 0, 1);
  285. p.add(tVia, 1, 1);
  286. p.add(lCiv, 0, 2);
  287. p.add(tCiv, 1, 2);
  288. p.add(lCap, 0, 3);
  289. p.add(tCap, 1, 3);
  290. p.add(lCitta, 0, 4);
  291. p.add(tCitta, 1, 4);
  292. p.add(lCod, 0, 5);
  293. p.add(tCod, 1, 5);
  294. p.add(lPiva, 0, 6);
  295. p.add(tPiva, 1, 6);
  296. p.add(lNomRap, 0, 7);
  297. p.add(tNomRap, 1, 7);
  298. p.add(lCognRap, 0, 8);
  299. p.add(tCognRap, 1, 8);
  300. p.add(lCfRap, 0, 9);
  301. p.add(tCfRap, 1, 9);
  302. p.add(lIdAtt, 0, 10);
  303.  
  304. p.add(cIdAtt, 1, 10, 2, 1);
  305.  
  306. p.add(bInsSogg, 1, 12);
  307. p.add(bModSogg, 1, 13);
  308. p.add(bLogout, 1, 14);
  309. p.add(lIdClasse, 2, 0);
  310. p.add(cIdClasse, 3, 0);
  311. p.add(lAnno, 2, 1);
  312. p.add(tAnno, 3, 1);
  313. p.add(lNumero, 2, 2);
  314. p.add(tNumero, 3, 2);
  315. p.add(lSezione, 2, 3);
  316. p.add(tSezione, 3, 3);
  317. p.add(bInsClasse, 3, 4);
  318. p.add(bModClasse, 3, 5);
  319. p.add(btnConfReg, 3, 7);
  320. p.add(lRichieste, 2, 6);
  321. p.add(cRichieste, 3, 6);
  322.  
  323. try {
  324. metodo = "comboSogg";
  325. sOut.writeObject(metodo);
  326. sOut.flush();
  327. sOut.writeObject("SELECT idSoggetto FROM soggetto");
  328. sOut.flush();
  329. sOut.writeObject("SELECT Nominativo FROM soggetto");
  330. sOut.flush();
  331.  
  332. daServerSogg = (String) sIn.readObject();
  333. while(daServerSogg!=null) {
  334.  
  335. cIdAtt.getItems().add(daServerSogg);
  336. daServerSogg = (String) sIn.readObject();
  337. }
  338.  
  339.  
  340.  
  341. }catch(IOException e) {
  342.  
  343. }
  344. try {
  345. metodo = "comboClasse";
  346. sOut.writeObject(metodo);
  347. sOut.flush();
  348. sOut.writeObject("SELECT idClasse,anno,numero,sezione FROM classe");
  349. sOut.flush();
  350.  
  351. daServerClasse = (String) sIn.readObject();
  352. while(daServerClasse!=null) {
  353.  
  354. cIdClasse.getItems().add(daServerClasse);
  355. daServerClasse = (String) sIn.readObject();
  356. }
  357.  
  358.  
  359.  
  360. }catch(IOException e) {
  361.  
  362. }
  363.  
  364.  
  365.  
  366. }else{
  367. ScriviLog("Non sei un Amministratore");
  368. }
  369. if(responsoCredenziali.equals("Errore")){
  370. ScriviLog("Credenziali errate");
  371. }
  372. }catch(IOException | ClassNotFoundException e){
  373. System.out.println(e);
  374. ScriviLog(e.toString());
  375. }
  376. }
  377. }
  378. public void RichiediOre() {
  379. p.getChildren().clear();
  380. Label lAttesa = new Label ("In attesa di conferma");
  381. p.add(lAttesa, 0, 0);
  382. }
  383.  
  384. //Metodo per inserire dati soggetto
  385. public void insSogg() {
  386. metodo = "insSoggetto";
  387. try {
  388. sOut.writeObject(metodo);
  389. sOut.flush();
  390. sOut.writeObject(tNom.getText());
  391. sOut.flush();
  392. sOut.writeObject(tVia.getText());
  393. sOut.flush();
  394. sOut.writeObject(tCiv.getText());
  395. sOut.flush();
  396. sOut.writeObject(tCap.getText());
  397. sOut.flush();
  398. sOut.writeObject(tCitta.getText());
  399. sOut.flush();
  400. sOut.writeObject(tCod.getText());
  401. sOut.flush();
  402. sOut.writeObject(tPiva.getText());
  403. sOut.flush();
  404. sOut.writeObject(tNomRap.getText());
  405. sOut.flush();
  406. sOut.writeObject(tCognRap.getText());
  407. sOut.flush();
  408. sOut.writeObject(tCfRap.getText());
  409. sOut.flush();
  410. System.out.println("Query riuscita con successo!");
  411. }catch(IOException e) {
  412. System.out.println(e);
  413. }
  414.  
  415.  
  416. }
  417. public void modSogg() {
  418. //metodo modifica dati
  419. metodo = "modificaSoggetto";
  420. try{
  421. sOut.writeObject(metodo);
  422. sOut.flush();
  423. sOut.writeObject(tNom.getText());
  424. sOut.flush();
  425. sOut.writeObject(tVia.getText());
  426. sOut.flush();
  427. sOut.writeObject(tCiv.getText());
  428. sOut.flush();
  429. sOut.writeObject(tCap.getText());
  430. sOut.flush();
  431. sOut.writeObject(tCitta.getText());
  432. sOut.flush();
  433. sOut.writeObject(tCod.getText());
  434. sOut.flush();
  435. sOut.writeObject(tPiva.getText());
  436. sOut.flush();
  437. sOut.writeObject(tNomRap.getText());
  438. sOut.flush();
  439. sOut.writeObject(tCognRap.getText());
  440. sOut.flush();
  441. sOut.writeObject(tCfRap.getText());
  442. sOut.flush();
  443. comboSogg = cIdAtt.getValue().toString();
  444. String[] split = comboSogg.split("-");
  445. sOut.writeObject(split[0]);
  446. sOut.flush();
  447. System.out.println("Modifica riuscita con successo!");
  448.  
  449. }catch(IOException e){
  450. System.out.println(e);
  451. ScriviLog(e.toString());
  452. }
  453.  
  454.  
  455.  
  456.  
  457. }
  458.  
  459.  
  460.  
  461. public void insClasse() {
  462. metodo = "inserisciClasse";
  463.  
  464.  
  465. try {
  466. sOut.writeObject(metodo);
  467. sOut.flush();
  468. sOut.writeObject(tAnno.getText());
  469. sOut.flush();
  470. sOut.writeObject(tNumero.getText());
  471. sOut.flush();
  472. sOut.writeObject(tSezione.getText());
  473.  
  474. System.out.println("Query classe riuscita con successo!");
  475. }catch(IOException e) {
  476. System.out.println(e);
  477. }
  478. }
  479. public void modClasse() {
  480. //metodo modifica classe
  481. metodo = "modificaClasse";
  482. try{
  483. sOut.writeObject(metodo);
  484. sOut.flush();
  485. comboClass = cIdClasse.getValue().toString();
  486. String[] split1 = comboClass.split("-");
  487. sOut.writeObject(split1[0]);
  488. sOut.flush();
  489. sOut.writeObject(tAnno.getText());
  490. sOut.flush();
  491. sOut.writeObject(tNumero.getText());
  492. sOut.flush();
  493. sOut.writeObject(tSezione.getText());
  494. sOut.flush();
  495. System.out.println("Modifica classe con successo!");
  496.  
  497. }catch(IOException e){
  498. System.out.println(e);
  499. ScriviLog(e.toString());
  500. }
  501.  
  502.  
  503.  
  504.  
  505. }
  506.  
  507.  
  508. public void LogoutAdmin() {
  509. p.getChildren().clear();
  510. Label lUser = new Label(" Username");
  511. TextField tUser = new TextField();
  512. Label lPass = new Label(" Password");
  513. PasswordField tPass = new PasswordField();
  514. Button bLogin = new Button(" Login");
  515. Label lNome = new Label(" Nome");
  516. TextField tNome = new TextField();
  517. Label lCognome = new Label(" Cognome");
  518. TextField tCognome = new TextField();
  519. Label lData = new Label(" Data di nascita");
  520. TextField tData = new TextField();
  521. Label lSesso = new Label (" Sesso");
  522. TextField tSesso = new TextField();
  523. Button btnRegistrati = new Button(" Non sei resgistrato?");
  524. Button btnIscrivi = new Button (" Registrati");
  525. Label lblLog = new Label(" Log");
  526. TextArea txtLog = new TextArea();
  527. bLogin.setOnAction(e -> Login());
  528. btnRegistrati.setOnAction(e -> registrati());
  529. lNome.setVisible(false);
  530. tNome.setVisible(false);
  531. lCognome.setVisible(false);
  532. tCognome.setVisible(false);
  533.  
  534. lData.setVisible(false);
  535. tData.setVisible(false);
  536. lSesso.setVisible(false);
  537. tSesso.setVisible(false);
  538. btnIscrivi.setVisible(false);
  539. p.setPadding(new Insets(10,10,10,10));
  540. p.setHgap(10);
  541. p.setVgap(10);
  542. p.add(btnRegistrati, 0, 0);
  543. p.add(lUser, 0, 1);
  544. p.add(tUser, 1, 1);
  545. p.add(bLogin, 2, 1);
  546. p.add(lPass, 0, 2);
  547. p.add(tPass, 1, 2);
  548. p.add(lNome, 0, 4);
  549. p.add(tNome, 1, 4);
  550. p.add(lCognome, 0, 5);
  551. p.add(tCognome, 1, 5);
  552. p.add(lData, 0, 6);
  553. p.add(tData, 1, 6);
  554. p.add(lSesso, 0, 7);
  555. p.add(tSesso, 1, 7);
  556. p.add(btnIscrivi, 1, 8);
  557. p.add(lblLog, 0, 10);
  558. p.add(txtLog, 1, 10, 3, 1);
  559. }
  560. public void LogoutStud() {
  561. p.getChildren().clear();
  562. Label lUser = new Label(" Username");
  563. TextField tUser = new TextField();
  564. Label lPass = new Label(" Password");
  565. PasswordField tPass = new PasswordField();
  566. Button bLogin = new Button(" Login");
  567. Label lNome = new Label(" Nome");
  568. TextField tNome = new TextField();
  569. Label lCognome = new Label(" Cognome");
  570. TextField tCognome = new TextField();
  571. Label lData = new Label(" Data di nascita");
  572. TextField tData = new TextField();
  573. Label lSesso = new Label (" Sesso");
  574. TextField tSesso = new TextField();
  575. Button btnRegistrati = new Button(" Non sei resgistrato?");
  576. Button btnIscrivi = new Button (" Registrati");
  577. Label lblLog = new Label(" Log");
  578. TextArea txtLog = new TextArea();
  579. bLogin.setOnAction(e -> Login());
  580. btnRegistrati.setOnAction(e -> registrati());
  581. lNome.setVisible(false);
  582. tNome.setVisible(false);
  583. lCognome.setVisible(false);
  584. tCognome.setVisible(false);
  585.  
  586. lData.setVisible(false);
  587. tData.setVisible(false);
  588. lSesso.setVisible(false);
  589. tSesso.setVisible(false);
  590. btnIscrivi.setVisible(false);
  591. p.setPadding(new Insets(10,10,10,10));
  592. p.setHgap(10);
  593. p.setVgap(10);
  594. p.add(btnRegistrati, 0, 0);
  595. p.add(lUser, 0, 1);
  596. p.add(tUser, 1, 1);
  597. p.add(bLogin, 2, 1);
  598. p.add(lPass, 0, 2);
  599. p.add(tPass, 1, 2);
  600. p.add(lNome, 0, 4);
  601. p.add(tNome, 1, 4);
  602. p.add(lCognome, 0, 5);
  603. p.add(tCognome, 1, 5);
  604. p.add(lData, 0, 6);
  605. p.add(tData, 1, 6);
  606. p.add(lSesso, 0, 7);
  607. p.add(tSesso, 1, 7);
  608. p.add(btnIscrivi, 1, 8);
  609. p.add(lblLog, 0, 10);
  610. p.add(txtLog, 1, 10, 3, 1);
  611. }
  612. public static void main (String[]args) {
  613. launch(args);
  614. }
  615.  
  616. private void ScriviLog(String s) {
  617. txtLog.setText(txtLog.getText() + s + "\n");
  618. txtLog.setScrollTop(Double.MAX_VALUE);
  619. }
  620. }
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630. import java.sql.*;
  631.  
  632. import java.io.IOException;
  633. import java.net.Socket;
  634.  
  635. import java.io.InputStream;
  636. import java.io.OutputStream;
  637. import java.io.ObjectInputStream;
  638. import java.io.ObjectOutputStream;
  639.  
  640. public class ServerThread extends Thread{
  641.  
  642. Socket soc;
  643. InputStream iStream;
  644. OutputStream oStream;
  645. ObjectInputStream sIn;
  646. ObjectOutputStream sOut;
  647. String idSogg;
  648. String Nom;
  649. String idClas;
  650. String annoClasse;
  651. String numClas;
  652. String sezClas;
  653. String registrazione;
  654.  
  655.  
  656.  
  657. //variabili login
  658. String userId;
  659. String password;
  660.  
  661. //variabili soggetto
  662. String nominativo;
  663. String via;
  664. String civico;
  665. String CAP;
  666. String citta;
  667. String codiceFiscale;
  668. String partitaIva;
  669. String nomeRapp;
  670. String cognomeRapp;
  671. String cfRapp;
  672. int idAttività;
  673.  
  674. //variabili classe
  675. int idClasse;
  676. int anno;
  677. int numero;
  678. String sezione;
  679.  
  680.  
  681. String metodo;
  682. String combo;
  683.  
  684.  
  685. public ServerThread(Socket s){
  686. soc = s;
  687. }
  688.  
  689. public void run(){
  690. // apertura canale di comunicazione
  691. System.out.println(getName() + ": apertura canale di comunicazione...");
  692.  
  693. try{
  694. iStream = soc.getInputStream();
  695. oStream = soc.getOutputStream();
  696. sIn = new ObjectInputStream(iStream);
  697. sOut = new ObjectOutputStream(oStream);
  698. System.out.println(getName()+ ": ...canale di comunicazione aperto.");
  699.  
  700. // legge username e password
  701. userId = (String)sIn.readObject();
  702. password = (String)sIn.readObject();
  703.  
  704.  
  705.  
  706.  
  707.  
  708. System.out.println(getName() + ": Ricevute credenziali di accesso.");
  709. // controlliamo le credenziali
  710. try {
  711. Class.forName("com.mysql.jdbc.Driver");
  712. System.out.println("Caricamento Diver");
  713. }catch(ClassNotFoundException e) {
  714.  
  715. }
  716.  
  717. String url = "jdbc:mysql://localhost:3306?/scrum?user=root&password=";
  718.  
  719. System.out.println("Connessione al database in corso...");
  720. try (Connection connection = DriverManager.getConnection(url)) {
  721. System.out.println("Database connesso!");
  722. Statement stm = connection.createStatement();
  723. //controllo credenziali
  724.  
  725. ResultSet rs = stm.executeQuery("SELECT idStudente, password FROM studente");
  726.  
  727. while (rs.next()) {
  728. if(rs.getString("idStudente").equals(userId) && rs.getString("password").equals(password)) {
  729. System.out.println("Benvenuto " + userId);
  730. sOut.writeObject("stud");
  731.  
  732. sOut.flush();
  733. }
  734. }
  735. connection.close();
  736. }catch(SQLException e) {
  737. throw new IllegalStateException("Impossibile connettersi al database!", e);
  738. }
  739.  
  740.  
  741. try (Connection connection = DriverManager.getConnection(url)) {
  742. Statement stm = connection.createStatement();
  743. ResultSet rs = stm.executeQuery("SELECT idAmministratore, password FROM amministratore");
  744.  
  745. while (rs.next()) {
  746. if(rs.getString("idAmministratore").equals(userId) && rs.getString("password").equals(password)) {
  747. System.out.println("Benvenuto " + userId);
  748. sOut.writeObject("amm");
  749. sOut.flush();
  750.  
  751. }else {
  752. sOut.writeObject("errore");
  753. }
  754. }
  755. connection.close();
  756.  
  757. } catch (SQLException e) {
  758. throw new IllegalStateException("Impossibile connettersi al database!", e);
  759. }
  760.  
  761.  
  762.  
  763.  
  764. while(true) {
  765. metodo = (String)sIn.readObject();
  766.  
  767. //Inserimento soggetto
  768. try (Connection connection = DriverManager.getConnection(url)) {
  769. Statement stm = connection.createStatement();
  770.  
  771.  
  772.  
  773. if(metodo.equals("insSoggetto")) {
  774.  
  775.  
  776.  
  777. nominativo = (String)sIn.readObject();
  778. via = (String)sIn.readObject();
  779. civico = (String)sIn.readObject();
  780. CAP = (String)sIn.readObject();
  781. citta = (String)sIn.readObject();
  782. codiceFiscale = (String)sIn.readObject();
  783. partitaIva = (String)sIn.readObject();
  784. nomeRapp = (String)sIn.readObject();
  785. cognomeRapp = (String)sIn.readObject();
  786. cfRapp = (String)sIn.readObject();
  787.  
  788.  
  789.  
  790. stm.executeUpdate("INSERT INTO soggetto (Nominativo,via,civico,CAP,città,codiceFiscale,partitaIva,nomeRapp,cognomeRapp,cfRapp) VALUES('"+nominativo+"','"+via+"','"+civico+"','"+CAP+"','"+citta+"','"+codiceFiscale+"','"+partitaIva+"','"+nomeRapp+"','"+cognomeRapp+"','"+cfRapp+"')");
  791. }
  792. connection.close();
  793. } catch (SQLException e) {
  794. throw new IllegalStateException("Impossibile connettersi al database!", e);
  795. }
  796.  
  797. try (Connection connection = DriverManager.getConnection(url)) {
  798. Statement stm = connection.createStatement();
  799.  
  800. if(metodo.equals("confRegistrazione")) {
  801.  
  802. registrazione = (String)sIn.readObject();
  803.  
  804. stm.executeUpdate(registrazione);
  805. }
  806. connection.close();
  807. } catch (SQLException e) {
  808. throw new IllegalStateException("Impossibile connettersi al database!", e);
  809. }
  810.  
  811.  
  812.  
  813.  
  814.  
  815.  
  816. //Popolamento combobox Soggetto
  817. try (Connection connection = DriverManager.getConnection(url)) {
  818. Statement stm = connection.createStatement();
  819. Statement stm1 = connection.createStatement();
  820.  
  821. if(metodo.equals("comboSogg")) {
  822. idSogg = (String)sIn.readObject();
  823. Nom = (String)sIn.readObject();
  824. ResultSet rs = stm.executeQuery(idSogg);
  825. ResultSet rs1 = stm1.executeQuery(Nom);
  826. while (rs.next() && rs1.next()) {
  827. sOut.writeObject(rs.getString("idSoggetto") + "-" + rs1.getString("Nominativo"));
  828. sOut.flush();
  829.  
  830. }
  831. sOut.writeObject(null);
  832. sOut.flush();
  833.  
  834. }
  835. connection.close();
  836. } catch (SQLException e) {
  837. throw new IllegalStateException("Impossibile connettersi al database!", e);
  838. }
  839.  
  840.  
  841. //Popolamento combobox Classe
  842. try (Connection connection = DriverManager.getConnection(url)) {
  843. Statement stm = connection.createStatement();
  844.  
  845.  
  846. if(metodo.equals("comboClasse")) {
  847. idClas = (String)sIn.readObject();
  848. ResultSet rs = stm.executeQuery(idClas);
  849.  
  850. while (rs.next()) {
  851. sOut.writeObject(rs.getString("idClasse") + "-" + rs.getString("anno")+ "-" + rs.getString("numero")+ "-" + rs.getString("sezione"));
  852. sOut.flush();
  853.  
  854. }
  855. sOut.writeObject(null);
  856. sOut.flush();
  857.  
  858. }
  859. connection.close();
  860. } catch (SQLException e) {
  861. throw new IllegalStateException("Impossibile connettersi al database!", e);
  862. }
  863.  
  864. // Query modifica dati soggetto
  865. try (Connection connection = DriverManager.getConnection(url)) {
  866. Statement stm = connection.createStatement();
  867.  
  868.  
  869. if(metodo.equals("modificaSoggetto")) {
  870.  
  871. nominativo = (String)sIn.readObject();
  872. via = (String)sIn.readObject();
  873. civico = (String)sIn.readObject();
  874. CAP = (String)sIn.readObject();
  875. citta = (String)sIn.readObject();
  876. codiceFiscale = (String)sIn.readObject();
  877. partitaIva = (String)sIn.readObject();
  878. nomeRapp = (String)sIn.readObject();
  879. cognomeRapp = (String)sIn.readObject();
  880. cfRapp = (String)sIn.readObject();
  881. idAttività =Integer.parseInt((String) sIn.readObject());
  882.  
  883. stm.executeUpdate("UPDATE soggetto SET Nominativo = '"+nominativo+"',via = '"+via+"', civico = '"+civico+"',CAP = '"+CAP+"',città = '"+citta+"',codiceFiscale = '"+codiceFiscale+"',partitaIva = '"+partitaIva+"',nomeRapp = '"+nomeRapp+"',cognomeRapp = '"+cognomeRapp+"',cfRapp = '"+cfRapp+"' where idSoggetto = "+idAttività);
  884. }
  885. connection.close();
  886. } catch (SQLException e) {
  887. throw new IllegalStateException("Impossibile connettersi al database!", e);
  888. }
  889.  
  890.  
  891.  
  892. //Query inserisci dati classe
  893. try (Connection connection = DriverManager.getConnection(url)) {
  894. Statement stm = connection.createStatement();
  895.  
  896. if(metodo.equals("inserisciClasse")) {
  897.  
  898.  
  899. anno = Integer.parseInt((String) sIn.readObject());
  900. numero = Integer.parseInt((String) sIn.readObject());
  901. sezione = (String)sIn.readObject();
  902.  
  903. stm.executeUpdate("INSERT INTO classe (anno,numero,sezione) values("+anno+","+numero+",'"+sezione+"')");
  904. }
  905. connection.close();
  906. } catch (SQLException e) {
  907. throw new IllegalStateException("Impossibile connettersi al database!", e);
  908. }
  909.  
  910.  
  911. // Query modifica dati classe
  912. try (Connection connection = DriverManager.getConnection(url)) {
  913. Statement stm = connection.createStatement();
  914.  
  915.  
  916.  
  917. if(metodo.equals("modificaClasse")) {
  918.  
  919. idClasse = Integer.parseInt((String) sIn.readObject());
  920. anno = Integer.parseInt((String) sIn.readObject());
  921. numero = Integer.parseInt((String) sIn.readObject());
  922. sezione = (String)sIn.readObject();
  923.  
  924. stm.executeUpdate("UPDATE classe SET anno = "+anno+",numero = "+numero+", sezione = '"+sezione+"' where idClasse = "+idClasse);
  925. }
  926. connection.close();
  927. } catch (SQLException e) {
  928. throw new IllegalStateException("Impossibile connettersi al database!", e);
  929. }
  930.  
  931. }
  932. } catch(IOException | ClassNotFoundException e){
  933. System.out.println(e);
  934. }
  935.  
  936.  
  937.  
  938. }
  939.  
  940. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement