Java中事务处理方法

来源:互联网 发布:智能电视怎么设置网络 编辑:程序博客网 时间:2024/05/16 18:05
public boolean addSeatInfo(TSeatInfo ts,String OrderDetailID)throws Exception
{
   boolean b=false;
   Connection conn=null;
   PreparedStatement ps = null;
   Statement st = null;
   ResultSet rs=null;
   try
   {
    conn = DBMgr.getInstance().getConn();
    conn.setAutoCommit(false);
   
    String sql="insert into t_seat_info(seq_seat_info_id,Creator,CreateTime,SeatDesc,WhichSell,WhoSell,WhenSell,Num)"
     +"values(seq_seat_info.nextval,?,sysdate,?,?,?,sysdate,?)";
    ps = conn.prepareStatement(sql);
    ps.setString(1,ts.getCreator());
    ps.setString(2,ts.getSeatDesc());
    ps.setString(3,ts.getWhichSell());
    ps.setString(4,ts.getWhoSell());
    ps.setFloat(5,ts.getNum());
    ps.executeUpdate();
   
    st = conn.createStatement();
    String key="";
    rs= st.executeQuery("select seq_seat_info.currval from dual");
    if(rs.next())
    {
     key=rs.getString(1);
    }
   
    sql="update t_order_detail set seq_seat_info_id=? where seq_order_detail_id=?";
    ps = conn.prepareStatement(sql);
    ps.setString(1,key);
    ps.setString(2,OrderDetailID);   
    ps.executeUpdate();
   
    conn.commit();
    b=true;
   
   }
   catch(Exception e)
   {   
    conn.rollback();
    e.printStackTrace();
   }
   finally
   {
    if(ps!=null)
    {
     ps.close();
    }
    if(st!=null)
    {
     st.close();
    }
    if(rs!=null)
    {
     rs.close();
    }
    conn.setAutoCommit(true);
          DBMgr.closeCon(conn);
   }
   return b;