/*
 * 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 javabeans.Client;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
 *
 * @author sio
 */
public class ClientMySQL {
    //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 int create(Client c){
        int id=-1;
        try{
            Statement stmt = laConnection.createStatement();
            int status= stmt.executeUpdate(
                    " INSERT INTO client (nom, prenom, mail) VALUES ('"
                    +c.getNom()     +"','"
                    +c.getPrenom()  +"','"
                    +c.getMail()    +"');",
            Statement.RETURN_GENERATED_KEYS);
            if (status>0)
            {
                ResultSet result=stmt.getGeneratedKeys();
                if (result.first()){
                    id = result.getInt(1);
                }
            }
           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 id;
    }
    
    public Client read(int num){
        return null;
    }
    
    public void update(int num){
        
    }
    
    public void delete(int num){
        
    }
    
    public ArrayList<String> readAll(){
        ArrayList <String> lesClients = new ArrayList<>();
        try{
            Statement stmt = laConnection.createStatement();
            ResultSet resultQ = null;
            resultQ = stmt.executeQuery("SELECT * FROM client");
            while(resultQ.next()){
                String resultat = resultQ.getString("nom") + " " + resultQ.getString("prenom");
                lesClients.add(resultat);
            }
            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 lesClients;
    }
}
