KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > enroller > EnrollerBean


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.rmi.RemoteException JavaDoc;
31 import javax.ejb.*;
32 import java.sql.*;
33 import javax.sql.*;
34 import java.util.*;
35 import javax.naming.*;
36
37
38 public class EnrollerBean implements SessionBean, EnrollerRemoteBusiness {
39     private static final String JavaDoc dbName = "jdbc/pointbase";
40     private Connection con;
41     private SessionContext context;
42
43     public EnrollerBean() {
44     }
45
46     public void enroll(String JavaDoc studentId, String JavaDoc courseId) {
47         try {
48             insertEntry(studentId, courseId);
49         } catch (Exception JavaDoc ex) {
50             throw new EJBException("enroll: " + ex.getMessage());
51         }
52     }
53
54     public void unEnroll(String JavaDoc studentId, String JavaDoc courseId) {
55         try {
56             deleteEntry(studentId, courseId);
57         } catch (Exception JavaDoc ex) {
58             throw new EJBException("unEnroll: " + ex.getMessage());
59         }
60     }
61
62     public void deleteStudent(String JavaDoc studentId) {
63         try {
64             deleteStudentEntries(studentId);
65         } catch (Exception JavaDoc ex) {
66             throw new EJBException("deleteStudent: " + ex.getMessage());
67         }
68     }
69
70     public void deleteCourse(String JavaDoc courseId) {
71         try {
72             deleteCourseEntries(courseId);
73         } catch (Exception JavaDoc ex) {
74             throw new EJBException("deleteCourse: " + ex.getMessage());
75         }
76     }
77
78     public ArrayList getStudentIds(String JavaDoc courseId) {
79         try {
80             return selectStudent(courseId);
81         } catch (Exception JavaDoc ex) {
82             throw new EJBException("getStudentIds: " + ex.getMessage());
83         }
84     }
85
86     public ArrayList getCourseIds(String JavaDoc studentId) {
87         try {
88             return selectCourse(studentId);
89         } catch (Exception JavaDoc ex) {
90             throw new EJBException("getCourseIds: " + ex.getMessage());
91         }
92     }
93
94     public void ejbCreate() {
95     }
96
97     public void ejbRemove() {
98     }
99
100     public void ejbActivate() {
101     }
102
103     public void ejbPassivate() {
104     }
105
106     public void setSessionContext(SessionContext context) {
107         this.context = context;
108     }
109
110     /*********************** Database Routines *************************/
111     private void makeConnection() {
112         try {
113             InitialContext ic = new InitialContext();
114             DataSource ds = (DataSource) ic.lookup(dbName);
115
116             con = ds.getConnection();
117         } catch (Exception JavaDoc ex) {
118             throw new EJBException("Unable to connect to database. " +
119                 ex.getMessage());
120         }
121     }
122
123     private void releaseConnection() {
124         try {
125             con.close();
126         } catch (SQLException ex) {
127             throw new EJBException("releaseConnection: " + ex.getMessage());
128         }
129     }
130
131     private void insertEntry(String JavaDoc studentId, String JavaDoc courseId)
132         throws SQLException {
133         makeConnection();
134
135         String JavaDoc insertStatement = "insert into enrollment values ( ? , ? )";
136         PreparedStatement prepStmt = con.prepareStatement(insertStatement);
137
138         prepStmt.setString(1, studentId);
139         prepStmt.setString(2, courseId);
140
141         prepStmt.executeUpdate();
142         prepStmt.close();
143         releaseConnection();
144     }
145
146     private void deleteEntry(String JavaDoc studentId, String JavaDoc courseId)
147         throws SQLException {
148         makeConnection();
149
150         String JavaDoc deleteStatement =
151             "delete from enrollment " + "where studentid = ? and courseid = ?";
152         PreparedStatement prepStmt = con.prepareStatement(deleteStatement);
153
154         prepStmt.setString(1, studentId);
155         prepStmt.setString(2, courseId);
156         prepStmt.executeUpdate();
157         prepStmt.close();
158         releaseConnection();
159     }
160
161     private void deleteStudentEntries(String JavaDoc studentId)
162         throws SQLException {
163         makeConnection();
164
165         String JavaDoc deleteStatement =
166             "delete from enrollment " + "where studentid = ?";
167         PreparedStatement prepStmt = con.prepareStatement(deleteStatement);
168
169         prepStmt.setString(1, studentId);
170         prepStmt.executeUpdate();
171         prepStmt.close();
172         releaseConnection();
173     }
174
175     private void deleteCourseEntries(String JavaDoc courseId) throws SQLException {
176         makeConnection();
177
178         String JavaDoc deleteStatement =
179             "delete from enrollment " + "where courseid = ?";
180         PreparedStatement prepStmt = con.prepareStatement(deleteStatement);
181
182         prepStmt.setString(1, courseId);
183         prepStmt.executeUpdate();
184         prepStmt.close();
185         releaseConnection();
186     }
187
188     private ArrayList selectStudent(String JavaDoc courseId) throws SQLException {
189         makeConnection();
190
191         String JavaDoc selectStatement =
192             "select studentid " + "from enrollment where courseid = ? ";
193         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
194
195         prepStmt.setString(1, courseId);
196
197         ResultSet rs = prepStmt.executeQuery();
198         ArrayList a = new ArrayList();
199
200         while (rs.next()) {
201             String JavaDoc id = rs.getString(1);
202
203             a.add(id);
204         }
205
206         prepStmt.close();
207         releaseConnection();
208
209         return a;
210     }
211
212     private ArrayList selectCourse(String JavaDoc studentId) throws SQLException {
213         makeConnection();
214
215         String JavaDoc selectStatement =
216             "select courseid " + "from enrollment where studentid = ? ";
217         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
218
219         prepStmt.setString(1, studentId);
220
221         ResultSet rs = prepStmt.executeQuery();
222         ArrayList a = new ArrayList();
223
224         while (rs.next()) {
225             String JavaDoc id = rs.getString(1);
226
227             a.add(id);
228         }
229
230         prepStmt.close();
231         releaseConnection();
232
233         return a;
234     }
235 }
236  // EnrollerBean
237
Popular Tags