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