Source Code Cross Referenced for WidgetBean.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 WidgetBean implements  EntityBean, WidgetRemoteBusiness {
037:            private String widgetId;
038:            private String description;
039:            private double price;
040:            private EntityContext context;
041:            private Connection con;
042:            private String dbName = "jdbc/pointbase";
043:
044:            public String getDescription() {
045:                return description;
046:            }
047:
048:            public double getPrice() {
049:                return price;
050:            }
051:
052:            public String ejbCreate(String widgetId, String description,
053:                    double price) throws CreateException {
054:                try {
055:                    insertRow(widgetId, description, price);
056:                } catch (Exception ex) {
057:                    throw new EJBException("ejbCreate: " + ex.getMessage());
058:                }
059:
060:                this .widgetId = widgetId;
061:                this .description = description;
062:                this .price = price;
063:
064:                return widgetId;
065:            }
066:
067:            public String ejbFindByPrimaryKey(String primaryKey)
068:                    throws FinderException {
069:                boolean result;
070:
071:                try {
072:                    result = selectByPrimaryKey(primaryKey);
073:                } catch (Exception ex) {
074:                    throw new EJBException("ejbFindByPrimaryKey: "
075:                            + ex.getMessage());
076:                }
077:
078:                if (result) {
079:                    return primaryKey;
080:                } else {
081:                    throw new ObjectNotFoundException("Row for id "
082:                            + primaryKey + " not found.");
083:                }
084:            }
085:
086:            public void ejbRemove() {
087:                try {
088:                    deleteRow(widgetId);
089:                } catch (Exception ex) {
090:                    throw new EJBException("ejbRemove: " + ex.getMessage());
091:                }
092:            }
093:
094:            public void setEntityContext(EntityContext context) {
095:                this .context = context;
096:            }
097:
098:            public void unsetEntityContext() {
099:            }
100:
101:            public void ejbActivate() {
102:                widgetId = (String) context.getPrimaryKey();
103:            }
104:
105:            public void ejbPassivate() {
106:                widgetId = null;
107:            }
108:
109:            public void ejbLoad() {
110:                try {
111:                    loadRow();
112:                } catch (Exception ex) {
113:                    throw new EJBException("ejbLoad: " + ex.getMessage());
114:                }
115:            }
116:
117:            public void ejbStore() {
118:                try {
119:                    storeRow();
120:                } catch (Exception ex) {
121:                    throw new EJBException("ejbLoad: " + ex.getMessage());
122:                }
123:            }
124:
125:            public void ejbPostCreate(String widgetId, String description,
126:                    double price) {
127:            }
128:
129:            /*********************** Database Routines *************************/
130:            private void makeConnection() {
131:                try {
132:                    InitialContext ic = new InitialContext();
133:                    DataSource ds = (DataSource) ic.lookup(dbName);
134:
135:                    con = ds.getConnection();
136:                } catch (Exception ex) {
137:                    throw new EJBException("Unable to connect to database. "
138:                            + ex.getMessage());
139:                }
140:            }
141:
142:            private void releaseConnection() {
143:                try {
144:                    con.close();
145:                } catch (SQLException ex) {
146:                    throw new EJBException("releaseConnection: "
147:                            + ex.getMessage());
148:                }
149:            }
150:
151:            private void insertRow(String widgetId, String description,
152:                    double price) throws SQLException {
153:                makeConnection();
154:
155:                String insertStatement = "insert into widget values ( ? , ? , ? )";
156:                PreparedStatement prepStmt = con
157:                        .prepareStatement(insertStatement);
158:
159:                prepStmt.setString(1, widgetId);
160:                prepStmt.setString(2, description);
161:                prepStmt.setDouble(3, price);
162:
163:                prepStmt.executeUpdate();
164:                prepStmt.close();
165:                releaseConnection();
166:            }
167:
168:            private void deleteRow(String widgetId) throws SQLException {
169:                makeConnection();
170:
171:                String deleteStatement = "delete from widget where widgetid = ? ";
172:                PreparedStatement prepStmt = con
173:                        .prepareStatement(deleteStatement);
174:
175:                prepStmt.setString(1, widgetId);
176:                prepStmt.executeUpdate();
177:                prepStmt.close();
178:                releaseConnection();
179:            }
180:
181:            private boolean selectByPrimaryKey(String primaryKey)
182:                    throws SQLException {
183:                makeConnection();
184:
185:                String selectStatement = "select widgetid "
186:                        + "from widget where widgetid = ? ";
187:                PreparedStatement prepStmt = con
188:                        .prepareStatement(selectStatement);
189:
190:                prepStmt.setString(1, primaryKey);
191:
192:                ResultSet rs = prepStmt.executeQuery();
193:                boolean result = rs.next();
194:
195:                prepStmt.close();
196:                releaseConnection();
197:
198:                return result;
199:            }
200:
201:            private void loadRow() throws SQLException {
202:                makeConnection();
203:
204:                String selectStatement = "select description, price "
205:                        + "from widget where widgetid = ? ";
206:                PreparedStatement prepStmt = con
207:                        .prepareStatement(selectStatement);
208:
209:                prepStmt.setString(1, this .widgetId);
210:
211:                ResultSet rs = prepStmt.executeQuery();
212:
213:                if (rs.next()) {
214:                    this .description = rs.getString(1);
215:                    this .price = rs.getDouble(2);
216:                    prepStmt.close();
217:                } else {
218:                    prepStmt.close();
219:                    throw new NoSuchEntityException("Row for widgetId "
220:                            + widgetId + " not found in database.");
221:                }
222:
223:                releaseConnection();
224:            }
225:
226:            private void storeRow() throws SQLException {
227:                makeConnection();
228:
229:                String updateStatement = "update widget set description =  ? , "
230:                        + "price = ? " + "where widgetid = ?";
231:                PreparedStatement prepStmt = con
232:                        .prepareStatement(updateStatement);
233:
234:                prepStmt.setString(1, description);
235:                prepStmt.setDouble(2, price);
236:                prepStmt.setString(3, widgetId);
237:
238:                int rowCount = prepStmt.executeUpdate();
239:
240:                prepStmt.close();
241:
242:                if (rowCount == 0) {
243:                    throw new EJBException("Storing row for widgetId "
244:                            + widgetId + " failed.");
245:                }
246:
247:                releaseConnection();
248:            }
249:        }
250:        // WidgetBean 
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.