登录社区:用户名: 密码: 忘记密码 网页功能:加入收藏 设为首页 网站搜索  

文档

下载

图书

论坛

安全

源码

硬件

游戏
首页 信息 空间 VB VC Delphi Java Flash 补丁 控件 安全 黑客 电子书 笔记本 手机 MP3 杀毒 QQ群 产品库 分类信息 编程网站
  立华软件园 - 安全技术中心 - 技术文档 - JAVA 技术文章 | 相关下载 | 电子图书 | 攻防录像 | 安全网站 | 在线论坛 | QQ群组 | 搜索   
 安全技术技术文档
  · 安全配制
  · 工具介绍
  · 黑客教学
  · 防火墙
  · 漏洞分析
  · 破解专题
  · 黑客编程
  · 入侵检测
 安全技术工具下载
  · 扫描工具
  · 攻击程序
  · 后门木马
  · 拒绝服务
  · 口令破解
  · 代理程序
  · 防火墙
  · 加密解密
  · 入侵检测
  · 攻防演示
 安全技术论坛
  · 安全配制
  · 工具介绍
  · 防火墙
  · 黑客入侵
  · 漏洞检测
  · 破解方法
 其他安全技术资源
  · 攻防演示动画
  · 电子图书
  · QQ群组讨论区
  · 其他网站资源
最新招聘信息

关于EJB查询返回值的解决方法
发表日期:2004-07-30作者:[转贴] 出处:  

多少天来,为此问题寝食难安,我曾发誓若我能解决这个问题就要把它贴满各大BBS,如今得偿所愿。
csdn的java版还需努力,最好分一下,如:javaunion
相信很多人都有如此之困惑,得此解决方法不敢独享,公之于众,以利后来人。
声明:此方法的至于彭璐大侠,彭大侠可能不常上网,这麽好的方法也不告诉我等之小虾米,只好代劳了,彭大侠的email不便公开,应该是金蝶的人吧。
好了,不废话了,有两种方法:
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.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.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
12/27/2001

Any question mailto:myvrml@263.net

 


我来说两句】 【发送给朋友】 【加入收藏】 【返加顶部】 【打印本页】 【关闭窗口
中搜索 关于EJB查询返回值的解决方法

 ■ [欢迎对本文发表评论]
用  户:  匿名发出:
您要为您所发的言论的后果负责,故请各位遵纪守法并注意语言文明。

最新招聘信息

关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放 / 友情链接  
Copyright ©2001-2006 Lihuasoft.net webmaster(at)lihuasoft.net
网站编程QQ群   京ICP备05001064号 页面生成时间:0.00193