Skip site navigation (1) Skip section navigation (2)

Inserting the wrong date

From: Lucas Sanabio <lsanabio(at)gmail(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Inserting the wrong date
Date: 2006-01-25 18:45:59
Message-ID: 91d5bf2c0601251045p5f5d5b6fp405c30b513cbdc6a@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-jdbc
Hi everyone! I'm having an problem when I try to insert data into a
timestamp field, let me try to explain what is going in on.

I developed my application at my local machine and at this machine
everything is working as it should, but when I deployed the application into
the production server, every time I save an timestamp it saves it one hour
before. The method that is inserting the data is pasted below:

############################
public int inserir(CompromissoVO vo, Connection conn) throws Exception {
        PreparedStatement stp = null;

        int chave = 0;

        final String sql = "INSERT INTO "
                + ESQUEMA
                + ".compromisso (com_id, com_criacao, com_data, "
                + "com_titulo, com_confidencial, tco_id, com_agendamento,
aud_id, "
                + "via_id, soe_id)"
                + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        try {

            chave = ConnectionManager.getSequence("comp_seq");
            int i = 1;

            stp = conn.prepareStatement(sql);

            stp.setInt(i++, chave);
            Date agora = new Date();
            stp.setTimestamp(i++, new Timestamp(agora.getTime()));

            SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyyy
HH:mm:ss");
            log.debug("Inserir - Data compromisso: " + sd.format(vo.getData
()));
            stp.setTimestamp(i++, new Timestamp(vo.getData().getTime()));
            stp.setString(i++, vo.getTitulo());
            stp.setString(i++, vo.getConfidencial());
            stp.setInt(i++, vo.getTipo().getCodigo());
            stp.setString(i++, vo.getTipoAgendamento());
            if (vo.getAudiencia() > 0){
                stp.setInt(i++, vo.getAudiencia());
            } else {
                stp.setNull(i++, Types.INTEGER);
            }
            if (vo.getViagem() > 0){
                stp.setInt(i++, vo.getViagem());
            } else {
                stp.setNull(i++, Types.INTEGER);
            }
            if (vo.getSolenidade() > 0){
                stp.setInt(i++, vo.getSolenidade());
            } else {
                stp.setNull(i++, Types.INTEGER);
            }

            stp.execute();

        } catch (NegocioException ne) {
            throw ne;
        } catch (Exception e) {
            log.debug("sql: " + sql);
            throw new NegocioException("error.inserir.compromisso", e);
        }

        return chave;
    }
############################

The vo.getData() return the date which was typed by the user, I logged the
date to see if it was right and I got it was, but when I look at the
database the date is one hour before.

Log: 2006-01-25 16:05:08,877 -> DEBUG (CompromissoDAO.java:140) - Alterar -
Data compromisso: 12/01/2006 12:00:00
The data which was saved at the database: 2006-01-12 11:00:00

My table script is:

CREATE TABLE compromisso
(
  com_id numeric(6) NOT NULL,
  com_criacao timestamp NOT NULL,
  com_data timestamp NOT NULL,
  com_titulo varchar(200) NOT NULL,
  com_confidencial varchar(400),
  com_agendamento char(1),
  tco_id numeric(6) NOT NULL,
  aud_id numeric(6),
  via_id numeric(6),
  soe_id numeric(6),
  CONSTRAINT compromisso_pk PRIMARY KEY (com_id),
  CONSTRAINT com_tipocompromisso_fk FOREIGN KEY (tco_id) REFERENCES
compromisso_tipo (tco_id) ON UPDATE RESTRICT ON DELETE RESTRICT,
  CONSTRAINT compromisso_audiencia_fk FOREIGN KEY (aud_id) REFERENCES
registro_audiencia (aud_id) ON UPDATE RESTRICT ON DELETE RESTRICT,
  CONSTRAINT compromisso_solenidade_fk FOREIGN KEY (soe_id) REFERENCES
registro_solenidade (soe_id) ON UPDATE RESTRICT ON DELETE RESTRICT,
  CONSTRAINT compromisso_viagem_fk FOREIGN KEY (via_id) REFERENCES
registro_viagem (via_id) ON UPDATE RESTRICT ON DELETE RESTRICT
)
WITHOUT OIDS;
ALTER TABLE compromisso OWNER TO uzzi;
GRANT ALL ON TABLE compromisso TO uzzi;


Does any one have an idea what is going in on?

Thanks,

--
Lucas Sanabio
-----------------------------------------------------------------------------
Sun Certified Programmer for the Java 2 Platform
Consultor Java - Marx Tecnologia
Email: lucas(at)marx(dot)com(dot)br
Cel: (61) 8402-8876

Responses

pgsql-jdbc by date

Next:From: David HustaceDate: 2006-01-25 19:23:01
Subject: Re: Socket read fails
Previous:From: Colin TaylorDate: 2006-01-25 06:03:15
Subject: Re: Socket read fails

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group