Workaround about EJB query return value

zhaozj2021-02-17  54

How many days, to sleep for this problem, I have vowed if I can solve this problem, I have to put it all the big BBS, and now I will pay the wish. 9CBS Java version still needs to work hard, it is best to take it, such as: Javaunion believes that many people have such confused, with this solution does not dare to enjoy exclusive, public, soon. Disclaimer: This method is as for Peng Wei, Peng Daxia may not be online. Such a good method does not tell me such a small shrimp, I have to work, Peng Taxia's Email is inconvenient, it should be a person who is Kingdee. Ok, no nonsense, there are two ways: 1, use vector: / **> * Finds all ejbeans with a balance greater trimary berth.> * 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 = ps.getResultSet ();> vector v = new vector ();> string pk;> while (rs.next )) {> Pk = rs.getstring (1);> v.addelement (pk);>}> Return v.eferences ();>} catch ("sqlexception:" sqe);> throw new ejbexception (SQE);>} finally {> cleanup (con);>}>} Conclusion: Unhappy, inconvenient.

2, RowsetrowSet Tutorial Chapter: http: //developer.java.sun.com/developer/books/jdbctutorial/chapter/book/jdbctutorial/chapter/books/jdbctutorial/chapter/Books/jdbctutorial/chapter is an interface, you need something to implement it, three Class: CachedRowSet, jdbcrowset WebrowSet, if you go to check JDK1.4 DOC and J2Skee1.2, there is rowset, but there is no three Class, the general development tool (at least my WSAD) is like this, so you need JDBC2.0 OPT-PACK: http : //Developer.java.sun.com/developer/earlyaccess/crs/

What should I do now? Put! How to put it? Setup! No? Ah, there is no setup, Sun does it, even setup doesn't do it, it is too lazy. ///////////////////////////////// hey, we really were spoiled ms, only to see the jar, No setup, there is nothing. Everyone thinks, what is the biggest feature of Java is what it is free to expand, now I understand what to do:

1, unpack, get rowset.jar, put it on, don't lose it. 2, add a path in your development tool, such as: rowset_path corresponds to: d: /jdk1.4/jre/rowset.jar (and 1 path). 3. Right-click your project file, appear: Property (most tools should be there), plus rowset_path. 4, in your source file: import sun.jdbc.rowset. *;

OK, get it! Look at you below. (Of course, you can also press ROWSET to JRE) should be said that RowSet (in fact, CachedRowset) is really a good thing, and MS ADO's ResultSet and Borland's TclientSet very similar, the biggest benefit is the cache function! Ok, look an example: ////////////////////////////////////////// Package Example4;

Import java.sql. *; import javax.sql. *; import sun.jdbc.rowset. *; import javax.naming. *; import javax.ejb. *; public class coffeesbean imports sessionBean {

Private sessionContext sc = null; private context ctx = null; private cackesbean ()} public void ejbcreate () THROWS CREATEXCEPTION {

Try {ctx = new initialContext (); DS = (DataSource) CTX.lookup ("JDBC / CoffeeSDB");} catch (excetion 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 {ix ( Con! = null) Con. close ();}} 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 () {}}

/////////////////////// 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 et 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) ("Price") * 1.10); Rset.Updaterow ();}}

// finally send the updated back to the bean ... system.out.println; Coffees.UpdateCoffees (ROWSET) RSET);} catch (Exception E) {system.out.println (e . GetMessage ());}}}}} The example is very simple. Cheers.Robin12 / 27/2001 An Questions Mailto: MyVRML@263.net

转载请注明原文地址:https://www.9cbs.com/read-29445.html

New Post(0)