有兩種方法
> public Enumeration ejbFindBigAccounts(double balanceGreaterThan) {
> log(
> Connection con = null;
> PreparedStatement ps = null;
>
> try {
> con = getConnection();
> ps = con
> ps
> ps
> ResultSet rs = ps
> Vector v = new Vector();
> String pk;
> while (rs
> pk = rs
> v
> }
> return v
> } catch (SQLException sqe) {
> log(
> throw new EJBException (sqe);
> } finally {
> cleanup(con
> }
> }
RowSet tutorial chapter
rowset是個interface
應該說rowset(其實主要是CachedRowSet)真的是個好東西
package example
import java
import javax
import sun
import javax
import javax
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
}
catch (Exception e) {
System
throw new CreateException();
}
}
public RowSet getCoffees() throws SQLException {
Connection con = null;
ResultSet rs;
CachedRowSet crs;
try {
con = ds
Statement stmt = con
rs = stmt
crs = new CachedRowSet();
crs
// the writer needs this because JDBC drivers
// don
crs
rs
stmt
} finally {
if (con != null)
con
}
return rset;
}
public updateCoffees(RowSet rs) throws SQLException {
Connection con = null;
try {
CachedRowSet crs = (CachedRowSet)rs;
con = ds
moves the changes back to the database
crs
} finally {
if (con != null)
con
}
}
//
// Methods inherited from SessionBean
//
public void setSessionContext(SessionContext sc) {
this
}
public void ejbRemove() {}
public void ejbPassivate() {}
public void ejbActivate() {}
}
//////////////////client端//////////////
package example
import java
import javax
import sun
import javax
import javax
import javax
class CoffeesClient {
public static void main(String[] args) {
try {
// init the bean
Context ctx = new InitialContext();
Object obj = ctx
CoffeesHome coffeesHome = (CoffeesHome)
PortableRemoteObject
Coffees coffees = coffeesHome
// get the rowset from the bean
CachedRowSet rset = (CachedRowSet)coffees
// find the Columbian coffee
while (rset
String coffeeName = rset
if (coffeeName
// columbian coffee has gone up
rset
(float)(rset
rset
}
}
// finally send the updated back to the bean
System
coffees
}
catch (Exception e) {
System
}
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26988.html