KickJava   Java API By Example, From Geeks To Geeks.

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


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: JdbcRA1EBRBean.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 public class JdbcRA1EBRBean implements EntityBean JavaDoc {
55
56     private DataSource JavaDoc dataSource1 = null;
57
58     static private Logger logger = null;
59     EntityContext JavaDoc ejbContext;
60
61     // ------------------------------------------------------------------
62
// State of the bean.
63
//
64
// ------------------------------------------------------------------
65

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

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

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

98             conn = getConnection();
99
100         // delete Object in DB
101

102         stmt = conn.prepareStatement("delete from jdbc_xa1 where xa_accno=?");
103         Integer JavaDoc pk = (Integer JavaDoc) ejbContext.getPrimaryKey();
104         stmt.setInt(1, pk.intValue());
105         stmt.executeUpdate();
106
107     } catch (SQLException JavaDoc e) {
108         throw new RemoveException JavaDoc ("Failed to delete bean from database " +e);
109     } finally {
110         try {
111         if (stmt != null) stmt.close(); // close statement
112
if (conn != null) conn.close(); // release connection
113
} catch (Exception JavaDoc ignore) {
114         }
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_xa1 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     public void ejbStore() {
161         logger.log(BasicLevel.DEBUG, "");
162
163         // Access database to store bean state in table
164

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

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

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

217     accno = new Integer JavaDoc(val_accno);
218     customer = val_customer;
219     balance = val_balance;
220
221     Connection JavaDoc conn = null;
222     PreparedStatement JavaDoc stmt = null;
223
224     try {
225         // get a connection for this transaction context
226

227             conn = getConnection();
228
229         // create object in DB
230

231         stmt = conn.prepareStatement("insert into jdbc_xa1 (xa_accno, xa_customer, xa_balance) values (?, ?, ?)");
232         stmt.setInt(1, accno.intValue());
233         stmt.setString(2, customer);
234         stmt.setDouble(3, balance);
235         stmt.executeUpdate();
236
237     } catch (SQLException JavaDoc e) {
238         throw new CreateException JavaDoc("Failed to create bean in database: "+e);
239     } finally {
240         try {
241         if (stmt != null) stmt.close(); // close statement
242
if (conn != null) conn.close(); // release connection
243
} catch (Exception JavaDoc ignore) {
244       }
245     }
246
247     // Return the primary key
248
return accno;
249
250     }
251
252     public java.lang.Integer JavaDoc ejbFindByPrimaryKey(Integer JavaDoc pk)
253         throws ObjectNotFoundException JavaDoc, FinderException JavaDoc {
254         logger.log(BasicLevel.DEBUG, "");
255
256         // Access database to find entry in table
257

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

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

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

307             conn = getConnection();
308
309         // lookup for this primary key in DB
310

311         stmt = conn.prepareStatement("select xa_accno from jdbc_xa1 where xa_accno=?");
312         stmt.setInt(1, accno);
313         ResultSet JavaDoc rs = stmt.executeQuery();
314
315         if (rs.next() == false) {
316         throw new javax.ejb.ObjectNotFoundException JavaDoc();
317         }
318
319     } catch (SQLException JavaDoc e) {
320         throw new javax.ejb.FinderException JavaDoc("Failed to executeQuery " +e);
321     } finally {
322         try {
323         if (stmt != null) stmt.close(); // close statement
324
if (conn != null) conn.close(); // release connection
325
} catch (Exception JavaDoc ignore) {
326       }
327     }
328
329     // return a primary key for this account
330
return new Integer JavaDoc(accno);
331     }
332
333     /**
334      * Creates an enumeration of primary keys for all accounts
335      *
336      * @return pkv The primary keys
337      *
338      * @exception FinderException - Failed to execute the query.
339      */

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

351             conn = getConnection();
352
353         // Lookup for all accounts in DB
354

355         stmt = conn.prepareStatement("select xa_accno from jdbc_xa1");
356         ResultSet JavaDoc rs = stmt.executeQuery();
357
358         // Build the vector of primary keys
359

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

393             conn = getConnection();
394
395         // create object in DB
396

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

423
424     private Connection JavaDoc getConnection() throws EJBException JavaDoc, SQLException JavaDoc {
425
426         Connection JavaDoc myconn1 = null;
427
428     if (dataSource1 == null) {
429
430         // Finds DataSource from JNDI
431

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

459
460     public double getBalance(){
461     return balance;
462     }
463
464     /**
465      * Business method for updating the balance.
466      *
467      * @param d balance to update
468      *
469      */

470
471     public void setBalance(double d){
472     balance = balance + d;
473     }
474
475     /**
476      * Business method for returning the customer.
477      *
478      * @return customer
479      *
480      */

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

492
493     public void setCustomer(String JavaDoc c) {
494     customer = c;
495     }
496
497     /**
498      * Business method to get the Account number
499      */

500
501     public int getNumber() {
502     return accno.intValue();
503     }
504
505 }
506
507
508
509
Popular Tags