When I first register the item in the database it picks up 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);
}
registerItem in 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 finally when I edit and save (where I think it's the problem):
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 in the saveItem to write in 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)