package com.fatecpg.data;

import br.com.fatecpg.helpers.ConnectionFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class Alternativa {
    private Integer _id;
    private String _texto;
    private boolean _correta;
    private int _questaoId;
    
    // ------ CONSTRUTORES --------------
    public Alternativa(String texto, boolean correta, int questionId) {
        this._texto = texto;
        this._correta = correta;
        this._questaoId = questionId;
    }

    public Alternativa(Integer id, String texto, boolean correta, int questionId) {
        this._id = id;
        this._texto = texto;
        this._correta = correta;
        this._questaoId = questionId;
    }

    // ###### GETTERS E SETTERS #######
    public int getId() {
        return _id;
    }

    public String getTexto() {
        return _texto;
    }

    public void setTexto(String texto) {
        this._texto = texto;
    }

    public boolean isCorreta() {
        return _correta;
    }

    public void setCorreta(boolean correta) {
        this._correta = correta;
    }

    public int getQuestaoId() {
        return _questaoId;
    }

    public void setQuestaoId(int _questaoId) {
        this._questaoId = _questaoId;
    }

    //####### OPERAÇÕES NO BANCO #######
    // ------- CREATE ---------------------
    // Insere o registro no banco de dados de acordo com os atributos do objeto
    public boolean store() throws SQLException {
        try (Connection connection = ConnectionFactory.getConnection()) {
            try {
                Statement statement = connection.createStatement();
                String SQL = String.format("INSERT INTO ALTERNATIVA(TEXTO, CORRECT, QUESTAO_ID) VALUES('%s', %b, %d)", this._texto, this._correta, this._questaoId);

                statement.execute(SQL, Statement.RETURN_GENERATED_KEYS);

                ResultSet rs = statement.getGeneratedKeys();

                if (rs.next()) {
                    this._id = rs.getInt(1);
                }

                rs.close();

                if (this._id != null) {
                    return true;
                }
            } catch (SQLException ex) {
                throw ex;
            }
            connection.close();
        }
        return false;
    }
}
@@ -2492,9 +2492,1209 @@
 %0A    %7D%0D%0A
+    %0D%0A    // ------- READ -----------------------%0D%0A    // Busca uma alternativa pelo ID%0D%0A    public static Alternativa find(Integer id) throws SQLException %7B%0D%0A        try (Connection connection = ConnectionFactory.getConnection()) %7B%0D%0A            PreparedStatement pstatement = connection.prepareStatement(String.format(%22SELECT * FROM ALTERNATIVA WHERE ID = ?%22));%0D%0A            pstatement.setInt(1, id);%0D%0A            %0D%0A            try (ResultSet result = pstatement.executeQuery()) %7B%0D%0A                if (result.next()) %7B%0D%0A                    return new Alternativa(%0D%0A                            result.getInt(%22ID%22),%0D%0A                            result.getString(%22TEXTO%22),%0D%0A                            result.getBoolean(%22CORRECT%22),%0D%0A                            result.getInt(%22QUESTAO_ID%22)%0D%0A                    );%0D%0A                %7D%0D%0A            %7D catch (Exception ex) %7B%0D%0A                System.out.println(%22Erro ao consultar a Alternativa: %22  + ex.getMessage());%0D%0A            %7D%0D%0A            %0D%0A            connection.close();%0D%0A        %7D catch (Exception ex) %7B%0D%0A            System.out.println(%22Erro ao obter conex%C3%A3o com o banco de dados: %22  + ex.getMessage());%0D%0A        %7D%0D%0A%0D%0A        return null;%0D%0A    %7D%0D%0A
 %7D

Implementa método para buscar Alternativa pelo ID