Source Code Cross Referenced for AccountEB.java in  » J2EE » JOnAS-4.8.6 » org » objectweb » jonas » jtests » beans » beanexc » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » JOnAS 4.8.6 » org.objectweb.jonas.jtests.beans.beanexc 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * JOnAS: Java(TM) Open Application Server
003:         * Copyright (C) 1999 Bull S.A.
004:         * Contact: jonas-team@objectweb.org
005:         * 
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2.1 of the License, or any later version.
010:         * 
011:         * This library is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         * Lesser General Public License for more details.
015:         * 
016:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
019:         * USA
020:         *
021:         * --------------------------------------------------------------------------
022:         * $Id: AccountEB.java 4371 2004-03-15 16:04:22Z durieuxp $
023:         * --------------------------------------------------------------------------
024:         */
025:
026:        package org.objectweb.jonas.jtests.beans.beanexc;
027:
028:        import java.sql.Connection;
029:        import java.sql.PreparedStatement;
030:        import java.sql.ResultSet;
031:        import java.sql.SQLException;
032:        import java.util.Enumeration;
033:        import java.util.Vector;
034:
035:        import javax.ejb.EntityBean;
036:        import javax.ejb.FinderException;
037:        import javax.ejb.ObjectNotFoundException;
038:        import javax.ejb.RemoveException;
039:        import javax.naming.Context;
040:        import javax.naming.InitialContext;
041:        import javax.sql.DataSource;
042:
043:        import org.objectweb.util.monolog.api.BasicLevel;
044:
045:        /**
046:         * This is an entity bean with "bean managed persistence".
047:         * The state of an instance is stored into a relational database.
048:         * The table must exist.
049:         * @author Philippe Durieux, Philippe Coq
050:         */
051:        public class AccountEB extends AccountEC implements  EntityBean {
052:
053:            static final String tableName = "beanexcAccountEB";
054:
055:            // Database related information
056:            private DataSource dataSource = null;
057:            private static String dataSourceName;
058:
059:            public void ejbLoad() {
060:                logger.log(BasicLevel.DEBUG, "");
061:                AccountPK pk = (AccountPK) entityContext.getPrimaryKey();
062:                PreparedStatement loadPstmt = null;
063:                Connection conn = null;
064:                try {
065:                    conn = getConnection();
066:                    loadPstmt = conn
067:                            .prepareStatement("select c_number, c_customer, c_balance  from "
068:                                    + tableName + " where c_number=?");
069:
070:                    // PK fields
071:                    loadPstmt.setInt(1, pk.number);
072:                    ResultSet rs = loadPstmt.executeQuery();
073:                    if (!rs.next()) {
074:                        System.err.println("Fails to load bean from database");
075:                        throw new javax.ejb.EJBException(
076:                                "Failed to load bean from database");
077:                    }
078:                    number = rs.getInt("c_number");
079:                    customer = rs.getString("c_customer");
080:                    balance = rs.getLong("c_balance");
081:                } catch (SQLException e) {
082:                    System.err.println("Fails to load bean from database");
083:                    throw new javax.ejb.EJBException(
084:                            "Failed to load bean from database" + e);
085:                } finally {
086:                    try {
087:                        loadPstmt.close();
088:                        conn.close();
089:                    } catch (Exception e) {
090:                        System.err.println("ejbLoad: Ignore exception:" + e);
091:                    }
092:                }
093:
094:            }
095:
096:            public void ejbStore() {
097:                AccountPK pk = (AccountPK) entityContext.getPrimaryKey();
098:                logger.log(BasicLevel.DEBUG, "pk=" + pk.number);
099:                PreparedStatement storePstmt = null;
100:                Connection conn = null;
101:                if (forceToFailEjbStore) {
102:                    forceToFailEjbStore = false;
103:                    throw new javax.ejb.EJBException(
104:                            "Failed to store bean to database");
105:                }
106:
107:                try {
108:                    conn = getConnection();
109:                    storePstmt = conn.prepareStatement("update " + tableName
110:                            + " set c_customer=?,c_balance=? where c_number=?");
111:
112:                    // non PK fields
113:                    if (customer == null) {
114:                        storePstmt.setNull(1, java.sql.Types.VARCHAR);
115:                    } else {
116:                        storePstmt.setString(1, customer);
117:                    }
118:                    storePstmt.setLong(2, balance);
119:                    // PK fields
120:                    storePstmt.setInt(3, number);
121:                    storePstmt.executeUpdate();
122:                } catch (SQLException e) {
123:                    throw new javax.ejb.EJBException(
124:                            "Failed to store bean to database");
125:                } finally {
126:                    try {
127:                        storePstmt.close();
128:                        conn.close();
129:                    } catch (Exception e) {
130:                        System.err.println("ejbStore: Ignore exception:" + e);
131:                    }
132:                }
133:            }
134:
135:            public void ejbRemove() throws RemoveException {
136:                logger.log(BasicLevel.DEBUG, "");
137:
138:                // A RemoveException may be throwned depending on the PK value
139:                super .ejbRemove();
140:
141:                AccountPK pk = (AccountPK) entityContext.getPrimaryKey();
142:                PreparedStatement removePstmt = null;
143:                Connection conn = null;
144:                try {
145:                    conn = getConnection();
146:                    removePstmt = conn.prepareStatement("delete from "
147:                            + tableName + " where c_number=?");
148:                    // PK fields
149:                    removePstmt.setInt(1, pk.number);
150:                    removePstmt.executeUpdate();
151:                } catch (SQLException e) {
152:                    throw new javax.ejb.EJBException(
153:                            "Failed to delete bean from database");
154:                } finally {
155:                    try {
156:                        removePstmt.close();
157:                        conn.close();
158:                    } catch (Exception e) {
159:                        System.err.println("ejbRemove: Ignore exception:" + e);
160:                    }
161:                }
162:            }
163:
164:            public AccountPK ejbCreate(int val_number, String val_customer,
165:                    long val_balance) throws javax.ejb.CreateException {
166:                logger.log(BasicLevel.DEBUG, "");
167:
168:                super .ejbCreate(val_number, val_customer, val_balance);
169:
170:                PreparedStatement createPstmt = null;
171:                Connection conn = null;
172:                AccountPK pk = new AccountPK(val_number);
173:
174:                // persistence management
175:                try {
176:                    conn = getConnection();
177:                    createPstmt = conn.prepareStatement("insert into "
178:                            + tableName + " values (?, ?, ?)");
179:                    // all fields
180:                    createPstmt.setInt(1, pk.number);
181:                    if (customer == null) {
182:                        createPstmt.setNull(2, java.sql.Types.VARCHAR);
183:                    } else {
184:                        createPstmt.setString(2, customer);
185:                    }
186:                    createPstmt.setLong(3, balance);
187:                    createPstmt.executeUpdate();
188:                } catch (SQLException e) {
189:                    throw new javax.ejb.EJBException(
190:                            "Failed to create bean in database" + e);
191:                } finally {
192:                    try {
193:                        createPstmt.close();
194:                        conn.close();
195:                    } catch (Exception e) {
196:                        logger.log(BasicLevel.ERROR,
197:                                "ejbCreate: Ignore exception:" + e);
198:                    }
199:                }
200:                return pk;
201:            }
202:
203:            /**
204:             * creer une instance bidon le flag sert pour savoir quelle exception lever
205:             */
206:            public AccountPK ejbCreate(int flag)
207:                    throws javax.ejb.CreateException, AppException {
208:                logger.log(BasicLevel.DEBUG, "");
209:                AccountPK pk = super .ejbCreate(flag);
210:                return pk;
211:            }
212:
213:            public AccountPK ejbCreate(boolean flag)
214:                    throws javax.ejb.CreateException, AppException {
215:                logger.log(BasicLevel.DEBUG, "");
216:                AccountPK pk = super .ejbCreate(flag);
217:                return pk;
218:            }
219:
220:            public void ejbPostCreate(int val_number, String val_customer,
221:                    long val_balance) {
222:                logger.log(BasicLevel.DEBUG, "");
223:            }
224:
225:            public AccountPK ejbFindByPrimaryKey(AccountPK pk)
226:                    throws ObjectNotFoundException, FinderException {
227:                logger.log(BasicLevel.DEBUG, "");
228:                PreparedStatement loadPstmt = null;
229:                Connection conn = null;
230:                try {
231:                    conn = getConnection();
232:                    loadPstmt = conn
233:                            .prepareStatement("select c_customer, c_balance from "
234:                                    + tableName + " where c_number=?");
235:                    loadPstmt.setInt(1, pk.number);
236:                    ResultSet rs = loadPstmt.executeQuery();
237:                    if (!rs.next()) {
238:                        throw new javax.ejb.ObjectNotFoundException(
239:                                "primary key");
240:                    }
241:                } catch (SQLException e) {
242:                    System.err.println("Fails to executeQuery " + e);
243:                    throw new javax.ejb.FinderException(
244:                            "Failed to executeQuery " + e);
245:                } finally {
246:                    try {
247:                        loadPstmt.close();
248:                        conn.close();
249:                    } catch (Exception e) {
250:                        logger.log(BasicLevel.ERROR,
251:                                "ejbFindByPrimaryKey: Ignore exception:" + e);
252:                    }
253:                }
254:                return pk;
255:            }
256:
257:            public AccountPK ejbFindByNoAccount(int number)
258:                    throws ObjectNotFoundException, FinderException {
259:                logger.log(BasicLevel.DEBUG, "");
260:                PreparedStatement loadPstmt = null;
261:                Connection conn = null;
262:                AccountPK pk = new AccountPK(number);
263:                try {
264:                    conn = getConnection();
265:                    loadPstmt = conn
266:                            .prepareStatement("select c_customer,c_balance from "
267:                                    + tableName + " where c_number=?");
268:                    loadPstmt.setInt(1, pk.number);
269:                    ResultSet rs = loadPstmt.executeQuery();
270:                    if (!rs.next()) {
271:                        logger.log(BasicLevel.ERROR,
272:                                "Object Not Found primary key : " + pk.number);
273:                        throw new javax.ejb.ObjectNotFoundException(
274:                                "primary key : " + pk.number);
275:                    }
276:                } catch (SQLException e) {
277:                    System.err.println("Fails to executeQuery " + e);
278:                    throw new javax.ejb.FinderException(
279:                            "Failed to executeQuery " + e);
280:                } finally {
281:                    try {
282:                        loadPstmt.close();
283:                        conn.close();
284:                    } catch (Exception e) {
285:                        logger.log(BasicLevel.ERROR,
286:                                "ejbFindByNoAccount: Ignore exception:" + e);
287:                    }
288:                }
289:                return pk;
290:            }
291:
292:            public Enumeration ejbFindCustomerAccounts(String name)
293:                    throws FinderException {
294:                logger.log(BasicLevel.DEBUG, "");
295:                PreparedStatement loadPstmt = null;
296:                Connection conn = null;
297:                Vector pkV = new Vector();
298:                try {
299:                    conn = getConnection();
300:                    loadPstmt = conn.prepareStatement("select c_number from "
301:                            + tableName + " where c_customer=?");
302:                    loadPstmt.setString(1, name);
303:                    ResultSet rs = loadPstmt.executeQuery();
304:                    while (rs.next()) {
305:                        int b = rs.getInt("c_number");
306:                        AccountPK pk = new AccountPK(b);
307:                        pkV.addElement((Object) pk);
308:                    }
309:
310:                } catch (SQLException e) {
311:                    System.err.println("Fails to executeQuery " + e);
312:                    throw new javax.ejb.FinderException(
313:                            "Failed to executeQuery " + e);
314:                } finally {
315:                    try {
316:                        loadPstmt.close();
317:                        conn.close();
318:                    } catch (Exception e) {
319:                        logger.log(BasicLevel.ERROR,
320:                                "ejbFindCustomerAccounts: Ignore exception:"
321:                                        + e);
322:                    }
323:                }
324:                return (pkV.elements());
325:            }
326:
327:            public Enumeration ejbFindBigAccounts(long minBalance)
328:                    throws FinderException {
329:                logger.log(BasicLevel.DEBUG, "");
330:                PreparedStatement loadPstmt = null;
331:                Connection conn = null;
332:                Vector pkV = new Vector();
333:                try {
334:                    conn = getConnection();
335:                    loadPstmt = conn.prepareStatement("select c_number from "
336:                            + tableName + " where c_balance>?");
337:                    loadPstmt.setLong(1, minBalance);
338:                    ResultSet rs = loadPstmt.executeQuery();
339:                    while (rs.next()) {
340:                        int b = rs.getInt("c_number");
341:                        AccountPK pk = new AccountPK(b);
342:                        pkV.addElement((Object) pk);
343:                    }
344:                } catch (SQLException e) {
345:                    System.err.println("Fails to executeQuery " + e);
346:                    throw new javax.ejb.FinderException(
347:                            "Failed to executeQuery " + e);
348:                } finally {
349:                    try {
350:                        loadPstmt.close();
351:                        conn.close();
352:                    } catch (Exception e) {
353:                        logger.log(BasicLevel.ERROR,
354:                                "ejbFindBigAccounts: Ignore exception:" + e);
355:                    }
356:                }
357:                return (pkV.elements());
358:            }
359:
360:            public Enumeration ejbFindAllAccounts() throws FinderException {
361:                logger.log(BasicLevel.DEBUG, "");
362:                PreparedStatement loadPstmt = null;
363:                Connection conn = null;
364:                Vector pkV = new Vector();
365:                try {
366:                    conn = getConnection();
367:                    loadPstmt = conn.prepareStatement("select c_number from "
368:                            + tableName);
369:                    ResultSet rs = loadPstmt.executeQuery();
370:                    while (rs.next()) {
371:                        int b = rs.getInt("c_number");
372:                        AccountPK pk = new AccountPK(b);
373:                        pkV.addElement((Object) pk);
374:                    }
375:                } catch (SQLException e) {
376:                    System.err.println("Fails to executeQuery " + e);
377:                    throw new javax.ejb.FinderException(
378:                            "Failed to executeQuery " + e);
379:                } finally {
380:                    try {
381:                        loadPstmt.close();
382:                        conn.close();
383:                    } catch (Exception e) {
384:                        logger.log(BasicLevel.ERROR,
385:                                "ejbFindAllAccounts: Ignore exception:" + e);
386:                    }
387:                }
388:                return (pkV.elements());
389:            }
390:
391:            private Connection getConnection() throws java.sql.SQLException {
392:                logger.log(BasicLevel.DEBUG, "");
393:                if (dataSource == null) {
394:                    // Finds DataSource from JNDI
395:                    Context initialContext = null;
396:                    try {
397:                        initialContext = new InitialContext();
398:                        dataSource = (DataSource) initialContext
399:                                .lookup("java:comp/env/jdbc/AccountEB");
400:                    } catch (Exception e) {
401:                        logger.log(BasicLevel.ERROR, "Pb with naming context");
402:                        throw new javax.ejb.EJBException(
403:                                "Pb with naming context ");
404:                    }
405:                }
406:                Connection ret = dataSource.getConnection();
407:                if (ret == null) {
408:                    throw new javax.ejb.EJBException(
409:                            "dataSource.getConnection() returned null");
410:                }
411:                return ret;
412:            }
413:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.