KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > fbasic > SimpleEB


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: SimpleEB.java,v 1.1 2003/12/05 14:16:52 legrasi Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.fbasic;
27
28 import java.rmi.RemoteException JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.sql.PreparedStatement JavaDoc;
31 import java.sql.ResultSet JavaDoc;
32 import java.sql.SQLException JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.Properties JavaDoc;
36 import java.util.Vector JavaDoc;
37
38 import javax.ejb.CreateException JavaDoc;
39 import javax.ejb.EJBException JavaDoc;
40 import javax.ejb.EntityBean JavaDoc;
41 import javax.ejb.EntityContext JavaDoc;
42 import javax.ejb.FinderException JavaDoc;
43 import javax.ejb.ObjectNotFoundException JavaDoc;
44
45 import javax.naming.Context JavaDoc;
46 import javax.naming.InitialContext JavaDoc;
47 import javax.sql.DataSource JavaDoc;
48
49
50
51 /**
52  * This is an entity bean with "bean managed persistence". The state of an
53  * instance is stored into a relational database.
54  * The table must exist.
55  * @author Philippe Durieux, Philippe Coq
56  */

57 public class SimpleEB extends SimpleEC implements EntityBean JavaDoc {
58
59     static final String JavaDoc tableName = "fbasicSimpleEB";
60
61     // Database related information
62
private DataSource JavaDoc dataSource = null;
63     private static String JavaDoc dataSourceName;
64
65
66     public void ejbLoad() {
67         // logger.log(BasicLevel.DEBUG, "");
68
String JavaDoc testname = (String JavaDoc)entityContext.getPrimaryKey();
69     PreparedStatement JavaDoc loadPstmt = null;
70     Connection JavaDoc conn = null;
71     try {
72         conn = getConnection();
73         loadPstmt = conn.prepareStatement("select c_info, c_numtest, c_testname from "+tableName+" where c_testname=?");
74
75         // PK fields
76
loadPstmt.setString(1, testname);
77         ResultSet JavaDoc rs = loadPstmt.executeQuery();
78         if (rs.next() == false) {
79         throw new javax.ejb.EJBException JavaDoc("Failed to load bean from database "+testname);
80         };
81         info = rs.getInt(1);
82         testname = rs.getString(3);
83         numtest = rs.getInt(2);
84         
85     } catch (SQLException JavaDoc e) {
86         throw new javax.ejb.EJBException JavaDoc("Failed to load bean from database" +e);
87     } finally {
88         try{
89         loadPstmt.close();
90         conn.close();
91         } catch(Exception JavaDoc e) {
92         System.err.println("ejbLoad: Ignore exception:"+e);
93         }
94     }
95
96     }
97
98     public void ejbStore() {
99         // logger.log(BasicLevel.DEBUG, "");
100
String JavaDoc testname = (String JavaDoc)entityContext.getPrimaryKey();
101     PreparedStatement JavaDoc storePstmt = null;
102     Connection JavaDoc conn = null;
103     
104     try {
105         conn = getConnection();
106         storePstmt = conn.prepareStatement("update "+tableName+" set c_info=?,c_numtest=? where c_testname=?");
107
108             storePstmt.setString(3, testname);
109         storePstmt.setInt(1, info);
110         storePstmt.setInt(2, numtest);
111         storePstmt.executeUpdate();
112     } catch (SQLException JavaDoc e) {
113         throw new javax.ejb.EJBException JavaDoc("Failed to store bean to database "+testname);
114         
115     } finally {
116         try{
117         storePstmt.close();
118         conn.close();
119         } catch(Exception JavaDoc e) {
120         System.err.println("ejbStore: Ignore exception:"+e);
121         }
122     }
123     }
124
125     public void ejbRemove() {
126         //logger.log(BasicLevel.DEBUG, "");
127
String JavaDoc testname = (String JavaDoc)entityContext.getPrimaryKey();
128     PreparedStatement JavaDoc removePstmt = null;
129     Connection JavaDoc conn = null;
130     try {
131         conn = getConnection();
132         removePstmt = conn.prepareStatement("delete from "+tableName+" where c_testname=?");
133         // PK fields
134
removePstmt.setString(1, testname );
135         removePstmt.executeUpdate();
136     } catch (SQLException JavaDoc e) {
137         throw new javax.ejb.EJBException JavaDoc("Failed to delete bean from database "+testname);
138     } finally {
139         try{
140         removePstmt.close();
141         conn.close();
142         } catch(Exception JavaDoc e) {
143         System.err.println("ejbRemove: Ignore exception:"+e);
144         }
145     }
146     }
147
148
149     public String JavaDoc ejbCreate(String JavaDoc name, int info, int num) throws CreateException JavaDoc {
150
151         super.ejbCreate(name, info, num);
152
153     PreparedStatement JavaDoc createPstmt = null;
154     Connection JavaDoc conn = null;
155
156     // persistence management
157
try {
158         conn = getConnection();
159         createPstmt = conn.prepareStatement("insert into "+tableName+" values (?, ?, ?)");
160         // all fields
161
createPstmt.setString(1, testname);
162             createPstmt.setInt(2, info);
163         createPstmt.setInt(3, numtest);
164         createPstmt.executeUpdate();
165     } catch (SQLException JavaDoc e) {
166         throw new javax.ejb.EJBException JavaDoc("Failed to create bean in database "+testname);
167     } finally {
168         try{
169         createPstmt.close();
170         conn.close();
171         } catch(Exception JavaDoc e) {
172                 // ignore exception
173
//logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
174
}
175     }
176     return name;
177     }
178
179     public void ejbPostCreate(String JavaDoc name, int info, int num) {
180         super.ejbPostCreate(name, info, num);
181     }
182
183  
184
185     public String JavaDoc ejbFindByPrimaryKey(String JavaDoc name ) throws ObjectNotFoundException JavaDoc, FinderException JavaDoc {
186         //logger.log(BasicLevel.DEBUG, "name="+ name);
187
PreparedStatement JavaDoc loadPstmt = null;
188     Connection JavaDoc conn = null;
189     try {
190         conn = getConnection();
191         loadPstmt = conn.prepareStatement("select c_info,c_numtest from "+tableName+" where c_testname=?");
192         loadPstmt.setString(1,name );
193         ResultSet JavaDoc rs = loadPstmt.executeQuery();
194         if (rs.next() == false) {
195         throw new javax.ejb.ObjectNotFoundException JavaDoc("primary key : "+name);
196         }
197     } catch (SQLException JavaDoc e) {
198         throw new javax.ejb.FinderException JavaDoc("Failed to executeQuery " +e);
199     } finally {
200         try{
201         loadPstmt.close();
202         conn.close();
203         } catch(Exception JavaDoc e) {
204         // ignore exception
205
//logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
206
}
207     }
208     return name;
209     }
210
211
212     public String JavaDoc ejbFindByTestName(String JavaDoc name)
213     throws ObjectNotFoundException JavaDoc, FinderException JavaDoc {
214         //logger.log(BasicLevel.DEBUG, "name="+ name);
215
PreparedStatement JavaDoc loadPstmt = null;
216     Connection JavaDoc conn = null;
217
218     try {
219         conn = getConnection();
220         loadPstmt = conn.prepareStatement("select c_info,c_numtest from "+tableName+" where c_testname=?");
221         loadPstmt.setString(1, name);
222         ResultSet JavaDoc rs = loadPstmt.executeQuery();
223         if (rs.next() == false) {
224         throw new javax.ejb.ObjectNotFoundException JavaDoc("primary key : "+name);
225         }
226     } catch (SQLException JavaDoc e) {
227         throw new javax.ejb.FinderException JavaDoc("Failed to executeQuery " +e);
228     } finally {
229         try{
230         loadPstmt.close();
231         conn.close();
232         } catch(Exception JavaDoc e) {
233                 // ignore exception
234
//logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
235
}
236     }
237     return name;
238     }
239
240     public Enumeration JavaDoc ejbFindInfoForNum(int num) throws FinderException JavaDoc {
241         // logger.log(BasicLevel.DEBUG, "num="+ num);
242
PreparedStatement JavaDoc loadPstmt = null;
243     Connection JavaDoc conn = null;
244     Vector JavaDoc pkV = new Vector JavaDoc();
245     try {
246         conn = getConnection();
247         loadPstmt = conn.prepareStatement("select c_testname from "+tableName+" where c_numtest=?");
248         loadPstmt.setInt(1, num);
249         ResultSet JavaDoc rs = loadPstmt.executeQuery();
250
251         while(rs.next()) {
252         String JavaDoc pk = rs.getString(1);
253         pkV.addElement(pk);
254         
255         }
256
257     } catch (SQLException JavaDoc e) {
258         throw new javax.ejb.FinderException JavaDoc("Failed to executeQuery " +e);
259     } finally {
260         try{
261         loadPstmt.close();
262         conn.close();
263         } catch(Exception JavaDoc e) {
264                 // ignore exception
265
//logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
266
}
267     }
268     return (pkV.elements());
269     }
270
271    public Collection JavaDoc ejbFindInCollection() throws FinderException JavaDoc {
272        //logger.log(BasicLevel.DEBUG, "");
273
PreparedStatement JavaDoc loadPstmt = null;
274        Connection JavaDoc conn = null;
275        Vector JavaDoc pkV = new Vector JavaDoc();
276     try {
277         conn = getConnection();
278         loadPstmt = conn.prepareStatement("select c_testname from "+tableName+" where c_numtest =?");
279             loadPstmt.setInt(1, 8);
280         ResultSet JavaDoc rs = loadPstmt.executeQuery();
281         while (rs.next()) {
282                 String JavaDoc pk = rs.getString(1);
283         pkV.addElement(pk);
284         }
285     } catch (SQLException JavaDoc e) {
286         throw new javax.ejb.FinderException JavaDoc("Failed to executeQuery " +e);
287     } finally {
288         try{
289         loadPstmt.close();
290         conn.close();
291         } catch(Exception JavaDoc e) {
292                 // ignore exception
293
//logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
294
}
295     }
296     return (pkV);
297    }
298
299     public Enumeration JavaDoc ejbFindInfo_beetwen(int p1, int p2) throws FinderException JavaDoc {
300         //logger.log(BasicLevel.DEBUG, "");
301
Vector JavaDoc pkC = new Vector JavaDoc();
302         Connection JavaDoc conn = null;
303         PreparedStatement JavaDoc loadPstmt = null;
304         try {
305             conn = getConnection();
306             loadPstmt = conn.prepareStatement("select c_testname from "+tableName+" where ?<=c_info and c_info<=? and ?<=c_numtest and c_numtest<=?");
307             loadPstmt.setInt(1, p1);
308             loadPstmt.setInt(2, p2);
309             loadPstmt.setInt(3, p1);
310             loadPstmt.setInt(4, p2);
311  
312             ResultSet JavaDoc rs = loadPstmt.executeQuery();
313             while (rs.next()) {
314                 String JavaDoc pk = rs.getString(1);
315                 pkC.addElement(pk);
316             }
317         } catch (Exception JavaDoc e) {
318             throw new javax.ejb.FinderException JavaDoc("Failed to find bean from database (findInfo_beetwen)");
319         } finally {
320             try{
321                 loadPstmt.close();
322                 conn.close();
323             } catch(Exception JavaDoc e) {
324                 // ignore exception
325
//logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
326
}
327         }
328         return(pkC.elements());
329     }
330
331     public Enumeration JavaDoc ejbFindEqualOne(int p1) throws FinderException JavaDoc {
332         // logger.log(BasicLevel.DEBUG, "");
333
Vector JavaDoc pkC = new Vector JavaDoc();
334         Connection JavaDoc conn = null;
335         PreparedStatement JavaDoc loadPstmt = null;
336         try {
337             conn = getConnection();
338             loadPstmt = conn.prepareStatement("select c_testname from "+tableName+" where c_info = ? and c_numtest = ?");
339             loadPstmt.setInt(1, p1);
340             loadPstmt.setInt(2, p1);
341  
342             ResultSet JavaDoc rs = loadPstmt.executeQuery();
343             while (rs.next()) {
344                 String JavaDoc pk = rs.getString(1);
345                 pkC.addElement(pk);
346             }
347         } catch (Exception JavaDoc e) {
348             throw new javax.ejb.FinderException JavaDoc("Failed to find bean from database (findInfo_beetwen)");
349         } finally {
350             try{
351                 loadPstmt.close();
352                 conn.close();
353             } catch(Exception JavaDoc e) {
354                 // ignore exception
355
//logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
356
}
357         }
358         return(pkC.elements());
359     }
360
361     public Enumeration JavaDoc ejbFindEqualTwo(int p1, int p2) throws FinderException JavaDoc {
362         // logger.log(BasicLevel.DEBUG, "");
363
Vector JavaDoc pkC = new Vector JavaDoc();
364         Connection JavaDoc conn = null;
365         PreparedStatement JavaDoc loadPstmt = null;
366         try {
367             conn = getConnection();
368             loadPstmt = conn.prepareStatement("select c_testname from "+tableName+" where c_info = ? and c_numtest = ?");
369             loadPstmt.setInt(1, p1);
370             loadPstmt.setInt(2, p2);
371  
372             ResultSet JavaDoc rs = loadPstmt.executeQuery();
373             while (rs.next()) {
374                 String JavaDoc pk = rs.getString(1);
375                 pkC.addElement(pk);
376             }
377         } catch (Exception JavaDoc e) {
378             throw new javax.ejb.FinderException JavaDoc("Failed to find bean from database (findInfo_beetwen)");
379         } finally {
380             try{
381                 loadPstmt.close();
382                 conn.close();
383             } catch(Exception JavaDoc e) {
384                 // ignore exception
385
// logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:"+e);
386
}
387         }
388         return(pkC.elements());
389     }
390
391     private Connection JavaDoc getConnection() throws java.sql.SQLException JavaDoc {
392     if (dataSource == null) {
393         // Finds DataSource from JNDI
394
Context JavaDoc initialContext = null;
395         try {
396         initialContext = new InitialContext JavaDoc();
397         dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/BMP");
398         } catch (Exception JavaDoc e) {
399         throw new javax.ejb.EJBException JavaDoc("Pb with naming context ");
400         }
401     }
402     Connection JavaDoc ret = dataSource.getConnection();
403     if (ret == null) {
404         throw new javax.ejb.EJBException JavaDoc("dataSource.getConnection() returned null");
405     }
406     return ret;
407     }
408
409     // Just to test we can define a method named 'ejbSelect' (see bug #522)
410
public void ejbSelect() {
411     }
412
413 }
414
Popular Tags