package examples;/**
* This is the local home interface for HelloBean.
* This interface is implemented by the EJB Server's
* tools - the implemented object is called the
* local home object, and serves as a factory for
* EJB local objects.
*/
public interface HelloLocalHome extends javax.ejb.EJBLocalHome
{ /*
* This method creates the EJB Object.
*
* @return The newly created EJB Object.
*/
HelloLocal create() throws javax.ejb.CreateException;
}package examples;/**
* This is the HelloBean remote interface.
*
* This interface is what clients operate on when
* they interact with EJB objects. The container
* vendor will implement this interface; the
* implemented object is the EJB object, which
* delegates invocations to the actual bean.
*/
public interface Hello extends javax.ejb.EJBObject
{ /**
* The one method - hello - returns a greeting to the client.
*/
public String hello() throws java.rmi.RemoteException;
}package examples;import javax.ejb.SessionContext;/**
* Demonstration stateless session bean.
*/
public class HelloBean implements javax.ejb.SessionBean
{
//
// EJB-required methods
//
public void ejbCreate()
{
System.out.println("ejbCreate()");
} public void ejbRemove()
{
System.out.println("ejbRemove()");
} public void ejbActivate()
{
System.out.println("ejbActivate()");
} public void ejbPassivate()
{
System.out.println("ejbPassivate()");
} public void setSessionContext(SessionContext ctx)
{
System.out.println("setSessionContext()");
} //
// Business methods
//
public String hello()
{
System.out.println("hello()");
return "Hello, World!";
}
}package examples;import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Properties;/**
* This class is an example of client code which invokes
* methods on a simple stateless session bean.
*/
public class HelloClient { public static void main(String[] args) throws Exception {
/*
* Setup properties for JNDI initialization.
*
* These properties will be read-in from
* the command-line.
*/
Properties props = System.getProperties(); /*
* Obtain the JNDI initial context.
*
* The initial context is a starting point for
* connecting to a JNDI tree. We choose our JNDI
* driver, the network location of the server, etc
* by passing in the environment properties.
*/
Context ctx = new InitialContext(props); /*
* Get a reference to the home object - the
* factory for Hello EJB Objects
*/
Object obj = ctx.lookup("HelloHome");
/*
* Home objects are RMI-IIOP objects, and so
* they must be cast into RMI-IIOP objects
* using a special RMI-IIOP cast.
*
* See Appendix X for more details on this.
*/
HelloHome home = (HelloHome)
javax.rmi.PortableRemoteObject.narrow(
obj, HelloHome.class); /*
* Use the factory to create the Hello EJB Object
*/
Hello hello = home.create(); /*
* Call the hello() method on the EJB object. The
* EJB object will delegate the call to the bean,
* receive the result, and return it to us.
*
* We then print the result to the screen.
*/
System.out.println(hello.hello()); /*
* Done with EJB Object, so remove it.
* The container will destroy the EJB object.
*/
hello.remove();
}
}
package examples;/**
* This is the home interface for HelloBean. This interface
* is implemented by the EJB Server's tools - the
* implemented object is called the Home Object, and serves
* as a factory for EJB Objects.
*
* One create() method is in this Home Interface, which
* corresponds to the ejbCreate() method in HelloBean.
*/
public interface HelloHome extends javax.ejb.EJBHome
{ /*
* This method creates the EJB Object.
*
* @return The newly created EJB Object.
*/
Hello create() throws java.rmi.RemoteException,
javax.ejb.CreateException;
}
package examples;/**
* This is the HelloBean local interface.
*
* This interface is what local clients operate
* on when they interact with EJB local objects.
* The container vendor will implement this
* interface; the implemented object is the
* EJB local object, which delegates invocations
* to the actual bean.
*/
public interface HelloLocal extends javax.ejb.EJBLocalObject
{ /**
* The one method - hello - returns a greeting to the client.
*/
public String hello();
}
* This is the local home interface for HelloBean.
* This interface is implemented by the EJB Server's
* tools - the implemented object is called the
* local home object, and serves as a factory for
* EJB local objects.
*/
public interface HelloLocalHome extends javax.ejb.EJBLocalHome
{ /*
* This method creates the EJB Object.
*
* @return The newly created EJB Object.
*/
HelloLocal create() throws javax.ejb.CreateException;
}package examples;/**
* This is the HelloBean remote interface.
*
* This interface is what clients operate on when
* they interact with EJB objects. The container
* vendor will implement this interface; the
* implemented object is the EJB object, which
* delegates invocations to the actual bean.
*/
public interface Hello extends javax.ejb.EJBObject
{ /**
* The one method - hello - returns a greeting to the client.
*/
public String hello() throws java.rmi.RemoteException;
}package examples;import javax.ejb.SessionContext;/**
* Demonstration stateless session bean.
*/
public class HelloBean implements javax.ejb.SessionBean
{
//
// EJB-required methods
//
public void ejbCreate()
{
System.out.println("ejbCreate()");
} public void ejbRemove()
{
System.out.println("ejbRemove()");
} public void ejbActivate()
{
System.out.println("ejbActivate()");
} public void ejbPassivate()
{
System.out.println("ejbPassivate()");
} public void setSessionContext(SessionContext ctx)
{
System.out.println("setSessionContext()");
} //
// Business methods
//
public String hello()
{
System.out.println("hello()");
return "Hello, World!";
}
}package examples;import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Properties;/**
* This class is an example of client code which invokes
* methods on a simple stateless session bean.
*/
public class HelloClient { public static void main(String[] args) throws Exception {
/*
* Setup properties for JNDI initialization.
*
* These properties will be read-in from
* the command-line.
*/
Properties props = System.getProperties(); /*
* Obtain the JNDI initial context.
*
* The initial context is a starting point for
* connecting to a JNDI tree. We choose our JNDI
* driver, the network location of the server, etc
* by passing in the environment properties.
*/
Context ctx = new InitialContext(props); /*
* Get a reference to the home object - the
* factory for Hello EJB Objects
*/
Object obj = ctx.lookup("HelloHome");
/*
* Home objects are RMI-IIOP objects, and so
* they must be cast into RMI-IIOP objects
* using a special RMI-IIOP cast.
*
* See Appendix X for more details on this.
*/
HelloHome home = (HelloHome)
javax.rmi.PortableRemoteObject.narrow(
obj, HelloHome.class); /*
* Use the factory to create the Hello EJB Object
*/
Hello hello = home.create(); /*
* Call the hello() method on the EJB object. The
* EJB object will delegate the call to the bean,
* receive the result, and return it to us.
*
* We then print the result to the screen.
*/
System.out.println(hello.hello()); /*
* Done with EJB Object, so remove it.
* The container will destroy the EJB object.
*/
hello.remove();
}
}
package examples;/**
* This is the home interface for HelloBean. This interface
* is implemented by the EJB Server's tools - the
* implemented object is called the Home Object, and serves
* as a factory for EJB Objects.
*
* One create() method is in this Home Interface, which
* corresponds to the ejbCreate() method in HelloBean.
*/
public interface HelloHome extends javax.ejb.EJBHome
{ /*
* This method creates the EJB Object.
*
* @return The newly created EJB Object.
*/
Hello create() throws java.rmi.RemoteException,
javax.ejb.CreateException;
}
package examples;/**
* This is the HelloBean local interface.
*
* This interface is what local clients operate
* on when they interact with EJB local objects.
* The container vendor will implement this
* interface; the implemented object is the
* EJB local object, which delegates invocations
* to the actual bean.
*/
public interface HelloLocal extends javax.ejb.EJBLocalObject
{ /**
* The one method - hello - returns a greeting to the client.
*/
public String hello();
}
解决方案 »
- hibernate save 得到主键
- 一个让我很郁闷的问题
- @Privilege(userType=PrivilegeType.系统管理员,message="需管管理员权限!")
- Struts2 Freemarker 遍历List出错,请教如何解决?
- 谁能给我一个级联保存与更新的完整例子
- 面试题
- 关于JSP输出PDF的问题,大家帮忙啊,新手
- 一个关于jdbc操作sqlserver2005的问题
- 华为的JAVA版CMPP API DEMO有错,解决的100分,谢谢~不够分再加~
- 关于如何在jbuilder+tomcat环境下配置数据源的问题??
- ejb返回java bean的奇怪问题?(高分)
- cmp 怎么和数据库联系在一起?
建议看看,有实例的.
finder方法的查询速度要快一些,但是因为它是要讲结果load到内存中的,所以如果是不常用的一般查询建议不要用。
例如,好多CMP的XML中都写了findByPrimeryPK,因为根据主键检索是最常用的一种,所以一般都会写上,以便提高程序的效率。
throws SQLException
{
String sql = " SELECT ID, NAME FROM CAT ORDER BY ID ASC ";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
ArrayList list = new ArrayList();//定义List用于存储记录
while(rs.next()){
String[] rec = new String[2]; //定义一个string数组,用于取得对应的记录,结果只有2列所以只定义了string[2]
rec[0] = rs.getString("CATELOG_ID");
rec[1] = rs.getString("CATELOG_NAME");
list.add(rec);
}
try{
rs.close();
}catch(SQLException se){
se.printE();
rs = null;
}
try{
stmt.close();
}catch(SQLException se){
se.printE();
stmt = null;
}
return list;
}
取数据的过程相反就可以了。
其中:
rec[0] = rs.getString("CATELOG_ID");
rec[1] = rs.getString("CATELOG_NAME");应为:
rec[0] = rs.getString("ID");
rec[1] = rs.getString("NAME");