●●●●200分:BMP和CMP在Bean类、home接口的程序结构和内容上有什么区别,比如:findBy、、、和 method等方面的区别????

zsq666 2003-10-20 06:27:23
最好具体一点!!
...全文
34 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
bobshi 2004-01-01
  • 打赏
  • 举报
回复
学习!
zsq666 2004-01-01
  • 打赏
  • 举报
回复
up
dan100000 2003-12-25
  • 打赏
  • 举报
回复
bmp和数据库之间的联系还有自身的状态是bean自身管理的,你需要实现相应的管理方法,而cmp中的这些内容是由容器管理的,从编码的角度和运行的效率来讲,更好一些吧,不过部署文件里的内容要多一些了(其实也很简单)
luckycat 2003-10-23
  • 打赏
  • 举报
回复
远程接口:
package ejb;

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

public interface User extends javax.ejb.EJBLocalObject {
public String getUserID();
public void setName(String name);
public String getName();
public void setSex(int sex);
public int getSex();
}
luckycat 2003-10-23
  • 打赏
  • 举报
回复
Home接口:
package ejb;

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

public interface UserHome extends javax.ejb.EJBLocalHome {
public User create(String name, int sex) throws CreateException;
public User findByPrimaryKey(String userID) throws FinderException;
}
luckycat 2003-10-23
  • 打赏
  • 举报
回复
bean实现类:

package ejb;

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

public class UserBean implements EntityBean {
EntityContext entityContext;
java.lang.String userID;
java.lang.String name;
int sex;
public java.lang.String ejbCreate(java.lang.String name, int sex) throws CreateException {
Connection conn = null;
PreparedStatement pStmt = null;
try
{
conn = getConnection();
pStmt = conn.prepareStatement("insert into mytest(id,name,sex) values(mytest_seq.nextVal,?,?)");
pStmt.setString(1,name);
pStmt.setInt(2,sex);
pStmt.executeUpdate();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
if(pStmt != null) pStmt.close();
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}
setName(name);
setSex(sex);
try
{
String maxID = "";
pStmt = conn.prepareStatement("select max(id) from mytest");
ResultSet rs = pStmt.executeQuery();
if(rs != null && rs.next())
maxID = rs.getString(1);
if(rs != null)
rs.close();
return maxID;
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
finally
{
try
{
if (pStmt != null)
pStmt.close();
if (conn != null)
conn.close();
}
catch(SQLException e)
{
System.err.println(e.getMessage());
}
}
return null;

}
public void ejbPostCreate(java.lang.String name, int sex) throws CreateException {
/**@todo Complete this method*/
}
public void ejbRemove() throws RemoveException {
Connection conn = null;
PreparedStatement pStmt = null;
try
{
conn = getConnection();
pStmt = conn.prepareStatement("delete from mytest where id=?");
pStmt.setString(1,userID);
pStmt.executeUpdate();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
if(pStmt != null) pStmt.close();
if(conn != null) conn.close();
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}

}
public java.lang.String getUserID() {
return userID;
}
public java.lang.String getName() {
return name;
}
public int getSex() {
return sex;
}
public java.lang.String ejbFindByPrimaryKey(java.lang.String userID) throws FinderException {
Connection conn = null;
PreparedStatement pStmt = null;
try
{
conn = getConnection();
pStmt = conn.prepareStatement("select * from mytest where id=?");
pStmt.setString(1,userID);
ResultSet rs = pStmt.executeQuery();
if(rs.next())
return userID;
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
if(pStmt != null) pStmt.close();
if(conn != null) conn.close();
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}

return null;
}
public void ejbLoad() {
Connection conn = null;
PreparedStatement pStmt = null;
String userID = (String)entityContext.getPrimaryKey();
try
{
conn = getConnection();
pStmt = conn.prepareStatement("select * from mytest where id=?");
pStmt.setString(1,userID);
ResultSet rs = pStmt.executeQuery();
if(rs != null && rs.next())
{
name = rs.getString("name");
sex = rs.getInt("sex");
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
if(pStmt != null) pStmt.close();
if(conn != null) conn.close();
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}
}
public void ejbStore() {
Connection conn = null;
PreparedStatement pStmt = null;
try
{
conn = getConnection();
pStmt = conn.prepareStatement("update mytest set name=?,sex=? where id=?");
pStmt.setString(1,name);
pStmt.setInt(2,sex);
pStmt.setString(3,userID);
pStmt.executeUpdate();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
if(pStmt != null) pStmt.close();
if(conn != null) conn.close();
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}

}
public void ejbActivate() {
/**@todo Complete this method*/
}
public void ejbPassivate() {
/**@todo Complete this method*/
}
public void unsetEntityContext() {
this.entityContext = null;
}
public void setEntityContext(EntityContext entityContext) {
this.entityContext = entityContext;
}
public Connection getConnection() throws Exception {
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
prop.put(Context.PROVIDER_URL,"t3://localhost:7001");
prop.put(Context.SECURITY_PRINCIPAL,"codebean");
prop.put(Context.SECURITY_CREDENTIALS,"12345678");
try
{
Context ctx = new InitialContext(prop);
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("OracleJdbc");
return ds.getConnection();
}
catch(NamingException e)
{
throw new Exception(e.getMessage());
}
catch(SQLException e)
{
throw new Exception(e.getMessage());
}
}
public void setName(java.lang.String name) {
this.name = name;
}
public void setSex(int sex) {
this.sex = sex;
}
}
zkjbeyond 2003-10-23
  • 打赏
  • 举报
回复
我有!可我没有ftp
xmqds 2003-10-23
  • 打赏
  • 举报
回复
我来!
BMP的Bean类:需实现大量的数据庫逻辑即需编写大量的JDBC代码!!!
CMP的Bean类:只关心业务逻辑,但必须为抽象的类,以便容器实现子类,来封装数据逻辑。
其它的(比如Home接口等)一模一样。所以BMP和CMP很容易转换

总的概括就这样子。
xwlovesh 2003-10-23
  • 打赏
  • 举报
回复
up!!!

http://xieweibbs.topcities.com
zsq666 2003-10-23
  • 打赏
  • 举报
回复
难道没人给指导一下!
zhangsq 2003-10-22
  • 打赏
  • 举报
回复
我也想知道!up
naxin 2003-10-22
  • 打赏
  • 举报
回复
tOO EasY
Wnyu 2003-10-22
  • 打赏
  • 举报
回复
太基础了!建议卖本书看看再问。
lzl123 2003-10-21
  • 打赏
  • 举报
回复
是啊, zsq666 (bluechina) 兄,看看Mastering EJB2或者到sun的网站上下一份EJB Specification,你的问题就解了
llll039901 2003-10-21
  • 打赏
  • 举报
回复
<<精通ejb 2>>请买吧!
zsq666 2003-10-21
  • 打赏
  • 举报
回复
我的信箱:zsq666@263.net
zsq666 2003-10-21
  • 打赏
  • 举报
回复
lzl123(潜龙)大哥!

能否给个BMP,实现按主键查询、删除相应记录的例子!
特别感谢!
lzl123 2003-10-21
  • 打赏
  • 举报
回复
在home接口种声明findByPrimaryKey();在Bean类的ejbFindByPrimaryKey中实现
zsq666 2003-10-21
  • 打赏
  • 举报
回复
各位好,能否给小弟一说:
在BMP中,findByPrimaryKey()是不是要自己定义,在什么地方定义???
naxin 2003-10-20
  • 打赏
  • 举报
回复
请买一本<<精通ejb 2>>中文版,

大家在这说一气,估计做成讲义都够了. :-)

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧