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
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
13
* Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.
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.
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.
34
import javax.naming.*;
35
import javax.rmi.PortableRemoteObject;
38
public class SalesRepBean implements EntityBean {
39
private static final String dbName = "java:comp/env/jdbc/SalesDB";
40
private String salesRepId;
42
private ArrayList customerIds;
43
private Connection con;
44
private EntityContext context;
45
private CustomerRemoteHome customerHome;
47
public ArrayList getCustomerIds() {
48
System.out.println("in getCustomerIds");
53
public String getName() {
57
public void setName(String name) {
61
public String ejbCreate(String salesRepId, String name)
62
throws CreateException {
63
System.out.println("in ejbCreate");
66
insertSalesRep(salesRepId, name);
67
} catch (Exception ex) {
68
throw new EJBException("ejbCreate: " + ex.getMessage());
71
this.salesRepId = salesRepId;
73
System.out.println("about to leave ejbCreate");
78
public String ejbFindByPrimaryKey(String primaryKey)
79
throws FinderException {
83
result = selectByPrimaryKey(primaryKey);
84
} catch (Exception ex) {
85
throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage());
91
throw new ObjectNotFoundException("Row for id " + primaryKey +
96
public void ejbRemove() {
98
deleteSalesRep(salesRepId);
99
} catch (Exception ex) {
100
throw new EJBException("ejbRemove: " + ex.getMessage());
104
public void setEntityContext(EntityContext context) {
105
System.out.println("in setEntityContext");
106
this.context = context;
107
customerIds = new ArrayList();
110
Context initial = new InitialContext();
111
Object objref = initial.lookup("java:comp/env/ejb/Customer");
114
(CustomerRemoteHome) PortableRemoteObject.narrow(objref,
115
CustomerRemoteHome.class);
116
} catch (Exception ex) {
117
throw new EJBException("setEntityContext: " + ex.getMessage());
120
System.out.println("leaving setEntityContext");
123
public void unsetEntityContext() {
126
public void ejbActivate() {
127
salesRepId = (String) context.getPrimaryKey();
130
public void ejbPassivate() {
134
public void ejbLoad() {
135
System.out.println("in ejbLoad");
140
} catch (Exception ex) {
141
throw new EJBException("ejbLoad: " + ex.getMessage());
144
System.out.println("leaving ejbLoad");
147
private void loadCustomerIds() {
148
System.out.println("in loadCustomerIds");
152
Collection c = customerHome.findBySalesRep(salesRepId);
153
Iterator i = c.iterator();
155
while (i.hasNext()) {
156
CustomerRemote customer = (CustomerRemote) i.next();
157
String id = (String) customer.getPrimaryKey();
159
System.out.println("adding " + id + " to list");
162
} catch (Exception ex) {
163
throw new EJBException("Exception in loadCustomerIds: " +
168
public void ejbStore() {
169
System.out.println("in ejbStore");
173
} catch (Exception ex) {
174
throw new EJBException("ejbStore: " + ex.getMessage());
177
System.out.println("leaving ejbStore");
180
public void ejbPostCreate(String salesRepId, String name) {
183
/*********************** Database Routines *************************/
184
private void makeConnection() {
186
InitialContext ic = new InitialContext();
187
DataSource ds = (DataSource) ic.lookup(dbName);
189
con = ds.getConnection();
190
} catch (Exception ex) {
191
throw new EJBException("Unable to connect to database. " +
196
private void releaseConnection() {
199
} catch (SQLException ex) {
200
throw new EJBException("releaseConnection: " + ex.getMessage());
204
private void insertSalesRep(String salesRepId, String name)
205
throws SQLException {
207
System.out.println("in insertSalesRep");
209
String insertStatement = "insert into salesrep values ( ? , ? )";
210
PreparedStatement prepStmt = con.prepareStatement(insertStatement);
212
prepStmt.setString(1, salesRepId);
213
prepStmt.setString(2, name);
215
prepStmt.executeUpdate();
217
System.out.println("leaving insertSalesRep");
221
private boolean selectByPrimaryKey(String primaryKey)
222
throws SQLException {
225
String selectStatement =
226
"select salesrepid " + "from salesrep where salesrepid = ? ";
227
PreparedStatement prepStmt = con.prepareStatement(selectStatement);
229
prepStmt.setString(1, primaryKey);
231
ResultSet rs = prepStmt.executeQuery();
232
boolean result = rs.next();
240
private void deleteSalesRep(String salesRepId) throws SQLException {
243
String deleteStatement =
244
"delete from salesrep " + "where salesrepid = ?";
245
PreparedStatement prepStmt = con.prepareStatement(deleteStatement);
247
prepStmt.setString(1, salesRepId);
248
prepStmt.executeUpdate();
253
private void loadSalesRep() throws SQLException {
256
String selectStatement =
257
"select name " + "from salesRep where salesrepid = ? ";
258
PreparedStatement prepStmt = con.prepareStatement(selectStatement);
260
prepStmt.setString(1, salesRepId);
262
ResultSet rs = prepStmt.executeQuery();
265
name = rs.getString(1);
269
throw new NoSuchEntityException("Row for salesRepId " + salesRepId +
270
" not found in database.");
276
private void storeSalesRep() throws SQLException {
279
String updateStatement =
280
"update salesrep set name = ? " + "where salesrepid = ?";
281
PreparedStatement prepStmt = con.prepareStatement(updateStatement);
283
prepStmt.setString(1, name);
284
prepStmt.setString(2, salesRepId);
286
int rowCount = prepStmt.executeUpdate();
291
throw new EJBException("Storing row for salesRepId " + salesRepId +