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

package br.com.ddns.grupotsergio;

import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import java.sql.ResultSet;

/**
 *
 * @author Edson
 */
public class apolicesGS {
    public int idapolices = 0;
    public String apolice = null;
    public String codigo = null;
    public String auditoria = null;
    public String obs = null;

    private static PreparedStatement stm = null;
    private static ResultSet rs = null;
    private static Statement stmt;

    public String getApolice() {
        return apolice;
    }

    public void setApolice(String apolice) {
        this.apolice = apolice;
    }

    public String getAuditoria() {
        return auditoria;
    }

    public void setAuditoria(String auditoria) {
        this.auditoria = auditoria;
    }

    public String getCodigo() {
        return codigo;
    }

    public void setCodigo(String codigo) {
        this.codigo = codigo;
    }

    public int getIdapolices() {
        return idapolices;
    }

    public void setIdapolices(int idapolices) {
        this.idapolices = idapolices;
    }

    public String getObs() {
        return obs;
    }

    public void setObs(String obs) {
        this.obs = obs;
    }


    public apolicesGS(){

    }

    public static boolean inserirApolice (apolicesGS grupo){
        try{
            stm = (PreparedStatement) Conexao.conectar().prepareStatement("INSERT INTO aplocies("
                    + "idaplocies,"
                    + "apolice,"
                    + "codigo,"
                    + "auditoria,"
                    + "obs) VALUES "
                    + "(?,?,?,?,?)");
            stm.setInt(1, grupo.getIdapolices());
            stm.setString(2, grupo.getApolice());
            stm.setString(3, grupo.getCodigo());
            stm.setString(4, grupo.getAuditoria());
            stm.setString(5, grupo.getObs());
            stm.executeUpdate();
            stm.close();
            return true;
        }catch(Exception e){
            System.out.println("Erro ao inserir");
            return false;
        }
    }

    public static boolean altApolApolice (apolicesGS grupo){
        boolean testa = false;
        try{
            stm = (PreparedStatement) Conexao.conectar().prepareStatement("update aplocies set "
                    + "apolice=? "
                    + "where idaplocies=?");
            stm.setString(1, grupo.getApolice());
            stm.setInt(2, grupo.getIdapolices());

            int executeUpdate = stm.executeUpdate();

            if(executeUpdate > 0)
                testa = true;
            else
                testa = false;
        }catch(Exception e){
            System.out.println("Erro ao alterar");
        }
        return testa;
    }

    public static boolean altApolCod (apolicesGS grupo){
        boolean testa = false;
        try{
            stm = (PreparedStatement) Conexao.conectar().prepareStatement("update aplocies set "
                    + "codigo=? "
                    + "where idaplocies=?");
            stm.setString(1, grupo.getCodigo());
            stm.setInt(2, grupo.getIdapolices());

            int executeUpdate = stm.executeUpdate();

            if(executeUpdate > 0)
                testa = true;
            else
                testa = false;
        }catch(Exception e){
            System.out.println("Erro ao alterar");
        }
        return testa;
    }

    public static boolean altApolAud (apolicesGS grupo){
        boolean testa = false;
        try{
            stm = (PreparedStatement) Conexao.conectar().prepareStatement("update aplocies set "
                    + "auditoria=? "
                    + "where idaplocies=?");
            stm.setString(1, grupo.getAuditoria());
            stm.setInt(2, grupo.getIdapolices());

            int executeUpdate = stm.executeUpdate();

            if(executeUpdate > 0)
                testa = true;
            else
                testa = false;
        }catch(Exception e){
            System.out.println("Erro ao alterar");
        }
        return testa;
    }

    public static boolean altApolObs (apolicesGS grupo){
        boolean testa = false;
        try{
            stm = (PreparedStatement) Conexao.conectar().prepareStatement("update aplocies set "
                    + "obs=? "
                    + "where idaplocies=?");
            stm.setString(1, grupo.getObs());
            stm.setInt(2, grupo.getIdapolices());

            int executeUpdate = stm.executeUpdate();

            if(executeUpdate > 0)
                testa = true;
            else
                testa = false;
        }catch(Exception e){
            System.out.println("Erro ao alterar");
        }
        return testa;
    }

    public static boolean excluiApolice (apolicesGS grupo){
        boolean testa = false;
        try {
            stm = (PreparedStatement) Conexao.conectar().prepareStatement("delete from aplocies "
                    + "where idaplocies = ?");
            stm.setInt(1, grupo.getIdapolices());
            int executeUpdate = stm.executeUpdate();

            if(executeUpdate > 0)
                testa = true;
            else
                testa = false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return testa;
    }

    public void selApolice(String Id) {
        try {
            apolicesGS scid = null;
            stmt = (Statement) Conexao.conectar().createStatement();
            rs = stmt.executeQuery("Select * From aplocies Where idaplocies ='"+Id+"'");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static int getAutoInc(String Generator) {
        int Codigo = 0;
        try {
            PreparedStatement pstmt = (PreparedStatement) Conexao.conectar().prepareStatement(
                    "Select max(idaplocies) From aplocies");
            ResultSet rss = pstmt.executeQuery();
            rss.next();
            Codigo = rss.getInt(1) + 1;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return Codigo;
    }

    public void listaApolice(){
        try{
            stmt = (Statement) Conexao.conectar().createStatement();
            rs = stmt.executeQuery("SELECT * FROM aplocies order by apolice");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public ResultSet getResultado(){
        return rs;
    }

}
