📄 withdraw.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -