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 Aloca class:
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 class Alocadao:
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 Allocation 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 fields for dates are: data_alocacao and data_alocacao.
And above is my canvas. QUESTION: how do I save these two dates in my database, I would be very grateful and you help me Please.
What’s the bug? Tried to use ps.setDate(1, date)?
– dhb