Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > 分布式数据库客户端数据集的选用
【标  题】:分布式数据库客户端数据集的选用
【关键字】:数据库,客户端,分布式
【来  源】:网络

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

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

有两种方法:
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/

下下来了再怎么办呢?
装呗!
怎么装呢?
setup呀!
没有呀?
啊,没setup呀,sun干什么吃的,连setup都不做个,也太懒了吧。
/////////////////////////////////
哎,我们确实是都被ms惯坏了,看到只有jar,没setup就没辙了,大家好好想想,java最大的特性是什么,就是它的类库
可以自由扩充呀,现在明白该怎么做了吧:

1、解包,得到rowset.jar,放在哪随您的意,别丢了就行。
2、在您的开发工具中增加一个路径,如:ROWSET_PATH对应:d:\jdk1.4\jre\rowset.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());
}
}
}
例子很简单就不多讲了。

出处 www.5xsoft.com
如何用JDO开发数据库应用(1):【上一篇】
数据库中数据项变化不定,如何设计Java Beans(4):【下一篇】
【相关文章】
  • 如何用JDO开发数据库应用(5)
  • 如何用JDO开发数据库应用(6)
  • 如何用JDO开发数据库应用(7)
  • 如何用JDO开发数据库应用(8)
  • 如何用JDO开发数据库应用(9)
  • 如何用JDO开发数据库应用(10)
  • 如何用JDO开发数据库应用(11)
  • 数据库BEAN:RESIN连接池
  • 应用JDOM处理数据库到XML转换JSP实现(1)
  • 应用JDOM处理数据库到XML转换JSP实现(2)
  • 【随机文章】
  • 技术支持程序员程序书写规范
  • 我的软件10年:需求的变化就是创新的机会[转]
  • linux的的可写不可删除(SAMBA+POSIX ACL)
  • 信息周刊:拨开SOA迷雾
  • 12个最重要的J2EE最佳实践(1)
  • 在DocX中写重载函数的帮助
  • sniffit的安装使用简述(转)
  • 彻底掌握erase函数
  • Linux Device Drivers: Building and Running Modules
  • ButtonColumn
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.