有两种方法:
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());
}
}
}
闽公网安备 35060202000074号