Registering new date in Mysql

Asked

Viewed 32 times

1

When I first register the item in the database it takes the date it was created and saved in the database, the problem happens when I edit the item in the program and then saved in the database it saves a new date.

Item:

String nome;
int valor;
java.sql.Timestamp datacriado;

public Item(String nome, int valor, java.sql.Timestamp datacriado){
    this.nome = nome;
    this.valor = valor;
    this.datacriado = datacriado;
}
get e set;

Creating new item:

 public void CriarItem(String nome, int valor){
 java.sql.Timestamp data = new java.sql.Timestamp(new java.util.Date().getTime());

 Item novoitem = new Item(nome, valor, data);
 registrarItem(novoitem);
 }

register in the database:

    String qy;
public void registrarItem(Item novoitem){
 qy = "INSERT INTO Itens (Nome, Valor, DataCriado) VALUES (?,?,?)";
    try {
        PreparedStatement ps = MySQL.getConn().prepareStatement(qy);
        ps.setString(1, novoitem.getNome());
        ps.setInt(2, novoitem.getValor());
        ps.setTimeStamp(3, novoitem.getDataCriado());
        ps.execute();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

And last when I edit and save (where I think the problem is):

public void salvarItem(Item novoitem){
        qy = "UPDATE Itens SET Nome=?, Valor=? WHERE Nome=?";
        if (novoitem != null) {
            try {
                PreparedStatement ps = MySQL.getConn().prepareStatement(qy);
                ps.setString(1, novoitem.getNome());
                ps.setInt(2, novoitem.getValor());
                ps.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

But if I put it in saveItem to write on the console the date of the item, the correct creation date appears, but after executeUpdate(); it writes a new date in the database (Only happens when I edit)

1 answer

0

I decided, I had to change the Datacreated column to the datetime format instead of timestamp because the timestamp was set to update itself.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.