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");
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() {}
// 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());
}
}
}
例子很简单就不多讲了。
cheers.
Robin
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");
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() {}
// 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());
}
}
}
例子很简单就不多讲了。
cheers.
Robin