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 enroller;
029:
030: import java.rmi.RemoteException;
031: import javax.ejb.*;
032: import java.sql.*;
033: import javax.sql.*;
034: import java.util.*;
035: import javax.naming.*;
036:
037: public class EnrollerBean implements SessionBean,
038: EnrollerRemoteBusiness {
039: private static final String dbName = "jdbc/pointbase";
040: private Connection con;
041: private SessionContext context;
042:
043: public EnrollerBean() {
044: }
045:
046: public void enroll(String studentId, String courseId) {
047: try {
048: insertEntry(studentId, courseId);
049: } catch (Exception ex) {
050: throw new EJBException("enroll: " + ex.getMessage());
051: }
052: }
053:
054: public void unEnroll(String studentId, String courseId) {
055: try {
056: deleteEntry(studentId, courseId);
057: } catch (Exception ex) {
058: throw new EJBException("unEnroll: " + ex.getMessage());
059: }
060: }
061:
062: public void deleteStudent(String studentId) {
063: try {
064: deleteStudentEntries(studentId);
065: } catch (Exception ex) {
066: throw new EJBException("deleteStudent: " + ex.getMessage());
067: }
068: }
069:
070: public void deleteCourse(String courseId) {
071: try {
072: deleteCourseEntries(courseId);
073: } catch (Exception ex) {
074: throw new EJBException("deleteCourse: " + ex.getMessage());
075: }
076: }
077:
078: public ArrayList getStudentIds(String courseId) {
079: try {
080: return selectStudent(courseId);
081: } catch (Exception ex) {
082: throw new EJBException("getStudentIds: " + ex.getMessage());
083: }
084: }
085:
086: public ArrayList getCourseIds(String studentId) {
087: try {
088: return selectCourse(studentId);
089: } catch (Exception ex) {
090: throw new EJBException("getCourseIds: " + ex.getMessage());
091: }
092: }
093:
094: public void ejbCreate() {
095: }
096:
097: public void ejbRemove() {
098: }
099:
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 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: "
128: + ex.getMessage());
129: }
130: }
131:
132: private void insertEntry(String studentId, String courseId)
133: throws SQLException {
134: makeConnection();
135:
136: String insertStatement = "insert into enrollment values ( ? , ? )";
137: PreparedStatement prepStmt = con
138: .prepareStatement(insertStatement);
139:
140: prepStmt.setString(1, studentId);
141: prepStmt.setString(2, courseId);
142:
143: prepStmt.executeUpdate();
144: prepStmt.close();
145: releaseConnection();
146: }
147:
148: private void deleteEntry(String studentId, String courseId)
149: throws SQLException {
150: makeConnection();
151:
152: String deleteStatement = "delete from enrollment "
153: + "where studentid = ? and courseid = ?";
154: PreparedStatement prepStmt = con
155: .prepareStatement(deleteStatement);
156:
157: prepStmt.setString(1, studentId);
158: prepStmt.setString(2, courseId);
159: prepStmt.executeUpdate();
160: prepStmt.close();
161: releaseConnection();
162: }
163:
164: private void deleteStudentEntries(String studentId)
165: throws SQLException {
166: makeConnection();
167:
168: String deleteStatement = "delete from enrollment "
169: + "where studentid = ?";
170: PreparedStatement prepStmt = con
171: .prepareStatement(deleteStatement);
172:
173: prepStmt.setString(1, studentId);
174: prepStmt.executeUpdate();
175: prepStmt.close();
176: releaseConnection();
177: }
178:
179: private void deleteCourseEntries(String courseId)
180: throws SQLException {
181: makeConnection();
182:
183: String deleteStatement = "delete from enrollment "
184: + "where courseid = ?";
185: PreparedStatement prepStmt = con
186: .prepareStatement(deleteStatement);
187:
188: prepStmt.setString(1, courseId);
189: prepStmt.executeUpdate();
190: prepStmt.close();
191: releaseConnection();
192: }
193:
194: private ArrayList selectStudent(String courseId)
195: throws SQLException {
196: makeConnection();
197:
198: String selectStatement = "select studentid "
199: + "from enrollment where courseid = ? ";
200: PreparedStatement prepStmt = con
201: .prepareStatement(selectStatement);
202:
203: prepStmt.setString(1, courseId);
204:
205: ResultSet rs = prepStmt.executeQuery();
206: ArrayList a = new ArrayList();
207:
208: while (rs.next()) {
209: String id = rs.getString(1);
210:
211: a.add(id);
212: }
213:
214: prepStmt.close();
215: releaseConnection();
216:
217: return a;
218: }
219:
220: private ArrayList selectCourse(String studentId)
221: throws SQLException {
222: makeConnection();
223:
224: String selectStatement = "select courseid "
225: + "from enrollment where studentid = ? ";
226: PreparedStatement prepStmt = con
227: .prepareStatement(selectStatement);
228:
229: prepStmt.setString(1, studentId);
230:
231: ResultSet rs = prepStmt.executeQuery();
232: ArrayList a = new ArrayList();
233:
234: while (rs.next()) {
235: String id = rs.getString(1);
236:
237: a.add(id);
238: }
239:
240: prepStmt.close();
241: releaseConnection();
242:
243: return a;
244: }
245: }
246: // EnrollerBean
|