Guest User

Untitled

a guest
Dec 4th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. package SQL_Project;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10.  
  11. public class Utilisateur {
  12. public static java.util.Scanner scanner = new java.util.Scanner(System.in);
  13.  
  14. private String url = "jdbc:postgresql://localhost:5432/postgres"; // LOCAL
  15. //private String url="jdbc:postgresql://172.24.2.6:5432/gcurato16"; // ECOLE
  16.  
  17. private PreparedStatement psAjoutUtilisateur;
  18. private PreparedStatement psAuthentification;
  19. private PreparedStatement psAjoutQuestion;
  20. private PreparedStatement psAjoutReponse;
  21. private Connection conn=null;
  22.  
  23.  
  24. public Utilisateur() {
  25.  
  26. // test driver
  27. try {
  28. Class.forName("org.postgresql.Driver");
  29.  
  30. } catch (ClassNotFoundException e) {
  31. System.out.println("Driver PostgreSQL manquant !");
  32. System.exit(1);
  33. }
  34.  
  35.  
  36. // test connection
  37. try {
  38. conn = DriverManager.getConnection(url, "gcurato16", "(9YAVaP@");
  39.  
  40. } catch (SQLException e) {
  41. System.out.println("Impossible de joindre le serveur");
  42. System.exit(1);
  43. }
  44. try {
  45. psAjoutUtilisateur = conn.prepareStatement("SELECT projet.ajouterUtilisateur(?,?,?);");
  46. psAjoutQuestion = conn.prepareStatement("SELECT projet.ajouterQuestions(?,?,?);");
  47. psAjoutReponse = conn.prepareStatement("SELECT projet.ajouterReponse(?,?,?);");
  48. psAuthentification = conn.prepareStatement("SELECT * from projet.Utilisateur WHERE desactive = 'false'");
  49. }catch (SQLException se){
  50. System.out.println("Error, "+se.getMessage());
  51. System.exit(1);
  52. }
  53.  
  54. }
  55.  
  56. // Inscription phase
  57. private void inscriptionUtilisateur() {
  58.  
  59. try {
  60. System.out.println("Veuillez trouver un nom d'utilisateur");
  61. String nom_utilisateur = scanner.next();
  62. System.out.println("Veuillez entrer votre email,");
  63. String email = scanner.next();
  64. System.out.println("votre mot de passe,");
  65. String mot_de_passe = scanner.next();
  66. psAjoutUtilisateur.setString(1,nom_utilisateur);
  67. psAjoutUtilisateur.setString(2,email);
  68. psAjoutUtilisateur.setString(3,mot_de_passe);
  69. ResultSet rs = psAjoutUtilisateur.executeQuery();
  70. while(rs.next()){
  71. System.out.println("Inscription bien effectuée");
  72. }
  73. }catch (SQLException se){
  74. System.out.println("Error, "+se.getMessage());
  75. System.exit(1);
  76. }
  77. }
  78.  
  79. // authentification phase
  80. private void authentification() {
  81. System.out.println("Bonjour, veuillez-vous identifier svp:");
  82. System.out.println("Login:");
  83. String s = scanner.next();
  84. System.out.println("Mot de Passe:");
  85. String s2 = scanner.next();
  86. if (!authentification(conn, s, s2)){
  87. System.out.println("Votre login/password est incorrect ou votre compte est désactivé ");
  88. System.exit(1);
  89. }
  90. }
  91. /* authentification methode */
  92.  
  93. /**
  94. * @POST: return a boolean depending on the authentification's success
  95. * @param connection
  96. * @param nom_utilisateur
  97. * @param mdp
  98. */
  99. public boolean authentification(Connection connection, String nom_utilisateur, String mdp){
  100. try (ResultSet rt = psAuthentification.executeQuery()) {
  101. while (rt.next()){
  102. if (rt.getString(4).equals(mdp) && rt.getString(2).equals(nom_utilisateur))
  103. return true;
  104. }
  105. } catch (SQLException se) {
  106. return false;
  107. }
  108. return false;
  109. }
  110.  
  111.  
  112. // AjouterQuestion
  113. public int ajouterQuestion(int num_utilisateur){
  114. try {
  115.  
  116. System.out.println("Veuillez inserer le titre de votre question");
  117. String titre = scanner.next();
  118. System.out.println("Veuillez ecrire le contenu de votre question");
  119. String contenu = scanner.next();
  120. psAjoutQuestion.setString(1, titre);
  121. psAjoutQuestion.setString(2, contenu);
  122. psAjoutQuestion.setInt(3, num_utilisateur);
  123. ResultSet rs = psAjoutQuestion.executeQuery();
  124. while(rs.next()) {
  125. return rs.getInt(1);
  126. }
  127.  
  128. }catch (SQLException se){
  129. System.out.println("Erreur, "+se.getMessage());
  130. }
  131. return -1;
  132. }
  133.  
  134.  
  135. //ajouterReponse
  136. public int ajouterReponse(int num_auteur, int num_question) {
  137. try {
  138. System.out.println("Veuillez entrer le contenu de votre réponse");
  139. String contenu = scanner.next();
  140. psAjoutReponse.setString(1, contenu);
  141. psAjoutReponse.setInt(2, num_question);
  142. psAjoutReponse.setInt(3, num_auteur);
  143. ResultSet rs = psAjoutReponse.executeQuery();
  144. while(rs.next()) {
  145. return rs.getInt(2);
  146. }
  147. }catch(SQLException se) {
  148. System.out.println("Erreur, "+se.getMessage());
  149. }
  150. return -1;
  151. }
  152.  
  153.  
  154.  
  155.  
  156.  
  157. // fermer connection
  158. public void close() {
  159. try {
  160. conn.close();
  161. } catch (SQLException e) {
  162. e.printStackTrace();
  163. }
  164. }
  165.  
  166. public static void main(String[] args) {
  167.  
  168. }
  169.  
  170. }
Add Comment
Please, Sign In to add comment