国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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

2019-11-18 12:05:18
字體:
來源:轉載
供稿:網友

  有兩種方法:
  1、用vector:
  > 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/
  
  1、解包,得到rowset.jar,放在哪隨您的意,別丟了就行。
  2、在您的開發工具中增加一個路徑,如:ROWSET_PATH對應:d:/jdk1.4/jre/rowset.jar(和1的路徑對應就行)。
  3、右鍵您的工程文件,出現:property(大多數工具應該都有吧),加上rowset_path。
  4、在您的源文件中:import sun.jdbc.rowset.*;
  
  應該說rowset(其實主要是CachedRowSet)真的是個好東西,和ms ado的resultset和borland的tclientset非常相似,最大的好處是Cache功能!
  
  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());
  }
  }
  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新兴县| 商都县| 凤庆县| 广州市| 友谊县| 河北省| 安吉县| 六枝特区| 万源市| 乳山市| 通榆县| 武冈市| 保靖县| 丁青县| 滦平县| 方山县| 保定市| 凭祥市| 牡丹江市| 姜堰市| 永丰县| 铁力市| 克什克腾旗| 南充市| 巢湖市| 崇义县| 鹤庆县| 阜平县| 南汇区| 娄底市| 西华县| 平远县| 虞城县| 古丈县| 宜黄县| 馆陶县| 防城港市| 新密市| 囊谦县| 南丰县| 明溪县|