容器管理事务.java

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

JAVA
39
字号
public voi Transfer(String toId,double amount)throws SQLException{
		PreparedStatement pstmt=null;
		try{
			UserTransaction ut=sessionConttext.getUserTansaction();
			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){
			sessionContext.setRollbackOnly();
			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){
			sessionContext.setRollbackOnly();
			throw new SQLException("Can't credit from the database!"+sqlEx.toString());
			
	}
}

⌨️ 快捷键说明

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