KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > jdbcra > JdbcRA2EBRBean


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JdbcRA2EBRBean.java,v 1.2 2004/03/19 11:57:18 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.jdbcra;
27
28 import java.sql.Connection JavaDoc;
29 import java.sql.PreparedStatement JavaDoc;
30 import java.sql.ResultSet JavaDoc;
31 import java.sql.SQLException JavaDoc;
32 import java.sql.Statement JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.Vector JavaDoc;
35
36 import javax.ejb.CreateException JavaDoc;
37 import javax.ejb.EJBException JavaDoc;
38 import javax.ejb.EntityBean JavaDoc;
39 import javax.ejb.EntityContext JavaDoc;
40 import javax.ejb.FinderException JavaDoc;
41 import javax.ejb.ObjectNotFoundException JavaDoc;
42 import javax.ejb.RemoveException JavaDoc;
43 import javax.naming.Context JavaDoc;
44 import javax.naming.InitialContext JavaDoc;
45 import javax.sql.DataSource JavaDoc;
46
47 import org.objectweb.jonas.common.Log;
48 import org.objectweb.util.monolog.api.BasicLevel;
49 import org.objectweb.util.monolog.api.Logger;
50
51 /**
52  *
53  */

54
55 public class JdbcRA2EBRBean implements EntityBean JavaDoc {
56
57     private DataSource JavaDoc dataSource2 = null;
58
59     static private Logger logger = null;
60     EntityContext JavaDoc ejbContext;
61
62     // ------------------------------------------------------------------
63
// State of the bean.
64
//
65
// ------------------------------------------------------------------
66

67     public Integer JavaDoc accno;
68     public String JavaDoc customer;
69     public double balance;
70
71     // ------------------------------------------------------------------
72
// EntityBean implementation
73
// ------------------------------------------------------------------
74

75     public void setEntityContext(EntityContext JavaDoc ctx) {
76
77         if (logger == null) {
78             logger = Log.getLogger("org.objectweb.jonas_tests");
79         }
80         logger.log(BasicLevel.DEBUG, "");
81         ejbContext = ctx;
82     }
83
84     public void unsetEntityContext() {
85         logger.log(BasicLevel.DEBUG, "");
86         ejbContext = null;
87     }
88     
89     public void ejbRemove() throws RemoveException JavaDoc {
90         logger.log(BasicLevel.DEBUG, "");
91
92         // Access database to remove bean in table
93

94         Connection JavaDoc conn = null;
95         PreparedStatement JavaDoc stmt = null;
96
97     try {
98         // get a connection for this transaction context
99

100             conn = getConnection();
101
102         // delete Object in DB
103

104         stmt = conn.prepareStatement("delete from jdbc_xa2 where xa_accno=?");
105         Integer JavaDoc pk = (Integer JavaDoc) ejbContext.getPrimaryKey();
106         stmt.setInt(1, pk.intValue());
107         stmt.executeUpdate();
108     } catch (SQLException JavaDoc e) {
109         throw new RemoveException JavaDoc ("Failed to delete bean from database"+e);
110     } finally {
111         try {
112         if (stmt != null) stmt.close(); // close statement
113
if (conn != null) conn.close(); // release connection
114
} catch (Exception JavaDoc ignore) {
115         }
116     }
117     }
118
119     public void ejbLoad() {
120         logger.log(BasicLevel.DEBUG, "");
121
122         // Access database to load bean state from table
123

124         Connection JavaDoc conn = null;
125         PreparedStatement JavaDoc stmt = null;
126
127     try {
128         // get a connection for this transaction context
129

130             conn = getConnection();
131
132         // find account in DB
133

134         stmt = conn.prepareStatement("select xa_customer,xa_balance from jdbc_xa2 where xa_accno=?");
135         Integer JavaDoc pk = (Integer JavaDoc) ejbContext.getPrimaryKey();
136         stmt.setInt(1, pk.intValue());
137         ResultSet JavaDoc rs = stmt.executeQuery();
138
139             if (rs.next() == false) {
140                 throw new EJBException JavaDoc("Failed to load bean from database");
141             }
142
143         // update object state
144

145         accno = pk;
146         customer = rs.getString("xa_customer");
147         balance = rs.getDouble("xa_balance");
148
149     } catch (SQLException JavaDoc e) {
150         throw new EJBException JavaDoc("Failed to load bean from database "+e);
151     } finally {
152         try {
153         if (stmt != null) stmt.close(); // close statement
154
if (conn != null) conn.close(); // release connection
155
} catch (Exception JavaDoc ignore) {
156       }
157     }
158
159     }
160
161     public void ejbStore() {
162         logger.log(BasicLevel.DEBUG, "");
163
164         // Access database to store bean state in table
165

166         Connection JavaDoc conn = null;
167         PreparedStatement JavaDoc stmt = null;
168
169     try {
170         // get a connection for this transaction context
171

172             conn = getConnection();
173
174         // store Object state in DB
175

176         stmt = conn.prepareStatement("update jdbc_xa2 set xa_customer=?,xa_balance=? where xa_accno=?");
177         stmt.setString(1, customer);
178         stmt.setDouble(2, balance);
179         Integer JavaDoc pk = (Integer JavaDoc) ejbContext.getPrimaryKey();
180         stmt.setInt(3, pk.intValue());
181         stmt.executeUpdate();
182
183     } catch (SQLException JavaDoc e) {
184         throw new EJBException JavaDoc("Failed to store bean to database "+e);
185     } finally {
186         try {
187         if (stmt != null) stmt.close(); // close statement
188
if (conn != null) conn.close(); // release connection
189
} catch (Exception JavaDoc ignore) {
190       }
191     }
192
193     }
194
195     public void ejbPassivate() {
196         logger.log(BasicLevel.DEBUG, "");
197     }
198
199     public void ejbActivate() {
200         logger.log(BasicLevel.DEBUG, "");
201     }
202
203     public void ejbPostCreate(int val_accno, String JavaDoc val_customer, double val_balance){
204         logger.log(BasicLevel.DEBUG, "");
205     }
206
207     public void ejbPostCreate(){
208         logger.log(BasicLevel.DEBUG, "");
209     }
210
211     public java.lang.Integer JavaDoc ejbCreate(int val_accno, String JavaDoc val_customer, double val_balance)
212         throws CreateException JavaDoc {
213
214         logger.log(BasicLevel.DEBUG, "");
215
216         // Init here the bean fields
217
// Init object state
218
accno = new Integer JavaDoc(val_accno);
219     customer = val_customer;
220     balance = val_balance;
221
222     Connection JavaDoc conn = null;
223
224     PreparedStatement JavaDoc stmt = null;
225
226     try {
227         // get a connection for this transaction context
228

229             conn = getConnection();
230
231         // create object in DB
232

233         stmt = conn.prepareStatement("insert into jdbc_xa2 (xa_accno, xa_customer, xa_balance) values (?, ?, ?)");
234         stmt.setInt(1, accno.intValue());
235         stmt.setString(2, customer);
236         stmt.setDouble(3, balance);
237         stmt.executeUpdate();
238     } catch (SQLException JavaDoc e) {
239         throw new CreateException JavaDoc("Failed to create bean in database: "+e);
240     } finally {
241         try {
242         if (stmt != null) stmt.close(); // close statement
243
if (conn != null) conn.close(); // release connection
244
} catch (Exception JavaDoc ignore) {
245       }
246     }
247
248     // Return the primary key
249

250     return accno;
251
252     }
253
254     public java.lang.Integer JavaDoc ejbFindByPrimaryKey(Integer JavaDoc pk)
255         throws ObjectNotFoundException JavaDoc, FinderException JavaDoc {
256         logger.log(BasicLevel.DEBUG, "");
257
258         // Access database to find entry in table
259

260         Connection JavaDoc conn = null;
261         PreparedStatement JavaDoc stmt = null;
262
263     try {
264         // get a connection for this transaction context
265

266             conn = getConnection();
267
268         // lookup for this primary key in DB
269

270         stmt = conn.prepareStatement("select xa_accno from jdbc_xa2 where xa_accno=?");
271         stmt.setInt(1, pk.intValue());
272         ResultSet JavaDoc rs = stmt.executeQuery();
273
274         if (rs.next() == false) {
275         throw new javax.ejb.ObjectNotFoundException JavaDoc();
276         }
277
278     } catch (SQLException JavaDoc e) {
279         throw new javax.ejb.FinderException JavaDoc("Failed to executeQuery " +e);
280     } finally {
281         try {
282         if (stmt != null) stmt.close(); // close statement
283
if (conn != null) conn.close(); // release connection
284
} catch (Exception JavaDoc ignore) {
285       }
286     }
287
288         return pk;
289     }
290
291
292     /**
293      * Find Account by its account number
294      *
295      * @return pk The primary key
296      *
297      * @exception FinderException - Failed to execute the query.
298      * @exception ObjectNotFoundException - Object not found for this account number
299      */

300
301     public Integer JavaDoc ejbFindByNumber(int accno)
302     throws ObjectNotFoundException JavaDoc, FinderException JavaDoc {
303
304     Connection JavaDoc conn = null;
305     PreparedStatement JavaDoc stmt = null;
306
307     try {
308         // get a connection for this transaction context
309

310             conn = getConnection();
311
312         // lookup for this primary key in DB
313

314         stmt = conn.prepareStatement("select xa_accno from jdbc_xa2 where xa_accno=?");
315         stmt.setInt(1, accno);
316         ResultSet JavaDoc rs = stmt.executeQuery();
317
318         if (rs.next() == false) {
319         throw new javax.ejb.ObjectNotFoundException JavaDoc();
320         }
321
322     } catch (SQLException JavaDoc e) {
323         throw new javax.ejb.FinderException JavaDoc("Failed to executeQuery " +e);
324     } finally {
325         try {
326         if (stmt != null) stmt.close(); // close statement
327
if (conn != null) conn.close(); // release connection
328
} catch (Exception JavaDoc ignore) {
329       }
330     }
331
332     // return a primary key for this account
333

334     return new Integer JavaDoc(accno);
335     }
336
337     /**
338      * Creates an enumeration of primary keys for all accounts
339      *
340      * @return pkv The primary keys
341      *
342      * @exception FinderException - Failed to execute the query.
343      */

344
345     public Enumeration JavaDoc ejbFindAllAccounts() throws FinderException JavaDoc {
346
347     Connection JavaDoc conn = null;
348     PreparedStatement JavaDoc stmt = null;
349
350     Vector JavaDoc pkv = new Vector JavaDoc();
351
352     try {
353         // get a connection for this transaction context
354

355             conn = getConnection();
356
357         // Lookup for all accounts in DB
358

359         stmt = conn.prepareStatement("select xa_accno from jdbc_xa2");
360         ResultSet JavaDoc rs = stmt.executeQuery();
361
362         // Build the vector of primary keys
363

364         while (rs.next()) {
365         Integer JavaDoc pk = new Integer JavaDoc(rs.getInt("xa_accno"));
366         pkv.addElement((Object JavaDoc)pk);
367         };
368
369     } catch (SQLException JavaDoc e) {
370         throw new javax.ejb.FinderException JavaDoc("Failed to executeQuery " +e);
371     } finally {
372         try {
373         if (stmt != null) stmt.close(); // close statement
374
if (conn != null) conn.close(); // release connection
375
} catch (Exception JavaDoc ignore) {
376       }
377     }
378
379     // return primary keys
380
return pkv.elements();
381     }
382
383     public java.lang.Integer JavaDoc ejbCreate()
384         throws CreateException JavaDoc{
385
386         logger.log(BasicLevel.DEBUG, "");
387
388     // Init object state
389

390     Connection JavaDoc conn = null;
391     Statement JavaDoc stmt = null;
392         Integer JavaDoc tempint = new Integer JavaDoc(0);
393
394     try {
395         // get a connection for this transaction context
396

397             conn = getConnection();
398
399         // create object in DB
400

401         stmt = conn.createStatement();
402         stmt.addBatch("insert into jdbc_xa2 (xa_accno, xa_customer, xa_balance) values (201, 'Albert Smith', 500)");
403         stmt.addBatch("insert into jdbc_xa2 (xa_accno, xa_customer, xa_balance) values (202, 'Bob Smith', 500)");
404         stmt.addBatch("insert into jdbc_xa2 (xa_accno, xa_customer, xa_balance) values (203, 'Carl Smith', 500)");
405         stmt.addBatch("insert into jdbc_xa2 (xa_accno, xa_customer, xa_balance) values (204, 'David Smith', 500)");
406         stmt.addBatch("insert into jdbc_xa2 (xa_accno, xa_customer, xa_balance) values (205, 'Edward Smith', 500)");
407         int[] upCounts = stmt.executeBatch();
408
409     } catch (SQLException JavaDoc e) {
410         throw new CreateException JavaDoc("Failed to create bean in database: "+e);
411     } finally {
412         try {
413         if (stmt != null) stmt.close(); // close statement
414
if (conn != null) conn.close(); // release connection
415
} catch (Exception JavaDoc ignore) {
416       }
417     }
418         return tempint;
419     }
420
421     /**
422      * @return the connection from the dataSource
423      * @exception EJBException Thrown by the method if the dataSource is not found
424      * in the naming.
425      * @exception SQLException may be thrown by dataSource.getConnection()
426      */

427
428     private Connection JavaDoc getConnection() throws EJBException JavaDoc, SQLException JavaDoc {
429
430         Connection JavaDoc myconn2 = null;
431
432     if (dataSource2 == null) {
433
434         // Finds DataSource from JNDI
435

436         Context JavaDoc initialContext = null;
437
438         try {
439         initialContext = new InitialContext JavaDoc();
440         dataSource2 = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/JdbcRA2Ds");
441         } catch (Exception JavaDoc e) {
442         throw new javax.ejb.EJBException JavaDoc("Cannot lookup dataSource2 "+e);
443         }
444     }
445
446         try {
447              myconn2 = dataSource2.getConnection();
448         } catch (Exception JavaDoc e) {
449              throw new javax.ejb.EJBException JavaDoc("Cannot getConnection dataSource2 "+e);
450         }
451
452     return myconn2;
453     }
454
455     /*========================= Account implementation ============================*/
456
457     /**
458      * Business method for returning the balance.
459      *
460      * @return balance
461      *
462      */

463
464     public double getBalance(){
465     return balance;
466     }
467
468     /**
469      * Business method for updating the balance.
470      *
471      * @param d balance to update
472      *
473      */

474
475     public void setBalance(double d){
476     balance = balance + d;
477     }
478
479     /**
480      * Business method for returning the customer.
481      *
482      * @return customer
483      *
484      */

485
486     public String JavaDoc getCustomer(){
487     return customer;
488     }
489
490     /**
491      * Business method for changing the customer name.
492      *
493      * @param c customer to update
494      *
495      */

496
497     public void setCustomer(String JavaDoc c) {
498     customer = c;
499     }
500
501     /**
502      * Business method to get the Account number
503      */

504
505     public int getNumber() {
506     return accno.intValue();
507     }
508
509 }
510
511
512
513
Popular Tags