📄 useraccountbean.java
字号:
package useraccount;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
import java.sql.Connection;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class UserAccountBean implements SessionBean {
SessionContext sessionContext;
String id=null;
String password=null;
Connection conn=null;
public void ejbCreate(String id, String password) throws CreateException,SQLException,NamingException {
InitialContext ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("test");
conn=ds.getConnection();
PreparedStatement pstmt=conn.prepareStatement("Select password from account where id=?");
pstmt.setString(1,id);
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
String pw=rs.getString("password");
if(password.equals(pw)){
this.id=id;
this.password=password;
return;
}else{
conn.close();
conn=null;
throw new CreateException("Incorrect User Id!");
}
}
}
public void ejbRemove() {
try{
id=null;
password=null;
conn.close();
conn=null;
}
catch(Exception ex){
ex.printStackTrace();
}
}
public void ejbActivate() {
try{
InitialContext ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("test");
conn=ds.getConnection();
}
catch(Exception ex){
ex.printStackTrace();
}
}
public void ejbPassivate() {
try{
conn.close();
conn=null;
}
catch(Exception ex){
ex.printStackTrace();
}
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
public double Balance()throws SQLException {
PreparedStatement pstmt=null;
pstmt=conn.prepareStatement("SELECT balance FROM account WHERE id=?");
pstmt.setString(1,id);
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
double balance=rs.getDouble("balance");
pstmt.close();
return balance;
}
else{
pstmt.close();
throw new SQLException("None user id can be found!");
}
}
public void Withdraw(double amount)throws SQLException {
PreparedStatement pstmt=null;
pstmt=conn.prepareStatement
("UPDATE account SET balance=balance-? WHERE id=?");
pstmt.setDouble(1,amount);
pstmt.setString(2,id);
if(pstmt.executeUpdate()==1){
pstmt.close();
return;
}
else{
pstmt.close();
throw new SQLException("Can't update the database!");
}
}
public void Deposit(double amount)throws SQLException {
PreparedStatement pstmt=null;
pstmt=conn.prepareStatement("UPDATE account SET balance=balance+? WHERE id=?");
pstmt.setDouble(1,amount);
pstmt.setString(2,id);
if(pstmt.executeUpdate()==1){
pstmt.close();
return;
}
else{
pstmt.close();
throw new SQLException("Can't update the database!(deposit)");
}
}
public void Transfer(String toId, double amount)throws SQLException {
PreparedStatement pstmt=null;
pstmt=conn.prepareStatement("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!");
}
pstmt=conn.prepareStatement("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)");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -