出自:java开发者 myvrml
相信很多人都有如此之困惑,得此解决方法不敢独享,公之于众,以利后来人。
声明:此方法的至于彭璐大侠,彭大侠可能不常上网,这麽好的方法也不告诉我等之小虾米,只好代劳了,彭大侠的email不便公开,应该是金蝶的人吧。
好了,不废话了,有两种方法:
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
> 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.4jre owset.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
}
// 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
any question mailto:myvrml@263.net
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器