📄 useraccounterbean.java
字号:
package banksystem;
import javax.ejb.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class UserAccountBean implements SessionBean{
private SessionContext sessionContext;
private String id=null;
private String password=null;
private Connection conn=null;
public void ejbCreate(String id,String password)
throws CreateException,SQLException,NamingException{
InitialContext ctx=new InitialContext();
DateSource ds=(DataSource)ctx.lookup("sqlserver");
conn=ds.getConnection();
PrepareedStatement pstmt=conn.prepareStatement("SELECT password FROM accout 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;
}
esle{
conn.close();
conn=null;
thow new CreateException("password Error!");
}
}
else{
conn.close();
conn=null;
thow 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();
DateSource ds=(DataSource)ctx.lookup("sqlserver");
conn=ds.getConnection();
}
catch(Exception ex){
ex.printStackTrace();
}
}
public void ejbPassivate(){
try{
conn.close();
conn=null;
}
catch(Exception ex){
ex.printTrace();
}
}
public void setSessionContext(SessionContext sessionContext){
this.sessionContext=sessionContext;
}
public double Balance()throws SQLException{
PreparedStatement pstmt=null;
pstmt=conn.preparedStatement("SELECT balance FROM account WHERE id=?");
pstmt.setString(1,id);
ResultSet rs=pstmt.excuteQuery();
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.preparedStatement("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.preparedStatement("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.preparedStatement("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!");
}
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)");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -