131 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			131 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
/*
 | 
						|
 * 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 com.test.bdd;
 | 
						|
import java.sql.*;
 | 
						|
import java.util.ArrayList;
 | 
						|
import java.util.List;
 | 
						|
import javabeans.Client;
 | 
						|
 | 
						|
/**
 | 
						|
 *
 | 
						|
 * @author josephine.vetu
 | 
						|
 */
 | 
						|
public class ClientMysql {
 | 
						|
    
 | 
						|
    private final Connection theConnection=Connexion.getConnect("10.121.38.104", "bdclient", "adminBDClient", "mdpBDClient");
 | 
						|
 | 
						|
    
 | 
						|
    public void create(String nom, String prenom, String mail) throws SQLException{
 | 
						|
        try {
 | 
						|
            String sql = "INSERT INTO client(nom, prenom, mail) VALUES (?, ?, ?)";
 | 
						|
            try (PreparedStatement createStmt = theConnection.prepareStatement(sql)) {
 | 
						|
                createStmt.setString(1, nom);
 | 
						|
                createStmt.setString(2, prenom);
 | 
						|
                createStmt.setString(3, mail);
 | 
						|
                createStmt.executeUpdate();
 | 
						|
                System.out.println("Un client a été ajouté !");
 | 
						|
            }
 | 
						|
}
 | 
						|
            catch (SQLException ex) {
 | 
						|
            System.out.println("SQLExeption : " + ex.getMessage());
 | 
						|
            System.out.println("SQLState : " + ex.getSQLState());
 | 
						|
            System.out.println("Code erreur : " + ex.getErrorCode());
 | 
						|
            
 | 
						|
        }
 | 
						|
 | 
						|
    }
 | 
						|
        
 | 
						|
    public Client read(int id) throws SQLException{
 | 
						|
        Client leClient = null;
 | 
						|
        
 | 
						|
        try {
 | 
						|
            String sql = "SELECT * FROM client WHERE id=?";
 | 
						|
            try (PreparedStatement readStmt = theConnection.prepareStatement(sql)) {
 | 
						|
                readStmt.setInt(1, id);
 | 
						|
                ResultSet result = readStmt.executeQuery();
 | 
						|
                if (result.next()){
 | 
						|
                    String nom = result.getString("nom");
 | 
						|
                    String prenom = result.getString("prenom");
 | 
						|
                    String mail = result.getString("mail");
 | 
						|
                    leClient = new Client(id, nom, prenom, mail);
 | 
						|
                }
 | 
						|
            }
 | 
						|
            
 | 
						|
        } catch (SQLException ex) {
 | 
						|
            System.out.println("SQLExeption : " + ex.getMessage());
 | 
						|
            System.out.println("SQLState : " + ex.getSQLState());
 | 
						|
            System.out.println("Code erreur : " + ex.getErrorCode());
 | 
						|
            
 | 
						|
        }
 | 
						|
        return leClient;
 | 
						|
    }
 | 
						|
        
 | 
						|
    public void update(int id, String nom, String prenom, String mail) throws SQLException{
 | 
						|
        try {
 | 
						|
            String sql = "UPDATE client SET nom=?, prenom=?, mail=? WHERE id=?";
 | 
						|
            try (PreparedStatement updateStmt = theConnection.prepareStatement(sql)) {
 | 
						|
                updateStmt.setInt(1, id);
 | 
						|
                updateStmt.setString(2, nom);
 | 
						|
                updateStmt.setString(3, prenom);
 | 
						|
                updateStmt.setString(4, mail);
 | 
						|
                updateStmt.executeUpdate();
 | 
						|
                System.out.println("Un client a été ajouté !");
 | 
						|
            }
 | 
						|
        }
 | 
						|
        catch (SQLException ex) {
 | 
						|
            System.out.println("SQLExeption : " + ex.getMessage());
 | 
						|
            System.out.println("SQLState : " + ex.getSQLState());
 | 
						|
            System.out.println("Code erreur : " + ex.getErrorCode());
 | 
						|
            
 | 
						|
        }
 | 
						|
    }
 | 
						|
        
 | 
						|
    public void delete(int id) throws SQLException{
 | 
						|
        try {
 | 
						|
            String sql = "DELETE FROM client WHERE id=?";
 | 
						|
            PreparedStatement deleteStmt = theConnection.prepareStatement(sql);
 | 
						|
            deleteStmt.setInt(1, id);
 | 
						|
            deleteStmt.executeQuery();
 | 
						|
        }
 | 
						|
        catch (SQLException ex) {
 | 
						|
            System.out.println("SQLExeption : " + ex.getMessage());
 | 
						|
            System.out.println("SQLState : " + ex.getSQLState());
 | 
						|
            System.out.println("Code erreur : " + ex.getErrorCode());
 | 
						|
            
 | 
						|
        }
 | 
						|
    }
 | 
						|
    
 | 
						|
        
 | 
						|
public ArrayList<Client> readAll(){
 | 
						|
    ArrayList<Client> lesClients = new ArrayList<>();
 | 
						|
 | 
						|
    // Loop through all IDs and retrieve each client
 | 
						|
        String sql = "SELECT * FROM client;";
 | 
						|
            try (PreparedStatement readStmt = theConnection.prepareStatement(sql)) {
 | 
						|
                ResultSet result = readStmt.executeQuery();
 | 
						|
                while (result.next())
 | 
						|
                {
 | 
						|
                    int id = result.getInt("id");
 | 
						|
                    String nom = result.getString("nom");
 | 
						|
                    String prenom = result.getString("prenom");
 | 
						|
                    String mail = result.getString("mail");
 | 
						|
                    Client leClient = new Client(id, nom, prenom, mail);
 | 
						|
                    lesClients.add(leClient);
 | 
						|
                }
 | 
						|
                readStmt.close();
 | 
						|
            
 | 
						|
        } catch (SQLException ex) {
 | 
						|
            System.out.println("SQLExeption : " + ex.getMessage());
 | 
						|
            System.out.println("SQLState : " + ex.getSQLState());
 | 
						|
            System.out.println("Code erreur : " + ex.getErrorCode());
 | 
						|
            
 | 
						|
        }
 | 
						|
        return lesClients;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
}
 | 
						|
 |