Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SQL_Project;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class Utilisateur {
- public static java.util.Scanner scanner = new java.util.Scanner(System.in);
- private String url = "jdbc:postgresql://localhost:5432/postgres"; // LOCAL
- //private String url="jdbc:postgresql://172.24.2.6:5432/gcurato16"; // ECOLE
- private Connection conn=null;
- public Utilisateur() {
- // test driver
- try {
- Class.forName("org.postgresql.Driver");
- } catch (ClassNotFoundException e) {
- System.out.println("Driver PostgreSQL manquant !");
- System.exit(1);
- }
- // test connection
- try {
- conn = DriverManager.getConnection(url, "gcurato16", "(9YAVaP@");
- } catch (SQLException e) {
- System.out.println("Impossible de joindre le serveur");
- System.exit(1);
- }
- }
- // Inscription phase
- private void inscriptionUtilisateur() {
- try {
- System.out.println("Veuillez trouver un nom d'utilisateur");
- String nom_utilisateur = scanner.next();
- System.out.println("Veuillez entrer votre email,");
- String email = scanner.next();
- System.out.println("votre mot de passe,");
- String mot_de_passe = scanner.next();
- PreparedStatement ps;
- ps = conn.prepareStatement("SELECT projet.ajouterUtilisateur(?,?,?);");
- ps.setString(1,nom_utilisateur);
- ps.setString(2,email);
- ps.setString(3,mot_de_passe);
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- System.out.println("Inscription bien effectuée");
- }
- }catch (SQLException se){
- System.out.println("Error, "+se.getMessage());
- System.exit(1);
- }
- }
- // authentification phase
- private void authentification() {
- System.out.println("Bonjour, veuillez-vous identifier svp:");
- System.out.println("Login:");
- String s = scanner.next();
- System.out.println("Mot de Passe:");
- String s2 = scanner.next();
- if (!authentification(conn, s, s2)){
- System.out.println("Votre login/password est incorrect");
- System.exit(1);
- }
- }
- /* authentification methode */
- /**
- * @POST: return a boolean depending on the authentification's success
- * @param connection
- * @param nom_utilisateur
- * @param mdp
- */
- public static boolean authentification(Connection connection, String nom_utilisateur, String mdp){
- try {
- Statement statement = connection.createStatement();
- try (ResultSet rt = statement.executeQuery("SELECT * FROM projet.utilisateurs WHERE nom_utilisateur=" + "'" + nom_utilisateur + "';")) {// renvoie le tuple spécifié
- while (rt.next()){
- if (rt.getString(4).equals(mdp))
- return true;
- }
- }
- } catch (SQLException se) {
- return false;
- }
- return false;
- }
- // fermer connection
- public void close() {
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement