KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > enroller > StudentBean


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 enroller;
29
30 import java.sql.*;
31 import javax.sql.*;
32 import java.util.*;
33 import javax.ejb.*;
34 import javax.naming.*;
35 import javax.rmi.PortableRemoteObject JavaDoc;
36
37
38 public class StudentBean implements EntityBean, StudentRemoteBusiness {
39     private static final String JavaDoc dbName = "jdbc/pointbase";
40     private String JavaDoc studentId;
41     private String JavaDoc name;
42     private ArrayList courseIds;
43     private Connection con;
44     private EntityContext context;
45     private EnrollerRemoteHome enrollerHome;
46
47     public ArrayList getCourseIds() {
48         System.out.println("in getCourseIds");
49
50         return courseIds;
51     }
52
53     public String JavaDoc getName() {
54         return name;
55     }
56
57     public void setName(String JavaDoc name) {
58         this.name = name;
59     }
60
61     public String JavaDoc ejbCreate(String JavaDoc studentId, String JavaDoc name)
62         throws CreateException {
63         System.out.println("in ejbCreate");
64
65         try {
66             insertStudent(studentId, name);
67         } catch (Exception JavaDoc ex) {
68             throw new EJBException("ejbCreate: " + ex.getMessage());
69         }
70
71         this.studentId = studentId;
72         this.name = name;
73         System.out.println("about to leave ejbCreate");
74
75         return studentId;
76     }
77
78     public String JavaDoc ejbFindByPrimaryKey(String JavaDoc primaryKey)
79         throws FinderException {
80         boolean result;
81
82         try {
83             result = selectByPrimaryKey(primaryKey);
84         } catch (Exception JavaDoc ex) {
85             throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage());
86         }
87
88         if (result) {
89             return primaryKey;
90         } else {
91             throw new ObjectNotFoundException("Row for id " + primaryKey +
92                 " not found.");
93         }
94     }
95
96     public void ejbRemove() {
97         try {
98             deleteStudent(studentId);
99         } catch (Exception JavaDoc ex) {
100             throw new EJBException("ejbRemove: " + ex.getMessage());
101         }
102     }
103
104     public void setEntityContext(EntityContext context) {
105         System.out.println("in setEntityContext");
106         this.context = context;
107         courseIds = new ArrayList();
108
109         try {
110            
111             enrollerHome = lookupEnrollerBean();
112         } catch (Exception JavaDoc ex) {
113             throw new EJBException("setEntityContext: " + ex.getMessage());
114         }
115     }
116
117     public void unsetEntityContext() {
118     }
119
120     public void ejbActivate() {
121         studentId = (String JavaDoc) context.getPrimaryKey();
122     }
123
124     public void ejbPassivate() {
125         studentId = null;
126     }
127
128     public void ejbLoad() {
129         System.out.println("in ejbLoad");
130
131         try {
132             loadStudent();
133             loadCourseIds();
134         } catch (Exception JavaDoc ex) {
135             throw new EJBException("ejbLoad: " + ex.getMessage());
136         }
137
138         System.out.println("leaving ejbLoad");
139     }
140
141     private void loadCourseIds() {
142         System.out.println("in loadCourseIds");
143         courseIds.clear();
144         System.out.println("in loadCourseIds, about to try");
145
146         try {
147             EnrollerRemote enroller = enrollerHome.create();
148             ArrayList a = enroller.getCourseIds(studentId);
149
150             courseIds.addAll(a);
151         } catch (Exception JavaDoc ex) {
152             throw new EJBException("Exception in loadCourseIds: " +
153                 ex.getMessage());
154         }
155
156         System.out.println("leaving loadCourseIds");
157     }
158
159     public void ejbStore() {
160         System.out.println("in ejbStore");
161
162         try {
163             storeStudent();
164         } catch (Exception JavaDoc ex) {
165             throw new EJBException("ejbStore: " + ex.getMessage());
166         }
167
168         System.out.println("leaving ejbStore");
169     }
170
171     public void ejbPostCreate(String JavaDoc studentId, String JavaDoc name) {
172     }
173
174     /*********************** Database Routines *************************/
175     private void makeConnection() {
176         try {
177             InitialContext ic = new InitialContext();
178             DataSource ds = (DataSource) ic.lookup(dbName);
179
180             con = ds.getConnection();
181         } catch (Exception JavaDoc ex) {
182             throw new EJBException("Unable to connect to database. " +
183                 ex.getMessage());
184         }
185     }
186
187     private void releaseConnection() {
188         try {
189             con.close();
190         } catch (SQLException ex) {
191             throw new EJBException("releaseConnection: " + ex.getMessage());
192         }
193     }
194
195     private void insertStudent(String JavaDoc studentId, String JavaDoc name)
196         throws SQLException {
197         makeConnection();
198
199         String JavaDoc insertStatement = "insert into student values ( ? , ? )";
200         PreparedStatement prepStmt = con.prepareStatement(insertStatement);
201
202         prepStmt.setString(1, studentId);
203         prepStmt.setString(2, name);
204
205         prepStmt.executeUpdate();
206         prepStmt.close();
207         releaseConnection();
208     }
209
210     private boolean selectByPrimaryKey(String JavaDoc primaryKey)
211         throws SQLException {
212         makeConnection();
213
214         String JavaDoc selectStatement =
215             "select studentid " + "from student where studentid = ? ";
216         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
217
218         prepStmt.setString(1, primaryKey);
219
220         ResultSet rs = prepStmt.executeQuery();
221         boolean result = rs.next();
222
223         prepStmt.close();
224         releaseConnection();
225
226         return result;
227     }
228
229     private void deleteStudent(String JavaDoc studentId) throws SQLException {
230         makeConnection();
231
232         String JavaDoc deleteStatement =
233             "delete from student " + "where studentid = ?";
234         PreparedStatement prepStmt = con.prepareStatement(deleteStatement);
235
236         prepStmt.setString(1, studentId);
237         prepStmt.executeUpdate();
238         prepStmt.close();
239         releaseConnection();
240     }
241
242     private void loadStudent() throws SQLException {
243         makeConnection();
244
245         String JavaDoc selectStatement =
246             "select name " + "from student where studentid = ? ";
247         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
248
249         prepStmt.setString(1, studentId);
250
251         ResultSet rs = prepStmt.executeQuery();
252
253         if (rs.next()) {
254             name = rs.getString(1);
255             prepStmt.close();
256         } else {
257             prepStmt.close();
258             throw new NoSuchEntityException("Row for studentId " + studentId +
259                 " not found in database.");
260         }
261
262         releaseConnection();
263     }
264
265     private void storeStudent() throws SQLException {
266         makeConnection();
267
268         String JavaDoc updateStatement =
269             "update student set name = ? " + "where studentid = ?";
270         PreparedStatement prepStmt = con.prepareStatement(updateStatement);
271
272         prepStmt.setString(1, name);
273         prepStmt.setString(2, studentId);
274
275         int rowCount = prepStmt.executeUpdate();
276
277         prepStmt.close();
278
279         if (rowCount == 0) {
280             throw new EJBException("Storing row for studentId " + studentId +
281                 " failed.");
282         }
283
284         releaseConnection();
285     }
286
287     private enroller.EnrollerRemoteHome lookupEnrollerBean() {
288         try {
289             javax.naming.Context JavaDoc c = new javax.naming.InitialContext JavaDoc();
290             Object JavaDoc remote = c.lookup("ejb/SimpleEnroller");
291             enroller.EnrollerRemoteHome rv = (enroller.EnrollerRemoteHome) javax.rmi.PortableRemoteObject.narrow(remote, enroller.EnrollerRemoteHome.class);
292             return rv;
293         }
294         catch(javax.naming.NamingException JavaDoc ne) {
295             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne);
296             throw new RuntimeException JavaDoc(ne);
297         }
298         
299     }
300 }
301  // StudentBean
302
Popular Tags