📄 openaccounttestclient1.java
字号:
package bankonlinesystem.openaccount.ejb.session;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import bankonlinesystem.openaccount.dto.AccountDto;
import java.lang.String;
import java.math.BigDecimal;
import java.util.Properties;
import java.util.ArrayList;
import java.math.BigDecimal;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.Context;
public class OpenAccountTestClient1 {
private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
private OpenAccount openAccount = null;
private OpenAccountHome openAccountHome = null;
//Construct the EJB test client
public OpenAccountTestClient1() {
initialize();
}
public void initialize() {
try {
//get naming context
Context context = getInitialContext();
//look up jndi name
Object ref = context.lookup("OpenAccount");
//look up jndi name and cast to Home interface
openAccountHome = (OpenAccountHome) PortableRemoteObject.narrow(ref,
OpenAccountHome.class);
} catch (Exception e) {
e.printStackTrace();
}
}
public Context getInitialContext() throws Exception {
String url = "t3://localhost:7001";
String user = null;
String password = null;
Properties properties;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS,
password == null ? "" : password);
}
return new javax.naming.InitialContext(properties);
} catch (Exception e) {
System.out.println("Unable to connect to WebLogic server at " + url);
System.out.println("Please make sure that the server is running.");
throw e;
}
}
//----------------------------------------------------------------------------
// Methods that use Home interface methods to generate a Remote interface reference
//----------------------------------------------------------------------------
public OpenAccount create() {
try {
openAccount = openAccountHome.create();
} catch (Exception e) {
e.printStackTrace();
}
return openAccount;
}
//----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//----------------------------------------------------------------------------
public void register(AccountDto account) {
if (openAccount == null) {
System.out.println("Error in register(): " + ERROR_NULL_REMOTE);
return;
}
try {
openAccount.register(account);
} catch (Exception e) {
e.printStackTrace();
}
}
public int login(String user, String password) {
int returnValue = 0;
if (openAccount == null) {
System.out.println("Error in login(): " + ERROR_NULL_REMOTE);
return returnValue;
}
try {
returnValue = openAccount.login(user, password);
} catch (Exception e) {
e.printStackTrace();
}
return returnValue;
}
public AccountDto getAccount(String user) {
AccountDto returnValue = null;
if (openAccount == null) {
System.out.println("Error in getAccount(): " + ERROR_NULL_REMOTE);
return returnValue;
}
try {
returnValue = openAccount.getAccount(user);
} catch (Exception e) {
e.printStackTrace();
}
return returnValue;
}
public void modifyMondy(String id, String operator, BigDecimal money) {
if (openAccount == null) {
System.out.println("Error in modifyMondy(): " + ERROR_NULL_REMOTE);
return;
}
try {
openAccount.modifyMondy(id, operator, money);
} catch (Exception e) {
e.printStackTrace();
}
}
public void changPassword(String id, String password) {
if (openAccount == null) {
System.out.println("Error in changPassword(): " + ERROR_NULL_REMOTE);
return;
}
try {
openAccount.changPassword(id, password);
} catch (Exception e) {
e.printStackTrace();
}
}
public ArrayList getAllRecordByID(String id) {
ArrayList returnValue = null;
if (openAccount == null) {
System.out.println("Error in getAllRecordByID(): " +
ERROR_NULL_REMOTE);
return returnValue;
}
try {
returnValue = openAccount.getAllRecordByID(id);
} catch (Exception e) {
e.printStackTrace();
}
return returnValue;
}
//----------------------------------------------------------------------------
// Utility Methods
//----------------------------------------------------------------------------
public OpenAccountHome getHome() {
return openAccountHome;
}
//Main method
public static void main(String[] args) {
OpenAccountTestClient1 client = new OpenAccountTestClient1();
try {
OpenAccountHome home = client.getHome();
OpenAccount account = home.create();
//登录测试(成功)
int flag = account.login("436100020003000123","22");
System.out.println("登录成功:"+flag);
//取出会话里,javaBean的值
AccountDto accountDto = account.getAccount("436100020003000123");
System.out.println("开户ID:"+accountDto.getID()+" 余额:"+accountDto.getOpenDate());
//存款测试
account.modifyMondy("436100020003000123","存款",new BigDecimal(10));
System.out.println("存款成功");
//取款测试
account.modifyMondy("436100020003000123","取款",new BigDecimal(10));
System.out.println("取款成功");
//修改密码测试
account.changPassword("436100020003000123","22");
System.out.println("修改密码成功");
//转帐测试
account.modifyMondy("436100020003000123","取款",new BigDecimal(2));
account.modifyMondy("436100020003000124","存款",new BigDecimal(2));
System.out.println("转帐成功");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -