Advertisement
Guest User

Untitled

a guest
Jan 4th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. Main
  2.  
  3. package fr.elecsprint73;
  4.  
  5. import java.security.NoSuchAlgorithmException;
  6. import java.sql.SQLException;
  7. import java.util.Scanner;
  8.  
  9. public class Main {
  10.  
  11. public static Sql sq;
  12.  
  13. public static void main(String[] args) {
  14.  
  15. String speudo;
  16. String mdp;
  17. String email;
  18. String urlbase = "jdbc:mysql://" ;
  19. String host = "localhost" ;
  20. String Database = "projet2019" ;
  21. String user = "root" ;
  22. String pass = "" ;
  23.  
  24. sq = new Sql(urlbase, host, Database, user, pass);
  25. (ligne 23) sq.connection();
  26.  
  27. if(!sq.isConnect()) System.exit(0);;
  28.  
  29. Scanner sc = new Scanner(System.in);
  30.  
  31. System.out.println("Votre speudo ?");
  32. speudo = sc.nextLine();
  33.  
  34. System.out.println("Votre mot de passe ?");
  35. mdp = sc.nextLine();
  36.  
  37. System.out.println("Votre email ?");
  38. email = sc.nextLine();
  39.  
  40. try {
  41. sq.inscription(speudo, email, mdp);
  42. } catch (NoSuchAlgorithmException | SQLException e) {
  43. e.printStackTrace();
  44. }
  45.  
  46.  
  47. }
  48. }
  49. ---------------------------------------------------------------------------------------------------------------------------------------
  50. Sql
  51. ---------------------------------------------------------------------------------------------------------------------------------------
  52. package fr.elecsprint73;
  53.  
  54. import java.security.NoSuchAlgorithmException;
  55. import java.sql.Connection;
  56. import java.sql.DriverManager;
  57. import java.sql.PreparedStatement;
  58. import java.sql.SQLException;
  59.  
  60. import fr.elecsprint73.inscription.Inscription;
  61.  
  62. public class Sql {
  63.  
  64. public static Connection connection;
  65. String urlbase , host , Database , user , pass;
  66.  
  67. public Sql(String urlbase , String host , String Database , String user , String pass) {
  68.  
  69. this.urlbase = urlbase;
  70. this.host = host;
  71. this.Database = Database;
  72. this.user = user;
  73. this.pass = pass;
  74. }
  75.  
  76. public void connection() {
  77. try {
  78. (ligne 27) connection = DriverManager.getConnection(urlbase + host + "/" + Database , user , pass);
  79. } catch (SQLException e) {
  80. e.printStackTrace();
  81. }
  82. }
  83.  
  84. public boolean isConnect() {
  85. return connection != null;
  86. }
  87.  
  88. public void disconnection() {
  89.  
  90. if(connection == null) return;
  91.  
  92. try {
  93. connection.close();
  94. } catch (SQLException e) {
  95. e.printStackTrace();
  96. }
  97.  
  98. }
  99.  
  100. public void inscription(String speudo , String email ,String mdp) throws NoSuchAlgorithmException, SQLException {
  101.  
  102. PreparedStatement ps = connection.prepareStatement("INSERT INTO inscription(UUID,SPEUDO,MDP,EMAIL) VALUES (?,?,?,?)");
  103. ps.setString(1, Inscription.UUID());
  104. ps.setString(2, speudo);
  105. ps.setString(3, Inscription.Hashage(mdp));
  106. ps.setString(4, email);
  107. ps.execute();
  108. ps.close();
  109.  
  110. }
  111. }
  112. ---------------------------------------------------------------------------------------------------------------------------------------
  113. Inscription
  114. ---------------------------------------------------------------------------------------------------------------------------------------
  115. package fr.elecsprint73.inscription;
  116.  
  117. import java.security.MessageDigest;
  118. import java.security.NoSuchAlgorithmException;
  119.  
  120. public class Inscription {
  121.  
  122. public static String Hashage(String mdp) throws NoSuchAlgorithmException {
  123.  
  124. MessageDigest md = MessageDigest.getInstance("SHA-256");
  125. md.update(mdp.getBytes());
  126.  
  127. byte byteData[] = md.digest();
  128.  
  129. StringBuffer sb = new StringBuffer();
  130. for (int i = 0; i < byteData.length; i++) {
  131.  
  132. sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
  133.  
  134. }
  135.  
  136. return sb.toString();
  137. }
  138.  
  139. public static String UUID() {
  140.  
  141. return java.util.UUID.randomUUID().toString();
  142.  
  143. }
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement