有两种方法
用vector:
> public Enumeration ejbFindBigAccounts(double balanceGreaterThan) {
> log(ejbFindBigAccounts (balance > + balanceGreaterThan + ));
> Connection con = null;
> PreparedStatement ps = null;
>
> try {
> con = getConnection();
> ps = conprepareStatement(select id from ejbAccounts where bal > ?);
> pssetDouble( balanceGreaterThan);
> psexecuteQuery();
> ResultSet rs = psgetResultSet();
> Vector v = new Vector();
> String pk;
> while (rsnext()) {
> pk = rsgetString();
> vaddElement(pk);
> }
> return velements();
> } catch (SQLException sqe) {
> log(SQLException: + sqe);
> throw new EJBException (sqe);
> } finally {
> cleanup(con ps);
> }
> }
RowSet
RowSet tutorial chapter
rowset是个interface需要有东西去实现它sun的规范中给了三个classcachedrowsetjdbcrowsetwebrowset如果去查jdk doc和jskee有rowset却没有那三个class一般的开发工具(至少我的wsad)中也是这样所以需要下jdbc opt-pack:
解包得到rowsetjar放在哪随您的意别丢了就行
在您的开发工具中增加一个路径如ROWSET_PATH对应d:\jdk\jre\rowsetjar(和的路径对应就行)
右键您的工程文件出现property(大多数工具应该都有吧)加上rowset_path
在您的源文件中import sunjdbcrowset*;
应该说rowset(其实主要是CachedRowSet)真的是个好东西和ms ado的resultset和borland的tclientset非常相似最大的好处是Cache功能!
package example;
import javasql*;
import javaxsql*;
import sunjdbcrowset*;
import javaxnaming*;
import javaxejb*;
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)ctxlookup(jdbc/CoffeesDB);
}
catch (Exception e) {
Systemoutprintln(egetMessage());
throw new CreateException();
}
}
public RowSet getCoffees() throws SQLException {
Connection con = null;
ResultSet rs;
CachedRowSet crs;
try {
con = dsgetConnection(webCustomer webPassword);
Statement stmt = concreateStatement();
rs = stmtexecuteQuery(select * from coffees);
crs = new CachedRowSet();
crspopulate(rs);
// the writer needs this because JDBC drivers
// dont provide this metadata
crssetTableName(coffees);
rsclose();
stmtclose();
} finally {
if (con != null)
conclose();
}
return rset;
}
public updateCoffees(RowSet rs) throws SQLException {
Connection con = null;
try {
CachedRowSet crs = (CachedRowSet)rs;
con = dsgetConnection(webCustomer webPassword);
moves the changes back to the database
crsacceptChanges(con);
} finally {
if (con != null)
conclose();
}
}
//
// Methods inherited from SessionBean
//
public void setSessionContext(SessionContext sc) {
thissc = sc;
}
public void ejbRemove() {}
public void ejbPassivate() {}
public void ejbActivate() {}
}
//////////////////client端//////////////
package example;
import javasql*;
import javaxsql*;
import sunjdbcrowset*;
import javaxnaming*;
import javaxejb*;
import javaxrmi*;
class CoffeesClient {
public static void main(String[] args) {
try {
// init the bean
Context ctx = new InitialContext();
Object obj = ctxlookup(ejb/Coffees);
CoffeesHome coffeesHome = (CoffeesHome)
PortableRemoteObjectnarrow(obj CoffeesHomeclass);
Coffees coffees = coffeesHomecreate();
// get the rowset from the bean
CachedRowSet rset = (CachedRowSet)coffeesgetCoffees();
// find the Columbian coffee
while (rsetnext()) {
String coffeeName = rsetgetString(COF_NAME);
if (coffeeNameequalsIgnoreCase(new String(Columbian))) {
// columbian coffee has gone up %
rsetupdateFloat(PRICE
(float)(rsetgetFloat(PRICE) * ));
rsetupdateRow();
}
}
// finally send the updated back to the bean
Systemoutprintln(Calling update method);
coffeesupdateCoffees((RowSet)rset);
}
catch (Exception e) {
Systemoutprintln(egetMessage());
}
}
}