Advertisement
Guest User

Untitled

a guest
Nov 30th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 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. private String url = "jdbc:postgresql://localhost:5432/postgres"; // LOCAL
  14. //private String url="jdbc:postgresql://172.24.2.6:5432/gcurato16"; // ECOLE
  15. private Connection conn=null;
  16.  
  17.  
  18. public Utilisateur() {
  19.  
  20. // test driver
  21. try {
  22. Class.forName("org.postgresql.Driver");
  23.  
  24. } catch (ClassNotFoundException e) {
  25. System.out.println("Driver PostgreSQL manquant !");
  26. System.exit(1);
  27. }
  28.  
  29.  
  30. // test connection
  31. try {
  32. conn = DriverManager.getConnection(url, "gcurato16", "(9YAVaP@");
  33.  
  34. } catch (SQLException e) {
  35. System.out.println("Impossible de joindre le serveur");
  36. System.exit(1);
  37. }
  38. }
  39.  
  40. // Inscription phase
  41. private void inscriptionUtilisateur() {
  42.  
  43. try {
  44. System.out.println("Veuillez trouver un nom d'utilisateur");
  45. String nom_utilisateur = scanner.next();
  46. System.out.println("Veuillez entrer votre email,");
  47. String email = scanner.next();
  48. System.out.println("votre mot de passe,");
  49. String mot_de_passe = scanner.next();
  50. PreparedStatement ps;
  51. ps = conn.prepareStatement("SELECT projet.ajouterUtilisateur(?,?,?);");
  52. ps.setString(1,nom_utilisateur);
  53. ps.setString(2,email);
  54. ps.setString(3,mot_de_passe);
  55. ResultSet rs = ps.executeQuery();
  56. while(rs.next()){
  57. System.out.println("Inscription bien effectuée");
  58. }
  59. }catch (SQLException se){
  60. System.out.println("Error, "+se.getMessage());
  61. System.exit(1);
  62. }
  63. }
  64.  
  65. // authentification phase
  66. private void authentification() {
  67. System.out.println("Bonjour, veuillez-vous identifier svp:");
  68. System.out.println("Login:");
  69. String s = scanner.next();
  70. System.out.println("Mot de Passe:");
  71. String s2 = scanner.next();
  72. if (!authentification(conn, s, s2)){
  73. System.out.println("Votre login/password est incorrect");
  74. System.exit(1);
  75. }
  76. }
  77. /* authentification methode */
  78.  
  79. /**
  80. * @POST: return a boolean depending on the authentification's success
  81. * @param connection
  82. * @param nom_utilisateur
  83. * @param mdp
  84. */
  85. public static boolean authentification(Connection connection, String nom_utilisateur, String mdp){
  86. try {
  87. Statement statement = connection.createStatement();
  88. try (ResultSet rt = statement.executeQuery("SELECT * FROM projet.utilisateurs WHERE nom_utilisateur=" + "'" + nom_utilisateur + "';")) {// renvoie le tuple spécifié
  89. while (rt.next()){
  90. if (rt.getString(4).equals(mdp))
  91. return true;
  92. }
  93. }
  94. } catch (SQLException se) {
  95. return false;
  96. }
  97. return false;
  98. }
  99.  
  100.  
  101.  
  102.  
  103.  
  104. // fermer connection
  105. public void close() {
  106. try {
  107. conn.close();
  108. } catch (SQLException e) {
  109. e.printStackTrace();
  110. }
  111. }
  112.  
  113. public static void main(String[] args) {
  114.  
  115. }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement