您的位置:首页 > 编程语言 > Java开发

Java中事务处理方法

2008-02-01 21:05 274 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: