KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > test > beans > EmployeeBean


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: EmployeeBean.java 1096 2004-03-26 21:41:16Z dblevins $
44  */

45 package org.openejb.test.beans;
46
47 import java.sql.Connection JavaDoc;
48 import java.sql.PreparedStatement JavaDoc;
49 import java.sql.ResultSet JavaDoc;
50 import java.sql.Statement JavaDoc;
51
52 import javax.ejb.EntityContext JavaDoc;
53 import javax.ejb.FinderException JavaDoc;
54 import javax.naming.InitialContext JavaDoc;
55
56 public class EmployeeBean implements javax.ejb.EntityBean JavaDoc {
57     int id;
58     String JavaDoc lastName;
59     String JavaDoc firstName;
60     
61     EntityContext JavaDoc ejbContext;
62     
63     public int ejbHomeSum(int one, int two) {
64         return one+two;
65     }
66     public Integer JavaDoc ejbFindByPrimaryKey(Integer JavaDoc primaryKey)
67     throws javax.ejb.FinderException JavaDoc{
68         boolean found = false;
69         try{
70         InitialContext JavaDoc jndiContext = new InitialContext JavaDoc( );
71         
72         javax.sql.DataSource JavaDoc ds =
73         (javax.sql.DataSource JavaDoc)jndiContext.lookup("java:comp/env/jdbc/orders");
74         
75         Connection JavaDoc con = ds.getConnection();
76         
77         
78         PreparedStatement JavaDoc stmt = con.prepareStatement("select * from Employees where EmployeeID = ?");
79         stmt.setInt(1, primaryKey.intValue());
80         ResultSet JavaDoc rs = stmt.executeQuery();
81         found = rs.next();
82         con.close();
83         }catch(Exception JavaDoc e){
84             e.printStackTrace();
85             throw new FinderException JavaDoc("FindByPrimaryKey failed");
86         }
87         
88         if(found)
89             return primaryKey;
90         else
91             throw new javax.ejb.ObjectNotFoundException JavaDoc();
92         
93         
94     }
95     public java.util.Collection JavaDoc ejbFindAll( ) throws FinderException JavaDoc{
96         try{
97         InitialContext JavaDoc jndiContext = new InitialContext JavaDoc( );
98         
99         javax.sql.DataSource JavaDoc ds =
100         (javax.sql.DataSource JavaDoc)jndiContext.lookup("java:comp/env/jdbc/orders");
101         
102         Connection JavaDoc con = ds.getConnection();
103         
104         Statement JavaDoc stmt = con.createStatement();
105         ResultSet JavaDoc rs = stmt.executeQuery("select EmployeeID from Employees");
106         java.util.Vector JavaDoc keys = new java.util.Vector JavaDoc();
107         while(rs.next()){
108             keys.addElement(new Integer JavaDoc(rs.getInt("EmployeeID")));
109         }
110         con.close();
111         return keys;
112         }catch(Exception JavaDoc e){
113             e.printStackTrace();
114             throw new FinderException JavaDoc("FindAll failed");
115         }
116     }
117     
118     public Integer JavaDoc ejbCreate(String JavaDoc fname, String JavaDoc lname)
119     throws javax.ejb.CreateException JavaDoc{
120         try{
121         lastName = lname;
122         firstName = fname;
123         
124         InitialContext JavaDoc jndiContext = new InitialContext JavaDoc( );
125  
126         javax.sql.DataSource JavaDoc ds =
127         (javax.sql.DataSource JavaDoc)jndiContext.lookup("java:comp/env/jdbc/orders");
128         
129         Connection JavaDoc con = ds.getConnection();
130         
131         
132         PreparedStatement JavaDoc stmt = con.prepareStatement("insert into Employees (FirstName, LastName) values (?,?)");
133         stmt.setString(1, firstName);
134         stmt.setString(2, lastName);
135         stmt.executeUpdate();
136         
137         stmt = con.prepareStatement("select EmployeeID from Employees where FirstName = ? AND LastName = ?");
138         stmt.setString(1, firstName);
139         stmt.setString(2, lastName);
140         ResultSet JavaDoc set = stmt.executeQuery();
141         while(set.next())
142             id = set.getInt("EmployeeID");
143         con.close();
144         
145         return new Integer JavaDoc(id);
146         
147         }catch(Exception JavaDoc e){
148             e.printStackTrace();
149             throw new javax.ejb.CreateException JavaDoc("can't create");
150         }
151     }
152     public String JavaDoc getLastName( ){
153         return lastName;
154     }
155     public String JavaDoc getFirstName( ){
156         return firstName;
157     }
158     public void setLastName(String JavaDoc lname){
159         lastName = lname;
160     }
161     public void setFirstName(String JavaDoc fname){
162         firstName = fname;
163     }
164     
165     public void ejbLoad( ){
166         try{
167         InitialContext JavaDoc jndiContext = new InitialContext JavaDoc( );
168         
169         javax.sql.DataSource JavaDoc ds =
170         (javax.sql.DataSource JavaDoc)jndiContext.lookup("java:comp/env/jdbc/orders");
171         
172         Connection JavaDoc con = ds.getConnection();
173         
174         
175         PreparedStatement JavaDoc stmt = con.prepareStatement("select * from Employees where EmployeeID = ?");
176         Integer JavaDoc primaryKey = (Integer JavaDoc)ejbContext.getPrimaryKey();
177         stmt.setInt(1, primaryKey.intValue());
178         ResultSet JavaDoc rs = stmt.executeQuery();
179         while(rs.next()){
180             lastName = rs.getString("LastName");
181             firstName = rs.getString("FirstName");
182         }
183         con.close();
184         
185         }catch(Exception JavaDoc e){
186             e.printStackTrace();
187         }
188         
189     }
190     
191     public void ejbStore( ){
192         try{
193         InitialContext JavaDoc jndiContext = new InitialContext JavaDoc( );
194         
195         javax.sql.DataSource JavaDoc ds =
196         (javax.sql.DataSource JavaDoc)jndiContext.lookup("java:comp/env/jdbc/orders");
197         Connection JavaDoc con = ds.getConnection();
198         
199         PreparedStatement JavaDoc stmt = con.prepareStatement("update Employees set FirstName = ?, LastName = ? where EmployeeID = ?");
200         stmt.setString(1, firstName);
201         stmt.setString(2, lastName);
202         stmt.setInt(3, id);
203         stmt.execute();
204         con.close();
205         }catch(Exception JavaDoc e){
206             e.printStackTrace();
207         }
208         
209     }
210     
211     public void ejbActivate( ){}
212     public void ejbPassivate( ){}
213     public void ejbRemove( ){
214                 
215         try{
216             InitialContext JavaDoc jndiContext = new InitialContext JavaDoc( );
217             
218             javax.sql.DataSource JavaDoc ds =
219             (javax.sql.DataSource JavaDoc)jndiContext.lookup("java:comp/env/jdbc/orders");
220             
221             Connection JavaDoc con = ds.getConnection();
222             
223             
224             PreparedStatement JavaDoc stmt = con.prepareStatement("delete from Employees where EmployeeID = ?");
225             Integer JavaDoc primaryKey = (Integer JavaDoc)ejbContext.getPrimaryKey();
226             stmt.setInt(1, primaryKey.intValue());
227             stmt.executeUpdate();
228             con.close();
229         
230         }catch(Exception JavaDoc e){
231             e.printStackTrace();
232         }
233     }
234     
235     public void setEntityContext(javax.ejb.EntityContext JavaDoc cntx){
236         ejbContext = cntx;
237     }
238     public void unsetEntityContext(){}
239 }
240     
241         
Popular Tags