/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package emf.wrk;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author DiasMadeiraL
 */
public class WrkDB {

    private Connection conn = null;

        public boolean ouvrirDB() {
        boolean isOk = false;
        try {
            if (conn == null) {
                isOk = open();
            } else if (!conn.isClosed()) {
                isOk = true;
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        

        return isOk;
    }

    private boolean open() {
        boolean ok = false;
        String piloteOBDCPathDb = "jdbc:mysql://localhost:3306/133ex04db" + "?zeroDateTimeBehavior=convertToNull";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(piloteOBDCPathDb, "root", "");
            ok = true;
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return ok;
    }

    private boolean close() {
        boolean ok = false;
        try {
            if (conn != null) {
                if (!conn.isClosed()) {
                    conn.close();
                }
            }
            ok = true;
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        return ok;
    }
    
    public boolean checkLogin(String user, String password) {
        boolean results = false;
        String statement = "SELECT count(*) FROM 133ex04db.t_login WHERE BINARY user=? AND BINARY password =? ;";

        try {
            PreparedStatement prepared = conn.prepareStatement(statement);
            prepared.setString(1, user);
            prepared.setString(2, password);
            ResultSet resultset = prepared.executeQuery();

            while (resultset.next()) {
                if (resultset.getInt(1) != 0) {
                    results = true;
                }
            }

        } catch (Exception e) {
            System.out.println(e.getMessage() + " CAUSE : " + e.getCause());
        }

        return results;
    }
    
        public String checkLogin2(String user, String password) {
        String results = "MARCHE PAS";
        String statement = "SELECT count(*) FROM 133ex04db.t_login WHERE BINARY user=? AND BINARY password =? ;";

        try {
            PreparedStatement prepared = conn.prepareStatement(statement);
            results = "STEP1";
            prepared.setString(1, user);
            prepared.setString(2, password);
            results = "STEP2";
            
            ResultSet resultset = prepared.executeQuery();
            results = "STEP3";
            
            while (resultset.next()) {
                if (resultset.getInt(1) != 0) {
                    results = "OK";
                }
            }
        } catch (Exception e) {
            //results = e.getMessage() + " CAUSE : " + e.getCause();
        }

        return results;
    }

    
}
