/*
 * 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 bdd;

import java.sql.Statement;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javabeans.User;
/**
 *
 * @author sio
 */
public class SalarieMySQL {
    //Connexion MV serveurBD lycée 
    private final Connection laConnection = Connexion.getConnect("10.121.38.106", "bdclient", "adminBDClient", "mdpBDClient");
 
    /*Connexion wampserver
    private final Connection laConnection = Connexion.getConnect("localhost", "bdclient", "adminBDClient", "mdpBDClient");*/
    
    //connexion MV serveurBD maison
   // private final Connection laConnection = Connexion.getConnect("192.168.1.21", "bdclient", "adminBDClient", "mdpBDClient");

   
    
    public ArrayList<User> readAll(){
        User leSalarie=new User("lo","log");
        ArrayList <User> lesSalaries = new ArrayList<>();
        try{
            Statement stmt = laConnection.createStatement();
            ResultSet resultQ = null;
            resultQ = stmt.executeQuery("SELECT * FROM salarie");
            while(resultQ.next()){
                leSalarie.setLogin(resultQ.getString("login")); 
                leSalarie.setId(resultQ.getInt("idSal"));
                leSalarie.setMdp(resultQ.getString("mdp"));
                leSalarie.setRole(resultQ.getBoolean("role"));
                lesSalaries.add(leSalarie);
            }
            resultQ.close();
            stmt.close();
        }catch (SQLException ex){
            System.out.println("SQLException : " + ex.getMessage());
            System.out.println("SQLState : " + ex.getSQLState());
            System.out.println("Code erreur : " + ex.getErrorCode());
        }
        return lesSalaries;
    }
    
    public User lireSalarie(String log, String m){
        User leSalarie=new User();
        ResultSet resultQ = null;
        try{
           
            PreparedStatement prepStmt = null;

            String sql = "SELECT * FROM Salarie WHERE login = ? AND mdp=?";
            prepStmt = laConnection.prepareStatement(sql); // pré-compilation

            prepStmt.setString(1, log); //le param. 1 est remplacé par "Toto"
             prepStmt.setString(2,m);
            resultQ = prepStmt.executeQuery();
            if (resultQ.first()){
                leSalarie.setLogin(resultQ.getString("login")); 
                leSalarie.setId(resultQ.getInt("idSal"));
                leSalarie.setMdp(resultQ.getString("mdp"));
                leSalarie.setRole(resultQ.getBoolean("role"));
            }
            resultQ.close();
            prepStmt.close();
        }catch (SQLException ex){
            System.out.println("SQLException : " + ex.getMessage());
            System.out.println("SQLState : " + ex.getSQLState());
            System.out.println("Code erreur : " + ex.getErrorCode());
        }
        return leSalarie;
    }
}
