⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 useraccounttestclient1.java

📁 J2EE技术是一种可以信赖的企业级软件开发技术
💻 JAVA
字号:
package useraccount;

import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.lang.String;
import java.util.Properties;

public class UserAccountTestClient1 {
    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 static final int MAX_OUTPUT_LINE_LENGTH = 100;
    private boolean logging = true;
    private UserAccount userAccount = null;
    private UserAccountHome userAccountHome = null;

    //Construct the EJB test client
    public UserAccountTestClient1() {
        initialize();
    }

    public void initialize() {
        long startTime = 0;
        if (logging) {
            log("Initializing bean access.");
            startTime = System.currentTimeMillis();
        }

        try {

            //get naming context
            Context context = getInitialContext();
            //look up jndi name
            Object ref = context.lookup("UserAccount");
            //look up jndi name and cast to Home interface
            userAccountHome = (UserAccountHome) PortableRemoteObject.narrow(ref,
                    UserAccountHome.class);
            if (logging) {
                log(
                        "Succeeded initializing bean access through Home interface.");
                long endTime = System.currentTimeMillis();
                log("Execution time: " + (endTime - startTime) + " ms.");
            }

        } catch (Exception e) {
            if (logging) {
                log("Failed initializing bean access.");
            }
            e.printStackTrace();
        }

    }

    public Context getInitialContext() throws Exception {
        String url = "t3://server2003: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) {
            log("Unable to connect to WebLogic server at " + url);
            log("Please make sure that the server is running.");
            throw e;
        }
    }

    //----------------------------------------------------------------------------
    // Methods that use Home interface methods to generate a Remote interface reference
    //----------------------------------------------------------------------------

    public UserAccount create(String id, String password) {
        long startTime = 0;
        if (logging) {
            log("Calling create(" + id + ", " + password + ")");
            startTime = System.currentTimeMillis();
        }

        try {
            userAccount = userAccountHome.create(id, password);
            if (logging) {
                log("Succeeded: create(" + id + ", " + password + ")");
                long endTime = System.currentTimeMillis();
                log("Execution time: " + (endTime - startTime) + " ms.");
            }
        } catch (Exception e) {
            if (logging) {
                log("Failed : create(" + id + ", " + password + ")");
            }
            e.printStackTrace();
        }
        if (logging) {
            log("Return value from create(" + id + ", " + password + "): " +
                userAccount + ".");
        }
        return userAccount;
    }

    //----------------------------------------------------------------------------
    // Methods that use Remote interface methods to access data through the bean
    //----------------------------------------------------------------------------

    public double Balance() {
        double returnValue = 0f;
        if (userAccount == null) {
            System.out.println("Error in Balance(): " + ERROR_NULL_REMOTE);
            return returnValue;
        }
        long startTime = 0;
        if (logging) {
            log("Calling Balance()");
            startTime = System.currentTimeMillis();
        }

        try {
            returnValue = userAccount.Balance();
            if (logging) {
                log("Succeeded: Balance()");
                long endTime = System.currentTimeMillis();
                log("Execution time: " + (endTime - startTime) + " ms.");
            }
        } catch (Exception e) {
            if (logging) {
                log("Failed : Balance()");
            }
            e.printStackTrace();
        }
        if (logging) {
            log("Return value from Balance(): " + returnValue + ".");
        }
        return returnValue;
    }

    public void Withdraw(double amount) {
        if (userAccount == null) {
            System.out.println("Error in Withdraw(): " + ERROR_NULL_REMOTE);
            return;
        }
        long startTime = 0;
        if (logging) {
            log("Calling Withdraw(" + amount + ")");
            startTime = System.currentTimeMillis();
        }

        try {
            userAccount.Withdraw(amount);
            if (logging) {
                log("Succeeded: Withdraw(" + amount + ")");
                long endTime = System.currentTimeMillis();
                log("Execution time: " + (endTime - startTime) + " ms.");
            }
        } catch (Exception e) {
            if (logging) {
                log("Failed : Withdraw(" + amount + ")");
            }
            e.printStackTrace();
        }
    }

    public void Deposit(double amount) {
        if (userAccount == null) {
            System.out.println("Error in Deposit(): " + ERROR_NULL_REMOTE);
            return;
        }
        long startTime = 0;
        if (logging) {
            log("Calling Deposit(" + amount + ")");
            startTime = System.currentTimeMillis();
        }

        try {
            userAccount.Deposit(amount);
            if (logging) {
                log("Succeeded: Deposit(" + amount + ")");
                long endTime = System.currentTimeMillis();
                log("Execution time: " + (endTime - startTime) + " ms.");
            }
        } catch (Exception e) {
            if (logging) {
                log("Failed : Deposit(" + amount + ")");
            }
            e.printStackTrace();
        }
    }

    public void Transfer(String toId, double amount) {
        if (userAccount == null) {
            System.out.println("Error in Transfer(): " + ERROR_NULL_REMOTE);
            return;
        }
        long startTime = 0;
        if (logging) {
            log("Calling Transfer(" + toId + ", " + amount + ")");
            startTime = System.currentTimeMillis();
        }

        try {
            userAccount.Transfer(toId, amount);
            if (logging) {
                log("Succeeded: Transfer(" + toId + ", " + amount + ")");
                long endTime = System.currentTimeMillis();
                log("Execution time: " + (endTime - startTime) + " ms.");
            }
        } catch (Exception e) {
            if (logging) {
                log("Failed : Transfer(" + toId + ", " + amount + ")");
            }
            e.printStackTrace();
        }
    }

    //----------------------------------------------------------------------------
    // Utility Methods
    //----------------------------------------------------------------------------

    private void log(String message) {

        if (message == null) {
            System.out.println("-- null");
        }
        if (message.length() > MAX_OUTPUT_LINE_LENGTH) {
            System.out.println("-- " +
                               message.substring(0, MAX_OUTPUT_LINE_LENGTH) +
                               " ...");
        } else {
            System.out.println("-- " + message);
        }
    }

    //Main method
    public static void main(String[] args) {
        UserAccountTestClient1 client = new UserAccountTestClient1();

        // Use the client object to call one of the Home interface wrappers
        // above, to create a Remote interface reference to the bean.
        // If the return value is of the Remote interface type, you can use it
        // to access the remote interface methods.  You can also just use the
        // client object to call the Remote interface wrappers.
        UserAccountHome hm=client.userAccountHome;
        try{
            UserAccount user=hm.create("112233445566","123456");
            System.out.println("Balance:"+user.Balance());
            user.Deposit(10000);
            System.out.println("The user has deposit some money. Now the balance is:"
                    +user.Balance());
            user.Withdraw(200);
            System.out.println("The user has withdraw some money. Now the balance is:"
                    +user.Balance());
            user.Transfer("123456123456",4000);
            System.out.println("The user has transfer some money. Now the balance is:"
                    +user.Balance());
            user.remove();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -