使用Hibernate如何在Oracle中插入Date类型的数据

不是吧,既然显示为科学计数,肯定是有问题了,不然为什么不能正常显示呢,而且读取时也会出错,5楼提示的我也试过了,问题一样,没有变化。
把代码贴出一部分吧,希望能帮找出问题。
Java code
 表的映射文件
  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
  <hibernate-mapping package="ge.threatscan.dataaccess.domainobject">
    <class name="Command" table="RMD_COMMAND" >
     [color=#FF0000] <id name="id" type="int" column="ID" > [/color]
        <generator class="sequence">
          <param name="sequence" >RMD_COMMAND_SEQ </param>
        </generator> 
      </id> 
      <property name="userID" type="string" column="USER_ID"> </property>
      <property name="status" type="string" column="STATUS"> </property>
      <property name="command" type="string" column="COMMAND"> </property>
      <many-to-one name="modem" 
         class="ge.threatscan.dataaccess.domainobject.Modem"
         column="MODEM_ID"  
         lazy="false"        
     />
      <property name="commandParameters" type="string" column="COMMAND_PARAMS"> </property>
     [color=#FF0000] <property name="createTime" type="timestamp" column="CREATE_TIME"> </property>
      <property name="modifiedTime" type="timestamp" column="MODIFIED_TIME"> </property>[/color] 
 
 表字段类
 package ge.threatscan.dataaccess.domainobject;
 
 import java.io.Serializable;
 import java.util.Date;
 
 /**
  * 
  * Domain object class is is the foundation of the whole program
  * as it is the data structure and is used in many layers. It is 
  * almostly conresponding to the table in database. 
  * 
  * This class is the data structure of Command and contains all of
  * useful data as properties.
  *
  */
 public class Command implements Serializable{
  /**
   * 
   */
  private static final long serialVersionUID = 1L;
  private int id;
  private String status; 
  private String userID; 
  private String command;
  private Modem modem;
  private String commandParameters;
  private Date createTime = null;
  private Date modifiedTime = null;
  /**
   * construct method
   *
   */
  public Command() {
   super();
  }
 
 
  /**
   * @return the status
   */
  public String getStatus() {
   return status;
  }
  /**
   * @param status the status to set
   */
  public void setStatus(String status) {
   this.status = status;
  }
  /**
   * @return the userID
   */
  public String getUserID() {
   return userID;
  }
  /**
   * @param userID the userID to set
   */
  public void setUserID(String userID) {
   this.userID = userID;
  }
  
  public int getId() {
   return id;
  }
  
  public void setId(int id) {
   this.id = id;
  }
 
  /**
   * @return the command
   */
  public String getCommand() {
   return command;
  }
 
  /**
   * @param command the command to set
   */
  public void setCommand(String command) {
   this.command = command;
  }
 
  /**
   * @return the commandParameters
   */
  public String getCommandParameters() {
   return commandParameters;
  }
 
  /**
   * @param commandParameters the commandParameters to set
   */
  public void setCommandParameters(String commandParameters) {
   this.commandParameters = commandParameters;
  }
 
  /**
   * @return the createTime
   */
  public Date getCreateTime() {
   return createTime;
  }
 
  /**
   * @param createTime the createTime to set
   */
  public void setCreateTime(Date createTime) {
   this.createTime = createTime;
  }
 
  /**
   * @return the modifiedTime
   */
  public Date getModifiedTime() {
   return modifiedTime;
  }
 
  /**
   * @param modifiedTime the modifiedTime to set
   */
  public void setModifiedTime(Date modifiedTime) {
   this.modifiedTime = modifiedTime;
  }
 
     /**
      * 
      * @return the modem
      */
  public Modem getModem() {
   return modem;
  }
 
     /**
      * 
      * @param modem
      */
  public void setModem(Modem modem) {
   this.modem = modem;
  }
 
 }
 
 
 插入数据类
 package ge.threatscan.dataaccess.dao;
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.GregorianCalendar;
 
 import org.hibernate.HibernateException;
 import org.hibernate.Query;
 import org.hibernate.Session;
 import org.hibernate.Transaction;
 
 import sun.util.calendar.Gregorian;
 
 import ge.threatscan.dataaccess.HibernateSessionFactory;
 import ge.threatscan.dataaccess.domainobject.Command;
 import ge.threatscan.dataaccess.domainobject.Modem;
 import ge.threatscan.dataaccess.util.ThreatScanDBException;
 
 public class CommandDAO {
  /**
   * This method is used to save a specified Command according to Command into
   * database.
   * 
   * @param command
   * @return boolean
   * @throws ThreatScanDBException
   */
  public boolean saveCommand(Command command) throws ThreatScanDBException {
   Session session = HibernateSessionFactory.getSession();
   Transaction tx = null;
   try {
    tx = session.beginTransaction();
    session.save(command);
    tx.commit();
    return true;
   } catch (HibernateException e) {
    if (tx != null) {
     tx.rollback();
    }
    throw new ThreatScanDBException(e.getMessage());
   } finally {
    session.close();
   }
  }
 
  public static Date convertDate(Date date){
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   try {
    String temp = sdf.format(date);
    date = sdf.parse(temp);
   } catch (ParseException e) { 
    //e.printStackTrace();
    System.out.println("error: " + e);
   }
   return date;
  }
 
  public static void main(String[] args) {
   CommandDAO commandDAO = new CommandDAO();
   try {
    Command command = new Command();
    command.setCommand("9");
    Modem modem = new Modem();
    modem.setId(1);
 
    command.setModem(modem);
    command.setStatus("2");
    command.setUserID("wd");
    Date date = new Date();
 
 //   date = convertDate(date);//打开这个就没问题
 
    command.setCreateTime(date);
    command.setModifiedTime(date);
    System.out.println(commandDAO.saveCommand(command));
    
 //   System.out.println((new Date().toString()));
    
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 }
    
    </class>
 
  </hibernate-mapping>
 
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值