求EJB的例子?
1、数据表Insert的例子
2、数据表select的例子,返回多条记录。
问题点数:100、回复次数:17Top
1 楼trier()回复于 2002-10-16 14:55:02 得分 5
一两段程序也说不清,最好看书。
我这里也有自己项目中写的,有兴趣可以联系。Top
2 楼chenyuan_tongji(codeguru)回复于 2002-10-16 14:59:50 得分 5
BMP的还是CMP的,说!Top
3 楼maxtiger(tiger)回复于 2002-10-16 17:03:33 得分 5
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();
}
Top
4 楼chenyuan_tongji(codeguru)回复于 2002-10-16 17:31:33 得分 5
楼上的,你这就不对了,人家要数据库操作的ejb,你贴的是什么?不负责任!!!Top
5 楼chime(chime)回复于 2002-10-17 14:37:52 得分 0
数据库操作的ejbTop
6 楼chime(chime)回复于 2002-10-17 17:19:45 得分 0
chenyuan_tongji(codeguru):CMP的,数据库的select和insert操作,带参数。Top
7 楼shixiaoxiang(帅得惊动了党!)回复于 2002-10-17 17:42:27 得分 5
最新的两期《程序员》上有李维先生的文章《EJB系统开发实战录 >>
建议看看,有实例的.Top
8 楼yyt99(疯度翩翩)回复于 2002-10-17 17:46:22 得分 0
如果你实在找不到,我这有例子,但你不能外泄!!!
我的信箱:yytFly@hotmail.com
Top
9 楼forjie(我爱我家)回复于 2002-10-17 18:06:31 得分 5
建议去看看《精通ejb》Top
10 楼chenyuan_tongji(codeguru)回复于 2002-10-18 18:29:20 得分 5
chime(天涯海角):你要cmp的,你的AppServer和database是什么?还是随便给例子?Top
11 楼chime(chime)回复于 2002-10-22 14:11:40 得分 0
weblogic6.1+Jbulider7+Oracle8i
session bean 调用cmp 实体bean,cmp 实体bean操作数据库。
Top
12 楼chime(chime)回复于 2002-10-23 13:44:27 得分 0
select 返回多条记录是如何处理的?Top
13 楼haode(好的)回复于 2002-10-23 17:36:20 得分 0
书上都有的Top
14 楼chime(chime)回复于 2002-10-24 10:40:18 得分 0
cmp entity bean 的finder和select有什么区别???Top
15 楼yyt99(疯度翩翩)回复于 2002-10-24 12:44:07 得分 15
finder 是使用EJB的QL语言的一种快速查询方法,select 则是通过Jdbc连接到数据库后的查询。
finder方法的查询速度要快一些,但是因为它是要讲结果load到内存中的,所以如果是不常用的一般查询建议不要用。
例如,好多CMP的XML中都写了findByPrimeryPK,因为根据主键检索是最常用的一种,所以一般都会写上,以便提高程序的效率。Top
16 楼yyt99(疯度翩翩)回复于 2002-10-24 12:51:30 得分 50
返回多条记录的处理:
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;
}
取数据的过程相反就可以了。Top
17 楼yyt99(疯度翩翩)回复于 2002-10-24 12:53:27 得分 0
不好意思。纠正一下。
其中:
rec[0] = rs.getString("CATELOG_ID");
rec[1] = rs.getString("CATELOG_NAME");
应为:
rec[0] = rs.getString("ID");
rec[1] = rs.getString("NAME");
Top




