Package dao

Source Code of dao.EnderecoDAO

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package dao;

import controller.operating.Strings;
import model.Endereco;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import model.Pessoa;

/**
*
* @author IconeServidor
*/
public class EnderecoDAO {
   
    public Endereco getEndereco(String codigo) {

        Connection conexao = Conexao.getSqlConnection();
        Endereco endereco = new Endereco();
        try {
            Statement statement = conexao.createStatement();
            statement.executeQuery("SELECT * " +
                    "FROM endereco " +
                    "WHERE codigoPessoa = " + codigo);
           
            ResultSet resultSet = statement.getResultSet();
            while (resultSet.next()) {
                Pessoa pessoa = new Pessoa();
                pessoa.setCodigoPessoa(resultSet.getInt("codigoPessoa"));
               
                endereco = new Endereco(
                    resultSet.getString("logradouro"),
                    resultSet.getString("numero"),
                    resultSet.getString("bairro"),
                    resultSet.getString("cep"),
                    resultSet.getString("complemento"),
                    resultSet.getString("nomeEstado"),
                    resultSet.getString("uf"),
                    pessoa);
            }
            resultSet.close();
            statement.close();
        } catch (Exception erro) {
            JOptionPane.showMessageDialog(null, Strings.getMensagemErroRecuperacao("Endereço", erro.getMessage()), Strings.tituloMessageErro, JOptionPane.ERROR_MESSAGE);
        }
        return endereco;
    }

    public int getMaiorCodigo() {

        Connection conexao = Conexao.getSqlConnection();
        int intCodigo = 0;
        try {
            Statement statement = conexao.createStatement();
            statement.executeQuery("SELECT MAX(codigoPessoa) AS codigo FROM pessoa");

            ResultSet resultSet = statement.getResultSet();
            while (resultSet.next()) {
                intCodigo = resultSet.getInt("codigo");
            }
            resultSet.close();
            statement.close();

        } catch (Exception erro) {
            JOptionPane.showMessageDialog(null, Strings.getMensagemErroNovoCodigo("Endereço", erro.getMessage()), Strings.tituloMessageErro, JOptionPane.ERROR_MESSAGE);
        }
        return intCodigo;
    }

    public ArrayList<Endereco> getEnderecos() {
       
        Connection conexao = Conexao.getSqlConnection();
        ArrayList<Endereco> listaDeEnderecos = new ArrayList<Endereco>();
        try {
            Statement objSTM = conexao.createStatement();
            objSTM.executeQuery("SELECT * " +
                    "FROM endereco");

            ResultSet resultSet = objSTM.getResultSet();
            Endereco endereco;
            while (resultSet.next()) {
                Pessoa pessoa = new Pessoa();
                pessoa.setCodigoPessoa(resultSet.getInt("codigoPessoa"));
               
                endereco = new Endereco(
                    resultSet.getString("logradouro"),
                    resultSet.getString("numero"),
                    resultSet.getString("bairro"),
                    resultSet.getString("cep"),
                    resultSet.getString("complemento"),
                    resultSet.getString("nomeEstado"),
                    resultSet.getString("uf"),
                    pessoa);
                listaDeEnderecos.add(endereco);
            }
            resultSet.close();
            objSTM.close();
        } catch (Exception erro) {
            JOptionPane.showMessageDialog(null, Strings.getMensagemErroRecuperacao("Endereços", erro.getMessage()), Strings.tituloMessageErro, JOptionPane.ERROR_MESSAGE);
        }
        return listaDeEnderecos;

    }

    public boolean salvar(Endereco pEndereco) {
       
        Connection conexao = Conexao.getSqlConnection();
        try {
            Statement statement = conexao.createStatement();
            statement.execute("INSERT INTO endereco(" +
                    "logradouro, numero, bairro, cep, complemento, nomeCidade, nomeEstado, uf, codigoPessoa) " +
                    "VALUES(" +
                    "'" + pEndereco.getLogradouro() +
                    "', '" + pEndereco.getNumero() +
                    "', '" + pEndereco.getBairro() +
                    "', '" + pEndereco.getCep() +
                    "', '" + pEndereco.getComplemento() +
                    "', '" + pEndereco.getNomeCidade() +
                    "', '" + pEndereco.getNomeEstado() +
                    "', '" + pEndereco.getUf() +
                    "', " + pEndereco.getPessoa().getCodigoPessoa() + ")");
            statement.close();
       
            return true;
        } catch (Exception erro) {
            JOptionPane.showMessageDialog(null, Strings.getMensagemErroPersitencia("Endereço", erro.getMessage()), Strings.tituloMessageErro, JOptionPane.ERROR_MESSAGE);
           
            return false;
        }

    }

    public boolean atualizar(Endereco pEndereco) {

        Connection conexao = Conexao.getSqlConnection();
        try {
            Statement statement = conexao.createStatement();           
            statement.execute("UPDATE endereco " +
                    "SET logradouro = '" + pEndereco.getLogradouro() +
                    "', numero = '" + pEndereco.getNumero() +
                    "', bairro = '" + pEndereco.getBairro() +
                    "', cep = '" + pEndereco.getCep() +
                    "', complemento = '" + pEndereco.getComplemento() +
                    "', nomeCidade = '" + pEndereco.getNomeCidade() +
                    "', nomeEstado = '" + pEndereco.getNomeEstado() +
                    "', uf = '" + pEndereco.getUf() +
                    "' WHERE codigoPessoa = " + pEndereco.getPessoa().getCodigoPessoa());
           
            statement.close();
           
            return true;
        } catch (Exception erro) {
            JOptionPane.showMessageDialog(null, Strings.getMensagemErroEdicao("Endereço", erro.getMessage()), Strings.tituloMessageErro, JOptionPane.ERROR_MESSAGE);
           
            return false;
        }

    }

    public boolean excluir(String codigo) {

        Connection conexao = Conexao.getSqlConnection();
        try {
            Statement statement = conexao.createStatement();
            statement.execute("DELETE FROM endereco WHERE codigoPessoa = '" + codigo + "'");
            statement.close();
           
            return true;
        } catch (Exception erro) {
            JOptionPane.showMessageDialog(null, Strings.getMensagemErroExclusao("Endereço", erro.getMessage()), Strings.tituloMessageErro, JOptionPane.ERROR_MESSAGE);
           
            return false;
        }
    }
}
TOP

Related Classes of dao.EnderecoDAO

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.