Source Code Cross Referenced for StorageBinBean.java in  » IDE-Netbeans » usersguide » storagebin » 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 » IDE Netbeans » usersguide » storagebin 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2005 Sun Microsystems, Inc.  All rights reserved.  U.S.
003:         * Government Rights - Commercial software.  Government users are subject
004:         * to the Sun Microsystems, Inc. standard license agreement and
005:         * applicable provisions of the FAR and its supplements.  Use is subject
006:         * to license terms.
007:         *
008:         * This distribution may include materials developed by third parties.
009:         * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
010:         * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
011:         * other countries.
012:         *
013:         * Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.
014:         *
015:         * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
016:         * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
017:         * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
018:         * en vigueur de la FAR (Federal Acquisition Regulations) et des
019:         * supplements a celles-ci.  Distribue par des licences qui en
020:         * restreignent l'utilisation.
021:         *
022:         * Cette distribution peut comprendre des composants developpes par des
023:         * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
024:         * sont des marques de fabrique ou des marques deposees de Sun
025:         * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
026:         */
027:
028:        package storagebin;
029:
030:        import java.sql.*;
031:        import javax.sql.*;
032:        import java.util.*;
033:        import javax.ejb.*;
034:        import javax.naming.*;
035:
036:        public class StorageBinBean implements  EntityBean,
037:                StorageBinRemoteBusiness {
038:            private static final String dbName = "jdbc/pointbase";
039:            private String storageBinId;
040:            private String widgetId;
041:            private int quantity;
042:            private EntityContext context;
043:            private Connection con;
044:
045:            public String getWidgetId() {
046:                return widgetId;
047:            }
048:
049:            public int getQuantity() {
050:                return quantity;
051:            }
052:
053:            public String ejbCreate(String storageBinId, String widgetId,
054:                    int quantity) throws CreateException {
055:                try {
056:                    insertRow(storageBinId, widgetId, quantity);
057:                } catch (Exception ex) {
058:                    throw new EJBException("ejbCreate: " + ex.getMessage());
059:                }
060:
061:                this .storageBinId = storageBinId;
062:                this .widgetId = widgetId;
063:                this .quantity = quantity;
064:
065:                return storageBinId;
066:            }
067:
068:            public String ejbFindByPrimaryKey(String primaryKey)
069:                    throws FinderException {
070:                boolean result;
071:
072:                try {
073:                    result = selectByPrimaryKey(primaryKey);
074:                } catch (Exception ex) {
075:                    throw new EJBException("ejbFindByPrimaryKey: "
076:                            + ex.getMessage());
077:                }
078:
079:                if (result) {
080:                    return primaryKey;
081:                } else {
082:                    throw new ObjectNotFoundException("Row for id "
083:                            + primaryKey + " not found.");
084:                }
085:            }
086:
087:            public String ejbFindByWidgetId(String widgetId)
088:                    throws FinderException {
089:                String storageBinId;
090:
091:                try {
092:                    storageBinId = selectByWidgetId(widgetId);
093:                } catch (Exception ex) {
094:                    throw new EJBException("ejbFindByWidgetId: "
095:                            + ex.getMessage());
096:                }
097:
098:                return storageBinId;
099:            }
100:
101:            public void ejbRemove() {
102:                try {
103:                    deleteRow(storageBinId);
104:                } catch (Exception ex) {
105:                    throw new EJBException("ejbRemove: " + ex.getMessage());
106:                }
107:            }
108:
109:            public void setEntityContext(EntityContext context) {
110:                this .context = context;
111:            }
112:
113:            public void unsetEntityContext() {
114:            }
115:
116:            public void ejbActivate() {
117:                storageBinId = (String) context.getPrimaryKey();
118:            }
119:
120:            public void ejbPassivate() {
121:                storageBinId = null;
122:            }
123:
124:            public void ejbLoad() {
125:                try {
126:                    loadRow();
127:                } catch (Exception ex) {
128:                    throw new EJBException("ejbLoad: " + ex.getMessage());
129:                }
130:            }
131:
132:            public void ejbStore() {
133:                try {
134:                    storeRow();
135:                } catch (Exception ex) {
136:                    throw new EJBException("ejbLoad: " + ex.getMessage());
137:                }
138:            }
139:
140:            public void ejbPostCreate(String storageBinId, String widgetId,
141:                    int quantity) {
142:            }
143:
144:            /*********************** Database Routines *************************/
145:            private void makeConnection() {
146:                try {
147:                    InitialContext ic = new InitialContext();
148:                    DataSource ds = (DataSource) ic.lookup(dbName);
149:
150:                    con = ds.getConnection();
151:                } catch (Exception ex) {
152:                    throw new EJBException("Unable to connect to database. "
153:                            + ex.getMessage());
154:                }
155:            }
156:
157:            private void releaseConnection() {
158:                try {
159:                    con.close();
160:                } catch (SQLException ex) {
161:                    throw new EJBException("releaseConnection: "
162:                            + ex.getMessage());
163:                }
164:            }
165:
166:            private void insertRow(String storageBinId, String widgetId,
167:                    int quantity) throws SQLException {
168:                makeConnection();
169:
170:                String insertStatement = "insert into storagebin values ( ? , ? , ? )";
171:                PreparedStatement prepStmt = con
172:                        .prepareStatement(insertStatement);
173:
174:                prepStmt.setString(1, storageBinId);
175:                prepStmt.setString(2, widgetId);
176:                prepStmt.setInt(3, quantity);
177:
178:                prepStmt.executeUpdate();
179:                prepStmt.close();
180:                releaseConnection();
181:            }
182:
183:            private void deleteRow(String storageBinId) throws SQLException {
184:                makeConnection();
185:
186:                String deleteStatement = "delete from storagebin where storagebinid = ? ";
187:                PreparedStatement prepStmt = con
188:                        .prepareStatement(deleteStatement);
189:
190:                prepStmt.setString(1, storageBinId);
191:                prepStmt.executeUpdate();
192:                prepStmt.close();
193:                releaseConnection();
194:            }
195:
196:            private boolean selectByPrimaryKey(String primaryKey)
197:                    throws SQLException {
198:                makeConnection();
199:
200:                String selectStatement = "select storagebinid "
201:                        + "from storagebin where storagebinid = ? ";
202:                PreparedStatement prepStmt = con
203:                        .prepareStatement(selectStatement);
204:
205:                prepStmt.setString(1, primaryKey);
206:
207:                ResultSet rs = prepStmt.executeQuery();
208:                boolean result = rs.next();
209:
210:                prepStmt.close();
211:                releaseConnection();
212:
213:                return result;
214:            }
215:
216:            private String selectByWidgetId(String widgetId)
217:                    throws SQLException {
218:                makeConnection();
219:
220:                String storageBinId;
221:
222:                String selectStatement = "select storagebinid "
223:                        + "from storagebin where widgetid = ? ";
224:                PreparedStatement prepStmt = con
225:                        .prepareStatement(selectStatement);
226:
227:                prepStmt.setString(1, widgetId);
228:
229:                ResultSet rs = prepStmt.executeQuery();
230:
231:                if (rs.next()) {
232:                    storageBinId = rs.getString(1);
233:                } else {
234:                    storageBinId = null;
235:                }
236:
237:                prepStmt.close();
238:                releaseConnection();
239:
240:                return storageBinId;
241:            }
242:
243:            private void loadRow() throws SQLException {
244:                makeConnection();
245:
246:                String selectStatement = "select widgetId, quantity "
247:                        + "from storagebin where storagebinid = ? ";
248:                PreparedStatement prepStmt = con
249:                        .prepareStatement(selectStatement);
250:
251:                prepStmt.setString(1, this .storageBinId);
252:
253:                ResultSet rs = prepStmt.executeQuery();
254:
255:                if (rs.next()) {
256:                    this .widgetId = rs.getString(1);
257:                    this .quantity = rs.getInt(2);
258:                    prepStmt.close();
259:                } else {
260:                    prepStmt.close();
261:                    throw new NoSuchEntityException("Row for storageBinId "
262:                            + storageBinId + " not found in database.");
263:                }
264:
265:                releaseConnection();
266:            }
267:
268:            private void storeRow() throws SQLException {
269:                makeConnection();
270:
271:                String updateStatement = "update storagebin set widgetId =  ? , "
272:                        + "quantity = ? " + "where storagebinid = ?";
273:                PreparedStatement prepStmt = con
274:                        .prepareStatement(updateStatement);
275:
276:                prepStmt.setString(1, widgetId);
277:                prepStmt.setInt(2, quantity);
278:                prepStmt.setString(3, storageBinId);
279:
280:                int rowCount = prepStmt.executeUpdate();
281:
282:                prepStmt.close();
283:
284:                if (rowCount == 0) {
285:                    throw new EJBException("Storing row for storageBinId "
286:                            + storageBinId + " failed.");
287:                }
288:
289:                releaseConnection();
290:            }
291:        }
292:        // StorageBinBean 
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.