gpt4 book ai didi

java - 使用 Hibernate 在 oracle timestamp(6) 列中插入 java.sql.Timestamp 数据

转载 作者:太空宇宙 更新时间:2023-11-04 15:03:34 27 4
gpt4 key购买 nike

我想使用 Struts2 表单在 Oracle 数据库中名为 vol 的表中插入一行,但插入操作返回 "input" 并且不执行插入这是 Vol.java 代码:

package models;

import java.sql.Timestamp;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name="vol")
public class Vol {
@Id
private int idVol;
private String typeAvion;
private String nomAvion;
private String depart;
private String destination;
@Temporal(TemporalType.TIMESTAMP)
private Date heureDepart;
private String heureArrivee;
private String technicien;

public int getIdVol() {
return idVol;
}

public void setIdVol(int idVol) {
this.idVol = idVol;
}

public String getTypeAvion() {
return typeAvion;
}

public void setTypeAvion(String typeAvion) {
this.typeAvion = typeAvion;
}

public String getNomAvion() {
return nomAvion;
}

public void setNomAvion(String nomAvion) {
this.nomAvion = nomAvion;
}

public String getDepart() {
return depart;
}

public void setDepart(String depart) {
this.depart = depart;
}

public String getDestination() {
return destination;
}

public void setDestination(String destination) {
this.destination = destination;
}

public Date getHeureDepart() {
return heureDepart;
}

public void setHeureDepart(Date heureDepart) {
this.heureDepart = heureDepart;
}

public String getheureArrivee() {
return heureArrivee;
}

public void setheureArrivee(String heureArrivee) {
this.heureArrivee = heureArrivee;
}

public String getTechnicien() {
return technicien;
}

public void setTechnicien(String technicien) {
this.technicien = technicien;
}



public Vol(){

}

}

这是以下形式的代码:

<s:form action="ajoutVol" method="post">
<s:textfield name="vol.idVol" label="idVol" size="20" />
<s:textfield name="vol.typeAvion" label="Type Avion" size="20" />
<s:textfield name="vol.nomAvion" label="Nom Avion" size="20" />
<s:textfield name="vol.depart" label="Depart" size="20" />
<s:textfield name="vol.destination" label="Destination" size="20" />
<s:textfield name="heureDepart" label="Heure de dapart" size="20" />
<s:textfield name="vol.heureArrivee" label="Heure d'arrivée" size="20" />
<s:textfield name="vol.technicien" label="technicien" size="20" />
<s:submit name="submit" label="Submit" />
</s:form>

这是操作的执行方法:

public String execute(){
VolDAO vd= new VolDAO();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
vol.setHeureDepart(dateFormat.parse(heureDepart));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
vd.insertVol(vol);
Map session = ActionContext.getContext().getSession();
session.put("vol", vol);
return SUCCESS;
}

最后是方法insertVol()的代码:

public void insertVol(Vol vol){
SessionFactory sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(vol);
session.getTransaction().commit();
session.close();
sessionFactory.close();
}

最佳答案

使用以下代码将Timestamp数据插入到timestamp Oracle类型

vol.setHeureDepart(new Timestamp(dateFormat.parse(heureDepart).getTime()));

实体应该像这样映射此列

@Column(name = "heure_depart", length = 19)
private Date heureDepart;

关于java - 使用 Hibernate 在 oracle timestamp(6) 列中插入 java.sql.Timestamp 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22298941/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com