网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.深入浅出Java的访问者模式.
.转:《学不会的JAVA,消不了的忧.
.Java语言基础 二.
.深入了解J2ME的几个重要概念.
.集成WAP和Java 浏览器解决电信难.
.J2ME 2D小游戏入门之周边工具类.
.初探Java类加载机制的奥秘.
.corba学习3--idl到java的映射.
.EJB——Enterprise JavaBeans技术.
.Java 程序初始化过程详解.
.在JAVA EE环境下使用Kodo&n.
.用java压缩文件示例(没有中文问.
.如何使用Java自带的正则表达式.
.编写跨平台Java程序注意事项.
.JBuilder2005单元测试之业务类介.
.利用反射机制实现XML-RPC.
.J2EE相关设计模式讨论.
.JSWDK环境安装与配置.
.将 DBMS 存储过程封装为会话 EJB.
.Java类基础.

分布式数据库客户端数据集的选用

发表日期:2008-1-5



  有两种方法:
  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());
  }
  }
  }
上一篇:如何封锁您的(或打开别人的)Java 代码 人气:543
下一篇:关于在bean里面打印html的利弊看法 人气:594
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐