Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Main
- package fr.elecsprint73;
- import java.security.NoSuchAlgorithmException;
- import java.sql.SQLException;
- import java.util.Scanner;
- public class Main {
- public static Sql sq;
- public static void main(String[] args) {
- String speudo;
- String mdp;
- String email;
- String urlbase = "jdbc:mysql://" ;
- String host = "localhost" ;
- String Database = "projet2019" ;
- String user = "root" ;
- String pass = "" ;
- sq = new Sql(urlbase, host, Database, user, pass);
- (ligne 23) sq.connection();
- if(!sq.isConnect()) System.exit(0);;
- Scanner sc = new Scanner(System.in);
- System.out.println("Votre speudo ?");
- speudo = sc.nextLine();
- System.out.println("Votre mot de passe ?");
- mdp = sc.nextLine();
- System.out.println("Votre email ?");
- email = sc.nextLine();
- try {
- sq.inscription(speudo, email, mdp);
- } catch (NoSuchAlgorithmException | SQLException e) {
- e.printStackTrace();
- }
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------------------
- Sql
- ---------------------------------------------------------------------------------------------------------------------------------------
- package fr.elecsprint73;
- import java.security.NoSuchAlgorithmException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import fr.elecsprint73.inscription.Inscription;
- public class Sql {
- public static Connection connection;
- String urlbase , host , Database , user , pass;
- public Sql(String urlbase , String host , String Database , String user , String pass) {
- this.urlbase = urlbase;
- this.host = host;
- this.Database = Database;
- this.user = user;
- this.pass = pass;
- }
- public void connection() {
- try {
- (ligne 27) connection = DriverManager.getConnection(urlbase + host + "/" + Database , user , pass);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public boolean isConnect() {
- return connection != null;
- }
- public void disconnection() {
- if(connection == null) return;
- try {
- connection.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void inscription(String speudo , String email ,String mdp) throws NoSuchAlgorithmException, SQLException {
- PreparedStatement ps = connection.prepareStatement("INSERT INTO inscription(UUID,SPEUDO,MDP,EMAIL) VALUES (?,?,?,?)");
- ps.setString(1, Inscription.UUID());
- ps.setString(2, speudo);
- ps.setString(3, Inscription.Hashage(mdp));
- ps.setString(4, email);
- ps.execute();
- ps.close();
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------------------
- Inscription
- ---------------------------------------------------------------------------------------------------------------------------------------
- package fr.elecsprint73.inscription;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- public class Inscription {
- public static String Hashage(String mdp) throws NoSuchAlgorithmException {
- MessageDigest md = MessageDigest.getInstance("SHA-256");
- md.update(mdp.getBytes());
- byte byteData[] = md.digest();
- StringBuffer sb = new StringBuffer();
- for (int i = 0; i < byteData.length; i++) {
- sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
- }
- return sb.toString();
- }
- public static String UUID() {
- return java.util.UUID.randomUUID().toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement