赛迪网 > IT技术 Java > 技术动态
  IT资讯搜索
 
IT产品搜索
[程序开发][网管世界][网络安全][数据库技术]
[操作系统][嘉宾聊天·在线访谈][活动集锦]
[精彩专题][Symantec专区][订阅IT技术周刊]
[开发论坛][网管论坛][安全论坛][数据库论坛]
[操作系统论坛][Sybase专区][IBM dW技术专区]
[病毒求助][病毒与漏洞播报][文档·源码下载]

基础:关于EJB返回值的最好的解决方案

发布时间:2007.12.28 04:43     来源:赛迪网    作者:lu_-yi

相信很多人都有如此之困惑,得此解决方法不敢独享,公之于众,以利后来人。 
  声明:此方法的至于彭璐大侠,彭大侠可能不常上网,这麽好的方法也不告诉我等之小虾米,只好代劳了。
  好了,不废话了,有两种方法: 
  1、用vector: 
  /** 
  * Finds all EJBeans with a balance greater than a given amount. 
  * Returns an Enumeration of found EJBean primary keys. 
  * 
  * @param balanceGreaterThan double Test Amount 
  * @return Enumeration EJBean Primary Keys 
  * @exception javax.ejb.EJBException 
  * if there is a communications or systems failure 
  */ 
  public Enumeration ejbFindBigAccounts(double balanceGreaterThan) { 
  log("ejbFindBigAccounts (balance > " + balanceGreaterThan + ")"); 
  Connection con = null; 
  PreparedStatement ps = null; 
  try { 
  con = getConnection(); 
  ps = con.prepareStatement("select id from ejbAccounts where bal ?"); 
  ps.setDouble(1, balanceGreaterThan); 
  ps.executeQuery(); 
  ResultSet rs = ps.getResultSet(); 
  Vector v = new Vector(); 
  String pk; 
  while (rs.next()) { 
  pk = rs.getString(1); 
  v.addElement(pk); 
  return v.elements(); 
  } catch (SQLException sqe) { 
  log("SQLException: " + sqe); 
  throw new EJBException (sqe); 
  } finally { 
  cleanup(con, ps); 
  } 
  } 
  结论:不爽,不方便。 
  2、RowSet 
  RowSet tutorial chapter : 
  http://developer.java.sun.com/developer/Books/JDBCTutorial/chapter5.html 
  rowset是个interface,需要有东西去实现它,sun的规范中给了三个class:cachedrowset,jdbcrowset,webrowset,如果去查jdk1.4 doc和j2skee1.2,有rowset,却没有那三个class,一般的开发工具(至少我的wsad)中也是这样,所以需要下jdbc2.0 opt-pack: 
  http://developer.java.sun.com/developer/earlyAccess/crs/ 
  下下来了再怎么办呢? 
  装呗! 
  怎么装呢? 
  setup呀! 
  没有呀? 
  啊,没setup呀,sun干什么吃的,连setup都不做个,也太懒了吧。 
  哎,我们确实是都被ms惯坏了,看到只有jar,没setup就没辙了,大家好好想想,java最大的特性是什么,就是它的类库可以自由扩充呀,现在明白该怎么做了吧: 
  1、解包,得到rowset.jar,放在哪随您的意,别丢了就行。 
  2、在您的开发工具中增加一个路径,如:ROWSET_PATH对应:d:\jdk1.4\jre\rowset.jar(和1的路径对应就行)。 
  3、右键您的工程文件,出现:property(大多数工具应该都有吧),加上rowset_path。 
  4、在您的源文件中:import sun.jdbc.rowset.*; 
  OK,搞定!下面就看您的了。(当然也可以把rowset压到jre里去) 
  应该说rowset(其实主要是CachedRowSet)真的是个好东西,和ms ado的resultset和borland的tclientset非常相似,最大的好处是Cache功能!
  好了,看例子吧: 
  ////server端///
  package example4; 
  import java.sql.*; 
  import javax.sql.*; 
  import sun.jdbc.rowset.*; 
  import javax.naming.*; 
  import javax.ejb.*; 
  public class CoffeesBean implements SessionBean { 
  private SessionContext sc = null; 
  private Context ctx = null; 
  private DataSource ds = null; 
  public CoffeesBean () {} 
  public void ejbCreate() throws CreateException { 
  try { 
  ctx = new InitialContext(); 
  ds = (DataSource)ctx.lookup("jdbc/CoffeesDB"); 
  } 
  catch (Exception e) { 
  System.out.println(e.getMessage()); 
  throw new CreateException(); 
  } 
  }
  public RowSet getCoffees() throws SQLException { 
  Connection con = null; 
  ResultSet rs; 
  CachedRowSet crs; 
  try { 
  con = ds.getConnection("webCustomer", "webPassword"); 
  Statement stmt = con.createStatement(); 
  rs = stmt.executeQuery("select * from coffees"); 
  
  crs = new CachedRowSet(); 
  crs.populate(rs); 
  // the writer needs this because JDBC drivers 
  // don't provide this meta-data. 
  crs.setTableName("coffees"); 
  rs.close(); 
  stmt.close(); 
  } finally { 
  if (con != null) 
  con.close(); 
  } 
  return rset;
  } 
  public updateCoffees(RowSet rs) throws SQLException { 
  Connection con = null; 
  try { 
  CachedRowSet crs = (CachedRowSet)rs; 
  con = ds.getConnection("webCustomer", "webPassword"); 
  // moves the changes back to the database 
  crs.acceptChanges(con); 
  } finally { 
  if (con != null) 
  con.close(); 
  } 
  } 
  // 
  // Methods inherited from SessionBean 
  // 
  public void setSessionContext(SessionContext sc) { 
  this.sc = sc; 
  public void ejbRemove() {} 
  public void ejbPassivate() {} 
  public void ejbActivate() {} 
  }
  //////////////////client端////////////// 
  package example4; 
  import java.sql.*; 
  import javax.sql.*; 
  import sun.jdbc.rowset.*; 
  import javax.naming.*; 
  import javax.ejb.*; 
  import javax.rmi.*;
  class CoffeesClient { 
  public static void main(String[] args) { 
  
  try { 
  // init the bean 
  Context ctx = new InitialContext(); 
  Object obj = ctx.lookup("ejb/Coffees"); 
  CoffeesHome coffeesHome = (CoffeesHome) 
  PortableRemoteObject.narrow(obj, CoffeesHome.class); 
  Coffees coffees = coffeesHome.create(); 
  
  // get the rowset from the bean 
  CachedRowSet rset = (CachedRowSet)coffees.getCoffees(); 
  
  // find the Columbian coffee 
  while (rset.next()) { 
  String coffeeName = rset.getString("COF_NAME"); 
  if (coffeeName.equalsIgnoreCase(new String("Columbian"))) { 
  // columbian coffee has gone up 10% 
  rset.updateFloat("PRICE", 
  (float)(rset.getFloat("PRICE") * 1.10)); 
  rset.updateRow(); 
  } 
  } 
  // finally send the updated back to the bean... 
  System.out.println("Calling update method"); 
  coffees.updateCoffees((RowSet)rset); 
  } 
  catch (Exception e) { 
  System.out.println(e.getMessage()); 
  } 
  } 
  } 
  例子很简单就不多讲了。 
  cheers. 
  Robin 
        (责任编辑:包春林)


[ 发表评论 ] 字体[  ] [ 打印 ] [ 进入博客 ] [ 进入论坛 ]  [ 推荐给朋友 ]
  相关文章
· J2SE综合--讨论Vector遍历的通用实现 (12-27) · JSP/Servlet:tomcat深入内部结构描述 (12-27)
· 用 JSP 定制标签创建超连接的方法(一) (12-27) · 用JSP定制标签创建超连接的方法(二) (12-27)
· J2EE综合:关于Config参数和Context参数 (12-27) · 用JSTL标签访问list且判断list的选中项 (12-27)
· Java入门:初学者因该了解内存优化编程 (12-27) · Java语言深入--关于java的输入输出 (12-27)
· 使用Annotation设计持久层 (12-27) · 程序人生--一个程序员应该注意的问题 (12-26)
  客户需求反馈表
* 姓  名:
更多资料  了解方案  认识厂商
* 单位名称:
* 联系电话:
* 电子邮件:
  赛迪推荐  
  手机·资费 ·新品·导购·评测·手机资费·宽带
手机搜索  诺基亚 N73 MOTO Z6
  IT产品 ·笔记本·台式机·服务器·打印·投影
IT产品搜索 
  IT技术 ·开发·网管·安全·数据库·操作系统
  信息化 ·热点·专题·访谈·周刊·方案案例
· 回顾ERP市场发展经历 提醒CIO选型需理性
· 网银系统拥堵 SOA从治病良方到罪魁祸首
· 国产ITIL运维先行者 四大厂商角力BI市场
· 金融行业GSN专题解决方案 企业网解决方案
  IT博客 ·曾剑秋·项立刚·Java学习·网管
  IT技术论坛 ·开发·网管·安全·数据库·系统