bean管理事务.java

来自「J2EE技术是一种可以信赖的企业级软件开发技术」· Java 代码 · 共 50 行

JAVA
50
字号
public void Transfer(String toId,double amount)throws SQLException{
		PreparedStatement pstmt=null;
		UserTransaction ut=sessionConttext.getUserTansaction();
		try{
			pstmt=conn.preparedStatement("UPDATE account SET balance=balance-? WHERE id=?");
			pstmt.setDouble(1,amount);
			pstmt.setString(2,id);
			if(pstmt.executeUpdate()==1){
				pstmt.close();
			
			}
			else{
				pstmt.close();
				throw new SQLException("Can't update the database!");
			}
		}
		catch (SQLException sqlEx){
			try{
				ut.rollback();
			}
			catch(Exception transEx){
				tranEx.printTrace();
			}
			throw new SQLException("Can't debit from the database!"+sqlEx.toString());
		}
		try{
			pstmt=conn.preparedStatement("UPDATE account SET balance=balance+? WHERE id=?");
			pstmt.setDouble(1,amount);
			pstmt.setString(2,toId);
			if(pstmt.executeUpdate()==1){
				pstmt.close();
				return;
			}
			else{
				pstmt.close();
				throw new SQLException("Can't update the database!(deposit)");
			}
		}
		catch (SQLException sqlEx){
			try{
				ut.rollback();
			}
			catch(Exception transEx){
				tranEx.printTrace();
			}
			throw new SQLException("Can't debit from the database!"+sqlEx.toString());
			
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?