How to Save the Date Field of a Java SE Application in PostgreSql

0

I need to save these two dates (ALLOCATION DATE and RETURN DATE) from an allocation, where I have the following Allocation Table:

CREATE TABLE public.aloca
(
  alocod serial NOT NULL,
  alodtdevolucao date NOT NULL,
  alodtalocacao date NOT NULL,
  alo_eqcod integer NOT NULL,
  alo_setcod integer NOT NULL,
  alo_funcod integer NOT NULL,
  CONSTRAINT pk_aloca PRIMARY KEY (alocod),
  CONSTRAINT fk_equipamento FOREIGN KEY (alo_eqcod)
      REFERENCES public.equipamento (eqcod) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk_funcionario FOREIGN KEY (alo_funcod)
      REFERENCES public.funcionario (funcod) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk_setor FOREIGN KEY (alo_setcod)
      REFERENCES public.setor (setcod) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

The Bean Class Allocates:

import java.sql.Date;

public class Aloca {
    private Integer alocod;
    private Date alodtdevolucao;
    private Date dtAlocacao;
    private Equipamento equipamento;
    private Setor setor;
    private Funcionario funcionario;

    public Integer getAlocod() {
        return alocod;
    }

    public void setAlocod(Integer alocod) {
        this.alocod = alocod;
    }

    public Date getAlodtdevolucao() {
        return alodtdevolucao;
    }

    public void setAlodtdevolucao(Date alodtdevolucao) {
        this.alodtdevolucao = alodtdevolucao;
    }

    public Date getDtAlocacao() {
        return dtAlocacao;
    }

    public void setDtAlocacao(Date dtAlocacao) {
        this.dtAlocacao = dtAlocacao;
    }

    public Equipamento getEquipamento() {
        return equipamento;
    }

    public void setEquipamento(Equipamento equipamento) {
        this.equipamento = equipamento;
    }

    public Setor getSetor() {
        return setor;
    }

    public void setSetor(Setor setor) {
        this.setor = setor;
    }

    public Funcionario getFuncionario() {
        return funcionario;
    }

    public void setFuncionario(Funcionario funcionario) {
        this.funcionario = funcionario;
    }
}

The AlocaDao class:

 public class AlocaDao {
    Connection conn = null;
    PreparedStatement ps   = null;
    ResultSet rs   = null;
    String sql  = "";

    public boolean salvar(Aloca aloca){
        conn = Conexao.getConexao();
        boolean teste = false;
        sql = "INSERT INTO aloca(alodtdevolucao, alodtalocacao, alo_eqcod, alo_setcod,alo_funcod)\n" +
                "    VALUES (?, ?, ?, ?, ?);";

        try {
            ps = conn.prepareStatement(sql);                    

                        ps.setString(1, aloca.getAlodtdevolucao().toString());
                        ps.setString(2,aloca.getDtAlocacao().toString());
                        ps.setString(1,aloca.getAlodtdevolucao().toString());
                        ps.setInt(2,aloca.getEquipamento().getEqcod());
                        ps.setInt(3,aloca.getSetor().getSetCod());
                        ps.setInt(4, aloca.getFuncionario().getFuncod());
            ps.execute();
            teste = true;
        } catch (Exception e) {
            System.out.println("Erro no salvar -> "+e.getMessage());
                        JOptionPane.showMessageDialog(null, "Erro ao Salvar !!!");

    }
        return teste;
    }
}

The Aloca Control class:

public class ControleAloca {
    AlocaDao aloDao = new AlocaDao();
    Aloca aloca = new Aloca();
    public boolean salvar(Aloca aloca){
        boolean salvou = aloDao.salvar(aloca);
        return salvou;
    }      

}

And finally the View class method that saves:

private void bt_realizar_alocActionPerformed(java.awt.event.ActionEvent evt) {                                                 
    aloca.setDtAlocacao((Date) data_alocacao.getDate());       
    aloca.setAlodtdevolucao((Date) data_alocacao.getDate());        
    aloca.setEquipamento((Equipamento) comboBox_Equipamento.getSelectedItem());     
    aloca.setSetor((Setor) comboBox_Setor.getSelectedItem());
    aloca.setFuncionario((Funcionario) comboBox_Func.getSelectedItem());       

    boolean salvou = controllerAloca.salvar(aloca);
    if(salvou){
        JOptionPane.showMessageDialog(null, "Registro Salvo com Sucesso !!!");            
    }

}              

In my View the fields for dates are: date_alocation and date_alocation.

And above is my screen.    QUESTION: how do I save these two dates in my database, I would be very grateful and you guys would help me please.

    
asked by anonymous 06.09.2017 / 22:52

1 answer

0

The date should be converted, following example below:

ps.setString(1, new java.sql.Date(aloca.getAlodtdevolucao()));
ps.setString(2, new java.sql.Date(aloca.getDtAlocacao()));
...
    
07.09.2017 / 00:40