Package dao

Source Code of dao.AvaliacaoDAO

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

import controller.AulaAvaliativaController;
import controller.operating.Strings;
import model.Avaliacao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import model.AulaAvaliativa;
import util.Data;

/**
*
* @author Consult
*/
public class AvaliacaoDAO {

    public Avaliacao getAvaliacao(String codigo) {

        Connection conexao = Conexao.getSqlConnection();
        Avaliacao avaliacao = new Avaliacao();
       
        try {
            Statement statement = conexao.createStatement();
            statement.executeQuery("SELECT * FROM avaliacao WHERE codigoAvaliacao = " + codigo);
            AulaAvaliativaController entrevistaCtrl = new AulaAvaliativaController();
            ResultSet resultSet = statement.getResultSet();
            while (resultSet.next()) {
                AulaAvaliativa aulaAvaliativa = new AulaAvaliativa();
                aulaAvaliativa = entrevistaCtrl.getAulaAvaliativa(String.valueOf(resultSet.getInt("codigoAulaAvaliativa")));
               
                avaliacao = new Avaliacao();
                avaliacao.setCodigoAvaliacao(resultSet.getInt("codigoAvaliacao"));
                avaliacao.setData(Data.converteDataMysqlBr(resultSet.getString("data")));
                avaliacao.setHora(resultSet.getString("hora"));
                avaliacao.setTema(resultSet.getString("tema"));
                avaliacao.setDisciplina(resultSet.getString("disciplina"));
                avaliacao.setAulaAvaliativa(aulaAvaliativa);
            }

            resultSet.close();
            statement.close();
        } catch (Exception erro) {
            JOptionPane.showMessageDialog(null, Strings.getMensagemErroRecuperacao("Avaliação", erro.getMessage()), Strings.tituloMessageErro, JOptionPane.ERROR_MESSAGE);
        }
        return avaliacao;
    }

    public ArrayList<Avaliacao> getAvaliacaos() {

        Connection conexao = Conexao.getSqlConnection();
        ArrayList<Avaliacao> listaDeAvaliacaos = new ArrayList<Avaliacao>();

        try {
            Statement statement = conexao.createStatement();
            statement.executeQuery("SELECT * FROM avaliacao");

            ResultSet resultSet = statement.getResultSet();
            Avaliacao avaliacao;
            AulaAvaliativaController entrevistaCtrl = new AulaAvaliativaController();

            while (resultSet.next()) {
                AulaAvaliativa aulaAvaliativa = new AulaAvaliativa();
                aulaAvaliativa = entrevistaCtrl.getAulaAvaliativa(String.valueOf(resultSet.getInt("codigoAulaAvaliativa")));
                avaliacao = new Avaliacao();
                avaliacao.setCodigoAvaliacao(resultSet.getInt("codigoAvaliacao"));
                avaliacao.setData(Data.converteDataMysqlBr(resultSet.getString("data")));
                avaliacao.setHora(resultSet.getString("hora"));
                avaliacao.setTema(resultSet.getString("tema"));
                avaliacao.setDisciplina(resultSet.getString("disciplina"));
                avaliacao.setAulaAvaliativa(aulaAvaliativa);

                listaDeAvaliacaos.add(avaliacao);
            }
            resultSet.close();
            statement.close();
        } catch (Exception erro) {
            JOptionPane.showMessageDialog(null, Strings.getMensagemErroRecuperacao("Avaliações", erro.getMessage()), Strings.tituloMessageErro, JOptionPane.ERROR_MESSAGE);
        }
        return listaDeAvaliacaos;

    }

    public boolean salvar(Avaliacao pAvaliacao) {

        Connection conexao = Conexao.getSqlConnection();
        try {
            Statement statement = conexao.createStatement();
            statement.execute(
                    "INSERT INTO avaliacao " +
                    "(codigoAvaliacao, codigoAulaAvaliativa, data, hora, tema, disciplina) " +
                    "VALUES(" + pAvaliacao.getCodigoAvaliacao() +
                    ", " + pAvaliacao.getAulaAvaliativa().getCodigoAulaAvaliativa() +
                    ", '" + Data.converteDataBrMysql(pAvaliacao.getData()) +
                    "', '" + pAvaliacao.getHora() +
                    "', '" + pAvaliacao.getTema() +
                    "', '" + pAvaliacao.getDisciplina() + "')");

            statement.close();

            return true;
        } catch (Exception erro) {
            JOptionPane.showMessageDialog(null, Strings.getMensagemErroPersitencia("Avaliação", erro.getMessage()), Strings.tituloMessageErro, JOptionPane.ERROR_MESSAGE);

            return false;
        }
    }

    public boolean atualizar(Avaliacao pAvaliacao) {

        Connection conexao = Conexao.getSqlConnection();
        try {
            Statement statement = conexao.createStatement();

            statement.execute("UPDATE avaliacao " +
                    "SET data = '" + Data.converteDataBrMysql(pAvaliacao.getData()) +
                    "', hora = '" + pAvaliacao.getHora() +
                    "', tema = '" + pAvaliacao.getTema() +
                    "', disciplina = '" + pAvaliacao.getDisciplina() +
                    "' WHERE codigoAvaliacao = " + pAvaliacao.getCodigoAvaliacao());
            statement.close();

            return true;
        } catch (Exception erro) {
            JOptionPane.showMessageDialog(null, Strings.getMensagemErroEdicao("Avaliaçã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 avaliacao WHERE codigoAvaliacao = " + codigo);
            statement.close();

            return true;
        } catch (Exception erro) {
            JOptionPane.showMessageDialog(null, Strings.getMensagemErroExclusao("Avaliação", erro.getMessage()), Strings.tituloMessageErro, JOptionPane.ERROR_MESSAGE);

            return false;
        }
    }

    public int getMaiorCodigo() {

        Connection conexao = Conexao.getSqlConnection();
        int intCodigo = 0;
        try {
            Statement statement = conexao.createStatement();
            statement.executeQuery("SELECT MAX(codigoAvaliacao) AS codigo FROM avaliacao");
            ResultSet resultSet = statement.getResultSet();
            while (resultSet.next()) {
                intCodigo = resultSet.getInt("codigo");
            }
            resultSet.close();
            statement.close();

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

Related Classes of dao.AvaliacaoDAO

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.