中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > 综合文章
关于EJB返回值的实用解决方案
作者:myvrml 时间:2006-09-18 08:25 出处:java开发者 责编:月夜寒箫
              摘要:关于EJB返回值的实用解决方案
 

 

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
            > 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/

下面:

1、解包,得到rowset.jar,放在哪随您的意,别丢了就行。

2、在您的开发工具中增加一个路径,如:ROWSET_PATH对应:d:jdk1.4jre owset.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
            }
            // 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

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有