withdraw.java

来自「Enjoy Web Dev With Tapestry 一书的源代码」· Java 代码 · 共 64 行

JAVA
64
字号
package com.ttdev.bank;

import java.sql.*;

import javax.naming.*;
import javax.sql.*;

import org.apache.tapestry.annotations.*;
import org.apache.tapestry.form.*;
import org.apache.tapestry.html.*;
import org.apache.tapestry.valid.*;

public abstract class Withdraw extends BasePage {
	public abstract String getAccNo();
	public abstract int getAmount();

	@Bean
	public abstract ValidationDelegate getDelegate();

	public void onOk() {
		try {
			Context context = new InitialContext();
			DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/bankDataSource");
			Connection conn = ds.getConnection();
			try {
				conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
				int balance;
				PreparedStatement st = conn.prepareStatement(
					"select * from accounts where accno=?");
				try {
					st.setString(1, getAccNo());
					ResultSet rs = st.executeQuery();
					rs.next();
					balance = rs.getInt("balance");
				} finally {
					st.close();
				}
				if (balance < getAmount()) {
					ValidationDelegate delegate = (ValidationDelegate) getBeans().getBean(
							"delegate");
					delegate.setFormComponent((IFormComponent) getComponent("amount"));
					delegate.record("Insufficient balance", null);
					return;
				}
				st = conn.prepareStatement("update accounts set balance=? where accno=?");
				try {
					st.setInt(1, balance-getAmount());
					st.setString(2, getAccNo());
					st.executeUpdate();
				} finally {
					st.close();
				}
				conn.commit();
			} catch (Exception e) {
				conn.rollback();
				throw e;
			} finally {
				conn.close();
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}

⌨️ 快捷键说明

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