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
|