[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[jfriends-ml 11181] EJB-SQL と Dynamic Query (3/3)



  EJB-SQL と Dynamic Query (2/3) の続きです。
------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans
2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd";>
<ejb-jar>
    <enterprise-beans>
        <entity>
            <display-name>Book</display-name>
            <ejb-name>Book</ejb-name>
            <home>bookejb.BookRemoteHome</home>
            <remote>bookejb.BookRemote</remote>
            <ejb-class>bookejb.BookBean</ejb-class>
            <persistence-type>Container</persistence-type>
            <prim-key-class>java.lang.Integer</prim-key-class>
            <reentrant>False</reentrant>
            <cmp-version>2.x</cmp-version>
            <abstract-schema-name>Book</abstract-schema-name>
            <cmp-field>
                <field-name>id</field-name>
            </cmp-field>
            <cmp-field>
                <field-name>isbn</field-name>
            </cmp-field>
            <cmp-field>
                <field-name>title</field-name>
            </cmp-field>
            <primkey-field>id</primkey-field>
            <resource-ref>
                <description />
                <res-ref-name>jdbc/MySQL</res-ref-name>
                <res-type>javax.sql.DataSource</res-type>
                <res-auth>Container</res-auth>
            </resource-ref>
        </entity>
    </enterprise-beans>
    <assembly-descriptor>
        <container-transaction>
            <method>
                <ejb-name>Book</ejb-name>
                <method-name>*</method-name>
            </method>
            <trans-attribute>Required</trans-attribute>
        </container-transaction>
    </assembly-descriptor>
</ejb-jar>
------------------------------------------------------------------------------

------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Borland Software Corporation//DTD Enterprise
JavaBeans 2.0//EN" "http://www.borland.com/devsupport/appserver/dtds/
ejb-jar_2_0-borland.dtd">
<ejb-jar>
    <enterprise-beans>
        <entity>
            <ejb-name>Book</ejb-name>
            <bean-home-name>BookRemote</bean-home-name>
            <resource-ref>
                <res-ref-name>jdbc/MySQL</res-ref-name>
                <jndi-name>serial://datasources/MySQL</jndi-name>
            </resource-ref>
            <cmp2-info>
                <cmp-field>
                    <field-name>id</field-name>
                    <column-name>ID</column-name>
                </cmp-field>
                <cmp-field>
                    <field-name>isbn</field-name>
                    <column-name>ISBN</column-name>
                </cmp-field>
                <cmp-field>
                    <field-name>title</field-name>
                    <column-name>TITLE</column-name>
                </cmp-field>
                <table-name>book</table-name>
            </cmp2-info>
            <property>
                <prop-name>ejb-designer-id</prop-name>
                <prop-type>String</prop-type>
                <prop-value>Book</prop-value>
            </property>
        </entity>
    </enterprise-beans>
    <table-properties>
        <table-name>book</table-name>
        <column-properties>
            <column-name>ID</column-name>
        </column-properties>
        <column-properties>
            <column-name>ISBN</column-name>
        </column-properties>
        <column-properties>
            <column-name>TITLE</column-name>
        </column-properties>
        <property>
            <prop-name>datasource</prop-name>
            <prop-type>String</prop-type>
            <prop-value>serial://datasources/MySQL</prop-value>
        </property>
    </table-properties>
    <assembly-descriptor />
</ejb-jar>
------------------------------------------------------------------------------

------------------------------------------------------------------------------
package bookejb;

import java.rmi.*;
import java.util.*;
import javax.naming.*;
import javax.rmi.*;
import javax.ejb.*;

public class BookTestClient1 extends Object {
  private BookRemoteHome bookRemoteHome = null;

  public BookTestClient1() {
    initialize();
  }

  public void initialize() {
    try {
      Context context = new InitialContext();
      Object ref = context.lookup("BookRemote");
      bookRemoteHome =
       (BookRemoteHome) PortableRemoteObject.narrow(ref, BookRemoteHome.class);
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  public BookRemoteHome getHome() {
    return bookRemoteHome;
  }

  //Main メソッド
  public static void main(String[] args) {
    BookTestClient1 client = new BookTestClient1();
    // insert test data
    try {
      client.getHome().create(new Integer(1), "1123412345", "Java Programming");
      client.getHome().create(new Integer(2), "1567856789", "EJB Patterns");
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
    // find by Dynamic SQL
    try {
      String sql = "select * from book where ISBN='1123412345'";
      Collection array = client.getHome().execDynaSQL(sql);
      Iterator iter = array.iterator();
      while( iter.hasNext() )
      {
        Handle ejbhandle = (Handle)iter.next();
        BookRemote book =
            (BookRemote)PortableRemoteObject.narrow(ejbhandle.getEJBObject(),
                                                    BookRemote.class);
        System.out.println(book.getTitle());
      }
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }
}
------------------------------------------------------------------------------

-- 
高橋智宏