更新新表中的现有数据

时间:2014-10-08 07:35:21

标签: java sql

我有一个名为owner的表,因为我有一个列“vehicle_no”,这是一个主键。我想要的是,如果我想保存数据(已经存在),现有数据必须移动到表“owner_backup”,新数据必须保存在“owner”表中。请帮帮我..提前谢谢:)

public String SaveTransferDetails(vehicle_ownership objBean) {
    String status = "failear";
    Session session = null;
    org.hibernate.Transaction transaxion = null;
    session = HibernateUtil.getSessionFactory().openSession();
    transaxion = session.beginTransaction();
    conn = session.connection();
    try {
        if (objBean != null) {
            vehicle_ownership objTempPermit = (vehicle_ownership) session.get(vehicle_ownership.class, objBean.getVeh_no());
            if (objTempPermit != null) {
                pst = conn.prepareStatement("insert into regd_vehicle_owner_backup select *,now() from regd_vehicle_owner where regn_no=? ");
                pst.setString(1, objBean.getVeh_no());
                rst = pst.executeQuery();
            /*int i= pst.executeUpdate();*/
                session.delete(objTempPermit);
                session.save(objBean);

            } else {
                session.save(objBean);
            }

        }

        transaxion.commit();
        objBean = null;
        status = "success";
    } catch (Exception ex) {
        ex.printStackTrace();
        transaxion.rollback();
        status = "failear";
    } finally {
        if (session.isOpen()) {
            session.clear();
            session.close();
        }
    }
    return status;
}

0 个答案:

没有答案