ContactBean代码:
package bmpbean;import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.ejb.FinderException;
import javax.ejb.*;
import javax.sql.*;
import java.sql.*;
import javax.naming.*;public class ContactBean implements EntityBean {
    EntityContext entityContext;
    String email;
    String last;
    String first;
    private Connection con;
    private PreparedStatement ps = null;
    public String ejbCreate(String last) throws CreateException {
        setLast(last);
        return "";
    }    public void ejbPostCreate(String last) throws CreateException {
    }    public void ejbRemove() throws RemoveException {
    }    public void setEmail(String email) {
        this.email = email;
    }    public String getEmail() {
        return email;
    }    public void setLast(String last) {
        this.last = last;
    }    public String getLast() {
        return last;
    }    public void setFirst(String first) {
        this.first = first;
    }    public String getFirst() {
        return first;
    }    public java.lang.String ejbFindByPrimaryKey(java.lang.String last) throws
            FinderException {
        System.out.println("---ejbFindByPrimaryKey begins---");
        try {
            ps = con.prepareStatement(
                    "SELECT  FIRST, LAST, EMAIL FROM contact WHERE  LAST = ? ");
            ps.setString(1, last);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                this.first = rs.getString(1);
                this.last = rs.getString(2);
                this.email = rs.getString(3);
            } else {
                System.out.println("Find Error");
            }
        } catch (SQLException ex) {
            throw new EJBException("Exception in ejbFindByPrimaryKey:" + ex);
        }
        try {
            ps.close();
        } catch (Exception e) {
            System.out.println("Cannt close statement:" + e);
        }
        System.out.println("---ejbFindByPrimaryKey ends---");
        return last;
    }    public void ejbLoad() {
    }    public void ejbStore() {
    }    public void ejbActivate() {
    }    public void ejbPassivate() {
    }    public void unsetEntityContext() {
        this.entityContext = null;
    }    public void setEntityContext(EntityContext entityContext) {
        this.entityContext = entityContext;
        try {
            System.out.println("make connection in setEntityContext");
            makeConnection();
        } catch (Exception ex) {
            throw new EJBException("Unable to connect to database. " + ex);
        }
    }    private Connection makeConnection() {
        try {
            InitialContext ic = new InitialContext();
            DataSource ds = (DataSource) ic.lookup("jdbc:comp/env/oracle1jndi");
            con = ds.getConnection();
            return con;
        } catch (Exception ex) {
            System.out.println("Exception in connection:" + ex);
            return null;
        }
    }}ContactRemote代码:
package bmpbean;import javax.ejb.EJBObject;
import java.rmi.RemoteException;public interface ContactRemote extends EJBObject {
    public void setEmail(String email) throws RemoteException;    public String getEmail() throws RemoteException;    public String getLast() throws RemoteException;    public void setFirst(String first) throws RemoteException;    public String getFirst() throws RemoteException;
}
ContactRemoteHome代码:
package bmpbean;import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import java.rmi.RemoteException;
import javax.ejb.FinderException;public interface ContactRemoteHome extends EJBHome {
    public ContactRemote create(String last) throws CreateException,
            RemoteException;    public ContactRemote findByPrimaryKey(String last) throws FinderException,
            RemoteException;
}

解决方案 »

  1.   

    ContactTestClient1代码:
    package bmpbean;import javax.naming.*;
    import javax.rmi.PortableRemoteObject;
    import java.lang.String;
    import java.util.Properties;public class ContactTestClient1 {
        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 ContactRemote contactRemote = null;
        private ContactRemoteHome contactRemoteHome = null;    //Construct the EJB test client
        public ContactTestClient1() {
            initialize();
        }    public void initialize() {        try {            //get naming context
                Context context = getInitialContext();
                //look up jndi name
                Object ref = context.lookup("Contact");
                //look up jndi name and cast to Home interface
                contactRemoteHome = (ContactRemoteHome) PortableRemoteObject.narrow(
                        ref, ContactRemoteHome.class);
                System.out.println("find a record whose key is:" + "Zhang");
                contactRemote = contactRemoteHome.findByPrimaryKey("Zhang");
                System.out.println("getFirst(): " + contactRemote.getFirst() +
                                   " getLast():" + contactRemote.getLast() +
                                   " getEmail():" + contactRemote.getEmail());        } 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 ContactRemote create(String last) {
            try {
                contactRemote = contactRemoteHome.create(last);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return contactRemote;
        }    public ContactRemote findByPrimaryKey(String last) {
            try {
                contactRemote = contactRemoteHome.findByPrimaryKey(last);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return contactRemote;
        }    //----------------------------------------------------------------------------
        // Methods that use Remote interface methods to access data through the bean
        //----------------------------------------------------------------------------    public void setEmail(String email) {
            if (contactRemote == null) {
                System.out.println("Error in setEmail(): " + ERROR_NULL_REMOTE);
                return;
            }        try {
                contactRemote.setEmail(email);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }    public String getEmail() {
            String returnValue = "";
            if (contactRemote == null) {
                System.out.println("Error in getEmail(): " + ERROR_NULL_REMOTE);
                return returnValue;
            }        try {
                returnValue = contactRemote.getEmail();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return returnValue;
        }    public String getLast() {
            String returnValue = "";
            if (contactRemote == null) {
                System.out.println("Error in getLast(): " + ERROR_NULL_REMOTE);
                return returnValue;
            }        try {
                returnValue = contactRemote.getLast();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return returnValue;
        }    public void setFirst(String first) {
            if (contactRemote == null) {
                System.out.println("Error in setFirst(): " + ERROR_NULL_REMOTE);
                return;
            }        try {
                contactRemote.setFirst(first);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }    public String getFirst() {
            String returnValue = "";
            if (contactRemote == null) {
                System.out.println("Error in getFirst(): " + ERROR_NULL_REMOTE);
                return returnValue;
            }        try {
                returnValue = contactRemote.getFirst();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return returnValue;
        }    //----------------------------------------------------------------------------
        // Utility Methods
        //----------------------------------------------------------------------------    public ContactRemoteHome getHome() {
            return contactRemoteHome;
        }    //Main method
        public static void main(String[] args) {
            ContactTestClient1 client = new ContactTestClient1();
            // Use the getHome() method of the client object to call Home interface
            // methods that will return a Remote interface reference.  Then
            // use that Remote interface reference to access the EJB.
        }
    }
      

  2.   

    ejb.jar.xml配置文件:
    <?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>
      <display-name>BmpBeanModel</display-name>
      <enterprise-beans>
        <entity>
          <ejb-name>Contact</ejb-name>
          <home>bmpbean.ContactRemoteHome</home>
          <remote>bmpbean.ContactRemote</remote>
          <ejb-class>bmpbean.ContactBean</ejb-class>
          <persistence-type>Bean</persistence-type>
          <prim-key-class>java.lang.String</prim-key-class>
          <reentrant>False</reentrant>
        </entity>
      </enterprise-beans>
      <assembly-descriptor>
        <container-transaction>
          <method>
            <ejb-name>Contact</ejb-name>
            <method-name>*</method-name>
          </method>
          <trans-attribute>Required</trans-attribute>
        </container-transaction>
      </assembly-descriptor>
    </ejb-jar>weblogic-ejb-jar.xml配置文件:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN" "http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd"><weblogic-ejb-jar>
      <weblogic-enterprise-bean>
        <ejb-name>Contact</ejb-name>
        <jndi-name>Contact</jndi-name>
      </weblogic-enterprise-bean>
    </weblogic-ejb-jar>
      

  3.   

    Caused by: java.lang.NullPointerException 
    at bmpbean.ContactBean.ejbFindByPrimaryKey(ContactBean.java:59) con没有初始化 为null
      

  4.   

    不知怎的,突然有可以了。还是谢谢两位。可能数weblogic连接池没问题。