⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bean管理事务.java

📁 J2EE技术是一种可以信赖的企业级软件开发技术
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -