/*
 * GerenteBD.java
 *
 * Created on 27 de Abril de 2006, 09:52
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package veterinaria;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author aluno
 */
public class ClienteDAO {
    Connection conexao;
    int id;
    
    /** Creates a new instance of GerenteBD */
    public ClienteDAO(Connection c) {
        conexao=c;
    }
    
    
    
    public boolean incluirCliente(Cliente cliente){
        String sql= "INSERT INTO Cliente(nome, CPF, telefone, endereco) VALUES(?, ?, ?, ?)";
        PreparedStatement pst;
        try {
            pst = conexao.prepareStatement(sql);
            pst.setString(1, cliente.getNome());
            pst.setString(2, cliente.getCPF());
            pst.setString(3, cliente.getTelefone());
            pst.setString(4, cliente.getEndereco());
            pst.executeUpdate();
            cliente.setId(obterValorId());
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }
    private int obterValorId() {
        String sql="select max(id) from Cliente";
        PreparedStatement pst2;
        try {
            pst2 = conexao.prepareStatement(sql);
            ResultSet rs = pst2.executeQuery();
            if(rs.next()) {
                int valor = rs.getInt(1);
                id = valor;
                return valor;
            } else {
                return 1;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return 0;
    }
    public boolean excluirCliente(Cliente cliente){
        String sql= "DELETE FROM  Cliente where id = ?";
        PreparedStatement pst2;
        try {
            pst2 = conexao.prepareStatement(sql);
            pst2.setInt(1,id);
            int r = pst2.executeUpdate();
            if(r == 1)
                return true;
            else 
                return false;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }
}



