熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

分布式數據庫客戶端數據集的選用

2022-06-13   來源: Java核心技術 

  有兩種方法
  用vector:
  > public Enumeration ejbFindBigAccounts(double balanceGreaterThan) {
  > log(ejbFindBigAccounts (balance > + balanceGreaterThan + ));
  > Connection con = null;
  > PreparedStatement ps = null;
  >
  > try {
  > con = getConnection();
  > ps = conprepareStatement(select id from ejbAccounts where bal > ?);
  > pssetDouble( balanceGreaterThan);
  > psexecuteQuery();
  > ResultSet rs = psgetResultSet();
  > Vector v = new Vector();
  > String pk;
  > while (rsnext()) {
  > pk = rsgetString();
  > vaddElement(pk);
  > }
  > return velements();
  > } catch (SQLException sqe) {
  > log(SQLException: + sqe);
  > throw new EJBException (sqe);
  > } finally {
  > cleanup(con ps);
  > }
  > }
  
  RowSet
  RowSet tutorial chapter
  
  
  rowset是個interface需要有東西去實現它sun的規范中給了三個classcachedrowsetjdbcrowsetwebrowset如果去查jdk doc和jskee有rowset卻沒有那三個class一般的開發工具(至少我的wsad)中也是這樣所以需要下jdbc opt-pack:
  
  
  解包得到rowsetjar放在哪隨您的意別丟了就行
  在您的開發工具中增加一個路徑ROWSET_PATH對應d:\jdk\jre\rowsetjar(和的路徑對應就行)
  右鍵您的工程文件出現property(大多數工具應該都有吧)加上rowset_path
  在您的源文件中import sunjdbcrowset*;
  
  應該說rowset(其實主要是CachedRowSet)真的是個好東西和ms ado的resultset和borland的tclientset非常相似最大的好處是Cache功能!
  
  package example;
  
  import javasql*;
  import javaxsql*;
  import sunjdbcrowset*;
  import javaxnaming*;
  import javaxejb*;
  
  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)ctxlookup(jdbc/CoffeesDB);
  }
  catch (Exception e) {
  Systemoutprintln(egetMessage());
  throw new CreateException();
  }
  }
  
  public RowSet getCoffees() throws SQLException {
  
  Connection con = null;
  ResultSet rs;
  CachedRowSet crs;
  
  try {
  con = dsgetConnection(webCustomer webPassword);
  Statement stmt = concreateStatement();
  rs = stmtexecuteQuery(select * from coffees);
  
  crs = new CachedRowSet();
  crspopulate(rs);
  // the writer needs this because JDBC drivers
  // dont provide this metadata
  crssetTableName(coffees);
  
  rsclose();
  stmtclose();
  } finally {
  if (con != null)
  conclose();
  }
  return rset;
  }
  
  public updateCoffees(RowSet rs) throws SQLException {
  
  Connection con = null;
  
  try {
  CachedRowSet crs = (CachedRowSet)rs;
  con = dsgetConnection(webCustomer webPassword);
  
  moves the changes back to the database
  crsacceptChanges(con);
  } finally {
  if (con != null)
  conclose();
  }
  }
  
  //
  // Methods inherited from SessionBean
  //
  
  public void setSessionContext(SessionContext sc) {
  thissc = sc;
  }
  
  public void ejbRemove() {}
  public void ejbPassivate() {}
  public void ejbActivate() {}
  
  
  }
  
  
  //////////////////client端//////////////
  package example;
  
  import javasql*;
  import javaxsql*;
  import sunjdbcrowset*;
  import javaxnaming*;
  import javaxejb*;
  import javaxrmi*;
  
  class CoffeesClient {
  
  public static void main(String[] args) {
  
  try {
  // init the bean
  Context ctx = new InitialContext();
  Object obj = ctxlookup(ejb/Coffees);
  CoffeesHome coffeesHome = (CoffeesHome)
  PortableRemoteObjectnarrow(obj CoffeesHomeclass);
  Coffees coffees = coffeesHomecreate();
  
  // get the rowset from the bean
  CachedRowSet rset = (CachedRowSet)coffeesgetCoffees();
  
  // find the Columbian coffee
  while (rsetnext()) {
  String coffeeName = rsetgetString(COF_NAME);
  if (coffeeNameequalsIgnoreCase(new String(Columbian))) {
  // columbian coffee has gone up %
  rsetupdateFloat(PRICE
  (float)(rsetgetFloat(PRICE) * ));
  rsetupdateRow();
  }
  }
  
  // finally send the updated back to the bean
  Systemoutprintln(Calling update method);
  coffeesupdateCoffees((RowSet)rset);
  }
  catch (Exception e) {
  Systemoutprintln(egetMessage());
  }
  }
  }
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26988.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.