溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

EJB查詢返回值的解決方法是什么呢

發(fā)布時間:2021-12-08 11:46:25 來源:億速云 閱讀:111 作者:柒染 欄目:編程語言

這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)EJB查詢返回值的解決方法是什么呢,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

關(guān)于EJB查詢返回值的解決方法 

多少天來,為此問題寢食難安,我曾發(fā)誓若我能解決這個問題就要把它貼滿各大BBS,如今得償所愿。
csdn的Java版還需努力,最好分一下,如:javaunion
相信很多人都有如此之困惑,得此解決方法不敢獨享,公之于眾,以利后來人。
聲明:此方法的至于彭璐大俠,彭大俠可能不常上網(wǎng),這麼好的方法也不告訴我等之小蝦米,只好代勞了,彭大俠的eMail不便公開,應(yīng)該是金蝶的人吧。
好了,不廢話了,有兩種方法:
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);
>  }
>  }
結(jié)論:不爽,不方便。

2、RowSet
RowSet tutorial chapter :
JDBCTutorial/chapter5.html">http://developer.java.sun.com/developer/Books/JDBCTutorial/chapter5.html

rowset是個interface,需要有東西去實現(xiàn)它,sun的規(guī)范中給了三個class:cachedrowset,jdbcrowset,webrowset,如果去查jdk1.4 doc和j2skee1.2,有rowset,卻沒有那三個class,一般的開發(fā)工具(至少我的wsad)中也是這樣,所以需要下jdbc2.0 opt-pack:
http://developer.java.sun.com/developer/earlyAccess/crs/

下下來了再怎么辦呢?
裝唄!
怎么裝呢?
setup呀!
沒有呀?
啊,沒setup呀,sun干什么吃的,連setup都不做個,也太懶了吧。
/////////////////////////////////
哎,我們確實是都被ms慣壞了,看到只有jar,沒setup就沒轍了,大家好好想想,java最大的特性是什么,就是它的類庫可以自由擴充呀,現(xiàn)在明白該怎么做了吧:

1、解包,得到rowset.jar,放在哪隨您的意,別丟了就行。
2、在您的開發(fā)工具中增加一個路徑,如:ROWSET_PATH對應(yīng):d:jdk1.4jrerowset.jar(和1的路徑對應(yīng)就行)。
3、右鍵您的工程文件,出現(xiàn):property(大多數(shù)工具應(yīng)該都有吧),加上rowset_path。
4、在您的源文件中:import sun.jdbc.rowset.*;

OK,搞定!下面就看您的了。(當然也可以把rowset壓到j(luò)re里去)
應(yīng)該說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 .NET">mailto:myvrml@263.net

上述就是小編為大家分享的EJB查詢返回值的解決方法是什么呢了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

ejb
AI