useraccounterbean.java
来自「开发无状态.有状态的会话Bean,可以结合有电子工业出版社的J2E教材学习」· Java 代码 · 共 156 行
JAVA
156 行
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 + =
减小字号Ctrl + -
显示快捷键?