KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > storagebin > WidgetBean


1 /*
2  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. U.S.
3  * Government Rights - Commercial software. Government users are subject
4  * to the Sun Microsystems, Inc. standard license agreement and
5  * applicable provisions of the FAR and its supplements. Use is subject
6  * to license terms.
7  *
8  * This distribution may include materials developed by third parties.
9  * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
10  * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
11  * other countries.
12  *
13  * Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.
14  *
15  * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
16  * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
17  * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
18  * en vigueur de la FAR (Federal Acquisition Regulations) et des
19  * supplements a celles-ci. Distribue par des licences qui en
20  * restreignent l'utilisation.
21  *
22  * Cette distribution peut comprendre des composants developpes par des
23  * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
24  * sont des marques de fabrique ou des marques deposees de Sun
25  * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
26  */

27
28 package storagebin;
29
30 import java.sql.*;
31 import javax.sql.*;
32 import java.util.*;
33 import javax.ejb.*;
34 import javax.naming.*;
35
36
37 public class WidgetBean implements EntityBean, WidgetRemoteBusiness {
38     private String JavaDoc widgetId;
39     private String JavaDoc description;
40     private double price;
41     private EntityContext context;
42     private Connection con;
43     private String JavaDoc dbName = "jdbc/pointbase";
44
45     public String JavaDoc getDescription() {
46         return description;
47     }
48
49     public double getPrice() {
50         return price;
51     }
52
53     public String JavaDoc ejbCreate(String JavaDoc widgetId, String JavaDoc description, double price)
54         throws CreateException {
55         try {
56             insertRow(widgetId, description, price);
57         } catch (Exception JavaDoc ex) {
58             throw new EJBException("ejbCreate: " + ex.getMessage());
59         }
60
61         this.widgetId = widgetId;
62         this.description = description;
63         this.price = price;
64
65         return widgetId;
66     }
67
68     public String JavaDoc ejbFindByPrimaryKey(String JavaDoc primaryKey)
69         throws FinderException {
70         boolean result;
71
72         try {
73             result = selectByPrimaryKey(primaryKey);
74         } catch (Exception JavaDoc ex) {
75             throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage());
76         }
77
78         if (result) {
79             return primaryKey;
80         } else {
81             throw new ObjectNotFoundException("Row for id " + primaryKey +
82                 " not found.");
83         }
84     }
85
86     public void ejbRemove() {
87         try {
88             deleteRow(widgetId);
89         } catch (Exception JavaDoc ex) {
90             throw new EJBException("ejbRemove: " + ex.getMessage());
91         }
92     }
93
94     public void setEntityContext(EntityContext context) {
95         this.context = context;
96     }
97
98     public void unsetEntityContext() {
99     }
100
101     public void ejbActivate() {
102         widgetId = (String JavaDoc) context.getPrimaryKey();
103     }
104
105     public void ejbPassivate() {
106         widgetId = null;
107     }
108
109     public void ejbLoad() {
110         try {
111             loadRow();
112         } catch (Exception JavaDoc ex) {
113             throw new EJBException("ejbLoad: " + ex.getMessage());
114         }
115     }
116
117     public void ejbStore() {
118         try {
119             storeRow();
120         } catch (Exception JavaDoc ex) {
121             throw new EJBException("ejbLoad: " + ex.getMessage());
122         }
123     }
124
125     public void ejbPostCreate(String JavaDoc widgetId, String JavaDoc description, double price) {
126     }
127
128     /*********************** Database Routines *************************/
129     private void makeConnection() {
130         try {
131             InitialContext ic = new InitialContext();
132             DataSource ds = (DataSource) ic.lookup(dbName);
133
134             con = ds.getConnection();
135         } catch (Exception JavaDoc ex) {
136             throw new EJBException("Unable to connect to database. " +
137                 ex.getMessage());
138         }
139     }
140
141     private void releaseConnection() {
142         try {
143             con.close();
144         } catch (SQLException ex) {
145             throw new EJBException("releaseConnection: " + ex.getMessage());
146         }
147     }
148
149     private void insertRow(String JavaDoc widgetId, String JavaDoc description, double price)
150         throws SQLException {
151         makeConnection();
152
153         String JavaDoc insertStatement = "insert into widget values ( ? , ? , ? )";
154         PreparedStatement prepStmt = con.prepareStatement(insertStatement);
155
156         prepStmt.setString(1, widgetId);
157         prepStmt.setString(2, description);
158         prepStmt.setDouble(3, price);
159
160         prepStmt.executeUpdate();
161         prepStmt.close();
162         releaseConnection();
163     }
164
165     private void deleteRow(String JavaDoc widgetId) throws SQLException {
166         makeConnection();
167
168         String JavaDoc deleteStatement = "delete from widget where widgetid = ? ";
169         PreparedStatement prepStmt = con.prepareStatement(deleteStatement);
170
171         prepStmt.setString(1, widgetId);
172         prepStmt.executeUpdate();
173         prepStmt.close();
174         releaseConnection();
175     }
176
177     private boolean selectByPrimaryKey(String JavaDoc primaryKey)
178         throws SQLException {
179         makeConnection();
180
181         String JavaDoc selectStatement =
182             "select widgetid " + "from widget where widgetid = ? ";
183         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
184
185         prepStmt.setString(1, primaryKey);
186
187         ResultSet rs = prepStmt.executeQuery();
188         boolean result = rs.next();
189
190         prepStmt.close();
191         releaseConnection();
192
193         return result;
194     }
195
196     private void loadRow() throws SQLException {
197         makeConnection();
198
199         String JavaDoc selectStatement =
200             "select description, price " + "from widget where widgetid = ? ";
201         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
202
203         prepStmt.setString(1, this.widgetId);
204
205         ResultSet rs = prepStmt.executeQuery();
206
207         if (rs.next()) {
208             this.description = rs.getString(1);
209             this.price = rs.getDouble(2);
210             prepStmt.close();
211         } else {
212             prepStmt.close();
213             throw new NoSuchEntityException("Row for widgetId " + widgetId +
214                 " not found in database.");
215         }
216
217         releaseConnection();
218     }
219
220     private void storeRow() throws SQLException {
221         makeConnection();
222
223         String JavaDoc updateStatement =
224             "update widget set description = ? , " + "price = ? " +
225             "where widgetid = ?";
226         PreparedStatement prepStmt = con.prepareStatement(updateStatement);
227
228         prepStmt.setString(1, description);
229         prepStmt.setDouble(2, price);
230         prepStmt.setString(3, widgetId);
231
232         int rowCount = prepStmt.executeUpdate();
233
234         prepStmt.close();
235
236         if (rowCount == 0) {
237             throw new EJBException("Storing row for widgetId " + widgetId +
238                 " failed.");
239         }
240
241         releaseConnection();
242     }
243 }
244  // WidgetBean
245
Popular Tags