CTOCIO IT专家网

天极传媒 比特网 | 天极网 | IT专家网 | IT商网 | 52PK游戏网 | 手机天极 | IT分众 |
IT专家网搜索

您现在的位置: IT专家网 > Web服务子站 > Java技术

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

作者: 佚名,  出处:IT专家网论坛, 责任编辑: 包春林, 
2008-05-13 04:00
  相信很多人都有如此之困惑,得此解决方法不敢独享,公之于众,以利后来人。

  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:

  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

共2页。 9 1 2

网友评论

笔名 
请您注意:遵守国家有关法律、法规,尊重网上道德,承担一切因您的行为而直接或间接引起的法律责任。    IT专家网友拥有管理笔名和留言的一切权利。
  • 周排行榜
  • 月排行榜

邮件订阅

       
天极服务 | 关于我们 | 网站律师 | 加入我们 | 联系我们 | 广告业务 | 友情链接 | 我要挑错
All Rights Reserved, Copyright 2004-2008, Ctocio.com.cn
渝ICP证B2-20030003号 如有意见请与我们联系 powered by 天极内容管理平台CMS4i