中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > 开发工具
wsad使用过程
作者:未知 时间:2005-07-24 21:27 出处:JR 责编:chinaitpower
              摘要:wsad使用过程
     Developing EJB by WSAD
Prerequisite:
        1. WSAD 402
        2. DB2/UDB 7.2.1 or greater version
        3. Has DB2 sample database created

Part One:     Creat a new EJB Project

1.    Perspective -> Open -> Other -> J2EE Perspective. 
         

2.    File -> New -> EJB Project
    Project Name:        AccountAndTransferEJBModule
    EAR Project:        AccountAndTransferEAR
Next -> Next -> Finish
 
Verify: 
J2EE Perspective -> J2EE View
        EJB Modules ->             AccountAndTransferEJBModule
        Enterprise Applications ->     AccountAndTransferEAR
     

Part Two:     Creating a CMP entity bean
1.     Create a CMP 
    File -> New -> Enterprise Bean
        Bean Name:                Account
        EJB Project:            AccountAndTransferEJBModule
        Source folder:            ejbModule
        Default package:        com.ibm.wsad.spc
 
    -> Next
        Bean supertype:            <none>
        Bean class:                com.ibm.wsad.spc.AccountBean
        Home interface:            com.ibm.wsad.spc.AccountHome
        Remote interface:        com.ibm.wsad.spc.Account
        Key class:                com.ibm.wsad.spc.AccountKey
        CMP attributes:        
        1). accountId:            long        (key field)
                   
        2). type:            int        
         
         3). balance:        float
         
    -> Next
        import             javax.ejb.*
     
    -> Finish
    Verify:    
    J2EE Perspective-> J2EE View
    EJB Modules ->  AccountAndTransferEJBModule -> Account
     
2.     Create a custom exception
    File -> New -> Other -> Java -> Java Class.
 
 
            Folder:                /AccountAndTransferEJBModule/ejbModule
            Package:            com.ibm.wsad.spc
            Name:                InsufficientFundsException
            Superclass:            java.lang.Exception
            Check "Constructor from superclass"
 
    -> Finish
 

3.       Add two methods to AccountBean.java
 

    /* adds the specified amount to the account balance and returns
    * the new balance
    */
    public float add(float amount) {
        balance += amount;
        return balance;
    }
    /* subtracts the specified amount from the account balance and
    * returns the new balance
    */
    public float subtract(float amount) throws InsufficientFundsException {
        if (balance < amount) {
            throw new InsufficientFundsException("Insufficient funds");
        }
        balance -= amount;
        return balance;
    }

4.     Add these two methods to Remote Interface
    Double Click J2EE Perspective -> J2EE View -> EJB Modules ->  AccountAndTransferEJBModule -> Account -> AccountBean
     
    Outline view
     
            Right mouse click add(float)         ->         Promote to Remote Interface
            Right mouse click substract(float)    ->         Promote to Remote Interface

     
    Verify:
    J2EE Perspective -> J2EE View -> EJB Modules ->  AccountAndTransferEJBModule -> Account -> Account 
    
    /* adds the specified amount to the account balance and returns
    * the new balance
    */
    public float add(float amount) throws java.rmi.RemoteException;
    /* subtracts the specified amount from the account balance and
    * returns the new balance
    */
    public float subtract(float amount) throws InsufficientFundsException,     java.rmi.RemoteException;

     

5.     Create Top Down EJB/RDB Mapping
    J2EE Perspective -> Navigator View 
     
    Right-click AccountAndTransferEJBModule
    Generate -> EJB to RDB maping
     
    Select Top Down way to build EJB to RDB maping 
     
    Verify:
    J2EE Perspective -> Navigator View 
    Schema.dbxmi and Map.mapxmi are both created
     
    Double click Map.mapxmi
     
6.      Create A Database Table
    Drag and drop Table.ddl out into a directory, eg: C:\temp

     
    Start -> Run -> db2cmd
    db2 connect to SAMPLE use db2admin using db2admin
    db2 list tables for all
    db2 -tf C:\temp\Table.ddl

    db2 insert into db2admin.account(accountid, type, balance) values(1, 1, 15.15)
    db2 insert into db2admin.account(accountid, type, balance) values(2, 1, 30.15)

    db2 select * from db2admin.account

7.     Define a Datasource name for our CMP
    J2EE Perspective -> Navigator View -> AccountAndTransferEJBModule -> META-INF
     
    Right mouse click ejb-jar.xml, Open with -> EJB Extension Editor
     
    Select "Bindings" tab
     
    JNDI Name:                    Account
    Datasource JNDI Name:        jdbc/SAMPLE
    Default use id:                db2admin
    Default password:            ************
    //note: "Ctrl + S" to save the change

8.     Define a Datasource to our Server Environment
    Open a Server Perspective
     
    File -> New -> Project -> Server Project
    Project Name:        SPC1
     
    -> Finish
    File -> New -> Server Instance and Configuration
    Server Name:        WASSVR1
         
    In "Server Configuration" window, Double click WASSVR1
     
    Select "Data source" tab
    Mark "COM.ibm.db2.jdbc.DB2ConnectionPoolDataSource" in JDBC driver list
     
    Add a Datasource defined in the JDBC driver select above
     
    Close WASSVR1 editor window
    
    Server Perspective -> Server Configuration window -> Server Configurations
    right mouse click WASSVR1
    Add Project -> AccountTransferEAR
     
    Verify:
    J2EE Perspective -> Navigator View 
    Right mouse click AccountTransferEJBModule
    Select Properties -> Server Preference
     
    
10. Genegrate Deployed Code
    J2EE Perspective -> J2EE View -> EJBModules
    Right mouse click AccountAndTransferEJBModule
    Generate -> Deploy code and RMIC Stub and Tie code
     
    -> Finish
    
Part3: Test CMP Bean
1.     Server Perspective -> Navigator View
    Right mouse click AccountAndTransferEJBModule
    Select Run on Server
     
    IBM EJB Test Client
     
    JNDI Explorer
    Click Account
     

    Click Account findByPrimaryKey(AccountKey)
     


     
    Invoke and Return
    Invoke
     
    Click Work with Object
     
    float getBalance()                15.15
    void setBalance(float)           12.15
    Start -> Run -> db2cmd
    db2 connect to sample user db2admin using db2admin
    db2 select * from Account
    C:\>db2 select * from account

    ACCOUNTID            TYPE        BALANCE
    -------------------- ----------- ------------------------
       1           1            +1.21500E+001
    2           1            +3.01500E+001

      2 record(s) selected.
    
2.     Stop Server
    Close EBJ Test Client window
    Server Perspective -> Servers View -> Server tab
    Right mouse click WASSVR1 -> Stop
     

Part 4:     Create Transfer Stateless Session EJB
1.    J2EE Perspective -> J2EE View        
    File -> New -> Enterprise Beans
     
    -> Next -> Next
     
    -> Finish
    Open TransferBean.java
     
    Add the following field and methods:

    private AccountHome accountHome = null;
    
    /**
    * Returns the balance of the account identified by the acctId
    * parameter
    */
public float getBalance(long acctId) throws javax.ejb.FinderException    {
        AccountKey key = new AccountKey(acctId);
        Account fromAccount;
        try {
            fromAccount = accountHome.findByPrimaryKey(key);
            return fromAccount.getBalance();
        } catch (FinderException ex) {
            throw new FinderException("Account " + acctId + "does not exist");
        } catch (RemoteException ex) {
            throw new EJBException("Cannot get balance for " + acctId);
        }
    }
    /**
    * Transfers the specified amount from the fromAcctId account
    * to the toAcctId account; throws InsufficientFundsException
    * if the account identified by the fromAcctId contains less
    * than the amount requested for tranfer.
    */
    public void transferFunds(long fromAcctId, long toAcctId, float amount)
        throws InsufficientFundsException, javax.ejb.FinderException {
        AccountKey fromKey = new AccountKey(fromAcctId);
        AccountKey toKey = new AccountKey(toAcctId);
        Account fromAccount, toAccount;
        try {
            fromAccount = accountHome.findByPrimaryKey(fromKey);
            toAccount = accountHome.findByPrimaryKey(toKey);
            toAccount.add(amount);
            fromAccount.subtract(amount);
        } catch (FinderException ex) {
throw new FinderException("Account " + fromAcctId + " does not exist.");
        } catch (RemoteException ex) {
            throw new EJBException();
        } catch (InsufficientFundsException ex) {
            mySessionCtx.setRollbackOnly();
throw new InsufficientFundsException("Insufficient funds in " + fromAcctId);
        }
    }
    /**
    * Gets the home object for the CMP Account EJB using ejb
    * references. This method returns a reference to the
    * AccountHome object when the bean is being created.
    */
    public AccountHome getHome() {
        try {
            InitialContext initCtx = new InitialContext();
            Object objref = initCtx.lookup("Account");
return (AccountHome) PortableRemoteObject.narrow(objref, AccountHome.class);
        } catch (NamingException ne) {
            ne.printStackTrace();
            throw new EJBException(
                "Error looking up AccountHome object: " + ne.getMessage());
        }
    }


    Add code into ejbCreate() of TransferBean.java

    accountHome = getHome();

     

2.    Promote getBalance and transferFunds to EJB Remote Interface

     

Part 5:    Test Both EJBs
1.    Regenerate the deployed code
    J2EE Perspective -> J2EE View -> EJB Modules
    Right mouse click AccountAndTransferEJBModule
     

    Select Generate -> Deploy code and RMIC Stub and Tie code
     

    2.     Start Server
        J2EE Perspective -> Navigator View
        Right mouse click AccountAndTransferEJBModule
        -> Run On Server
         
        
        Transfer
         

        Transfer create()
        Invoke
         

        Work with Object
         

        float getBalance()
         

        Invoke
         

        void transferFunds(long, long, float)
         









    



































    




















关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有