Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. import java.sql.*;
  2. import java.io.IOException;
  3. import java.net.Socket;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.io.ObjectInputStream;
  7. import java.io.ObjectOutputStream;
  8.  
  9. public class ServerThread extends Thread{
  10.  
  11. Socket soc;
  12. InputStream iStream;
  13. OutputStream oStream;
  14. ObjectInputStream sIn;
  15. ObjectOutputStream sOut;
  16. Studente pDaClient;
  17.  
  18. //variabili login
  19. String userId;
  20. String password;
  21.  
  22. //variabili soggetto
  23. String metodo;
  24. String nominativo;
  25. String via;
  26. String civico;
  27. String CAP;
  28. String citta;
  29. String codiceFiscale;
  30. String partitaIva;
  31. String nomeRapp;
  32. String cognomeRapp;
  33. String cfRapp;
  34. int idAttivita;
  35.  
  36.  
  37.  
  38. public ServerThread(Socket s){
  39. soc = s;
  40. }
  41.  
  42. public void run(){
  43. // apertura canale di comunicazione
  44. System.out.println(getName() + ": apertura canale di comunicazione...");
  45.  
  46. try{
  47. iStream = soc.getInputStream();
  48. oStream = soc.getOutputStream();
  49. sIn = new ObjectInputStream(iStream);
  50. sOut = new ObjectOutputStream(oStream);
  51. System.out.println(getName()+ ": ...canale di comunicazione aperto.");
  52.  
  53. // legge username e password
  54. userId = (String)sIn.readObject();
  55. password = (String)sIn.readObject();
  56.  
  57.  
  58.  
  59. System.out.println(getName() + ": Ricevute credenziali di accesso.");
  60. // controlliamo le credenziali
  61. try {
  62. Class.forName("com.mysql.jdbc.Driver");
  63. System.out.println("Caricamento Diver");
  64. }catch(ClassNotFoundException e) {
  65.  
  66. }
  67.  
  68. String url = "jdbc:mysql://localhost:3306/scrum?user=root&password=";
  69.  
  70. System.out.println("Connessione al database in corso...");
  71. try (Connection connection = DriverManager.getConnection(url)) {
  72. System.out.println("Database connesso!");
  73. Statement stm = connection.createStatement();
  74. //controllo credenziali
  75.  
  76. ResultSet rs = stm.executeQuery("SELECT idStudente, password FROM studente");
  77.  
  78. while (rs.next()) {
  79. if(rs.getString("idStudente").equals(userId) && rs.getString("password").equals(password)) {
  80. System.out.println("Benvenuto, " + userId + ", sei uno studente ");
  81. sOut.writeObject("stud");
  82. sOut.flush();
  83.  
  84. }
  85. }
  86. connection.close();
  87. } catch (SQLException e) {
  88. throw new IllegalStateException("Impossibile connettersi al database!", e);
  89. }
  90.  
  91. try (Connection connection = DriverManager.getConnection(url)) {
  92. System.out.println("Database connesso!");
  93.  
  94. Statement stm = connection.createStatement();
  95. ResultSet rs = stm.executeQuery("SELECT idAmministratore, password FROM amministratore");
  96.  
  97. while (rs.next()) {
  98. if(rs.getString("idAmministratore").equals(userId) && rs.getString("password").equals(password)) {
  99. System.out.println("Benvenuto, " + userId + ", sei un amministratore");
  100. sOut.writeObject("amm");
  101. sOut.flush();
  102.  
  103. }else {
  104. sOut.writeObject("Errore");
  105. }
  106. }
  107. connection.close();
  108. } catch (SQLException e) {
  109. throw new IllegalStateException("Impossibile connettersi al database!", e);
  110. }
  111. // legge dati soggetto
  112. metodo = (String)sIn.readObject();
  113. nominativo = (String)sIn.readObject();
  114. via = (String)sIn.readObject();
  115. civico = (String)sIn.readObject();
  116. CAP = (String)sIn.readObject();
  117. citta = (String)sIn.readObject();
  118. codiceFiscale = (String)sIn.readObject();
  119. partitaIva = (String)sIn.readObject();
  120. nomeRapp = (String)sIn.readObject();
  121. cognomeRapp = (String)sIn.readObject();
  122. cfRapp = (String)sIn.readObject();
  123.  
  124.  
  125. // Query per inserire dati soggetto
  126. try (Connection connection = DriverManager.getConnection(url)) {
  127. System.out.println("Database connesso!");
  128.  
  129. Statement stm = connection.createStatement();
  130. if(metodo.equals("insert")) {
  131. 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 + "');");
  132.  
  133. }
  134. connection.close();
  135. } catch (SQLException e) {
  136. throw new IllegalStateException("Impossibile connettersi al database!", e);
  137. }
  138. try (Connection connection = DriverManager.getConnection(url)) {
  139. System.out.println("Database connesso!");
  140.  
  141. Statement stm = connection.createStatement();
  142. if(metodo.equals("modifica")) {
  143. idAttivita = Integer.parseInt((String)sIn.readObject());
  144. String query = "UPDATE soggetto SET Nominativo = '"+ nominativo + "',via = '"+ via +"', civico = '" +civico +"',CAP = '" + CAP + "',città = '" + citta + "',codiceFiscale = '"+ codiceFiscale + "',partitaIva = '" +partitaIva+"',nomeRapp = '" + nomeRapp + "',cognomeRapp = '" + cognomeRapp + "',cfRapp = '" + cfRapp + "' WHERE idAttività = " + idAttivita;
  145. stm.executeUpdate(query);
  146. }
  147. connection.close();
  148. } catch (SQLException e) {
  149. throw new IllegalStateException("Impossibile connettersi al database!", e);
  150. }
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157. } catch(IOException | ClassNotFoundException e){
  158. System.out.println(e);
  159. }
  160.  
  161.  
  162.  
  163. }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement