当前位置: 技术问答>java相关
求EJB的例子?
来源: 互联网 发布时间:2015-10-10
本文导语: 1、数据表Insert的例子 2、数据表select的例子,返回多条记录。 | 返回多条记录的处理: public static ArrayList getCataList(Connection conn) throws SQLException { String sql = " SELECT ID, NAME F...
1、数据表Insert的例子
2、数据表select的例子,返回多条记录。
2、数据表select的例子,返回多条记录。
|
返回多条记录的处理:
public static ArrayList getCataList(Connection conn)
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;
}
取数据的过程相反就可以了。
public static ArrayList getCataList(Connection conn)
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;
}
取数据的过程相反就可以了。
|
finder 是使用EJB的QL语言的一种快速查询方法,select 则是通过Jdbc连接到数据库后的查询。
finder方法的查询速度要快一些,但是因为它是要讲结果load到内存中的,所以如果是不常用的一般查询建议不要用。
例如,好多CMP的XML中都写了findByPrimeryPK,因为根据主键检索是最常用的一种,所以一般都会写上,以便提高程序的效率。
finder方法的查询速度要快一些,但是因为它是要讲结果load到内存中的,所以如果是不常用的一般查询建议不要用。
例如,好多CMP的XML中都写了findByPrimeryPK,因为根据主键检索是最常用的一种,所以一般都会写上,以便提高程序的效率。
|
一两段程序也说不清,最好看书。
我这里也有自己项目中写的,有兴趣可以联系。
我这里也有自己项目中写的,有兴趣可以联系。
|
BMP的还是CMP的,说!
|
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();
}
|
楼上的,你这就不对了,人家要数据库操作的ejb,你贴的是什么?不负责任!!!
|
最新的两期《程序员》上有李维先生的文章《EJB系统开发实战录 >>
建议看看,有实例的.
建议看看,有实例的.
|
建议去看看《精通ejb》
|
chime(天涯海角):你要cmp的,你的AppServer和database是什么?还是随便给例子?