1 25 26 package org.objectweb.jonas.jtests.beans.fbasic; 27 28 import java.rmi.RemoteException ; 29 import java.sql.Connection ; 30 import java.sql.PreparedStatement ; 31 import java.sql.ResultSet ; 32 import java.sql.SQLException ; 33 import java.util.Collection ; 34 import java.util.Enumeration ; 35 import java.util.Properties ; 36 import java.util.Vector ; 37 38 import javax.ejb.CreateException ; 39 import javax.ejb.EJBException ; 40 import javax.ejb.EntityBean ; 41 import javax.ejb.EntityContext ; 42 import javax.ejb.FinderException ; 43 import javax.ejb.ObjectNotFoundException ; 44 45 import javax.naming.Context ; 46 import javax.naming.InitialContext ; 47 import javax.sql.DataSource ; 48 49 50 51 57 public class SimpleEB extends SimpleEC implements EntityBean { 58 59 static final String tableName = "fbasicSimpleEB"; 60 61 private DataSource dataSource = null; 63 private static String dataSourceName; 64 65 66 public void ejbLoad() { 67 String testname = (String )entityContext.getPrimaryKey(); 69 PreparedStatement loadPstmt = null; 70 Connection 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 loadPstmt.setString(1, testname); 77 ResultSet rs = loadPstmt.executeQuery(); 78 if (rs.next() == false) { 79 throw new javax.ejb.EJBException ("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 e) { 86 throw new javax.ejb.EJBException ("Failed to load bean from database" +e); 87 } finally { 88 try{ 89 loadPstmt.close(); 90 conn.close(); 91 } catch(Exception e) { 92 System.err.println("ejbLoad: Ignore exception:"+e); 93 } 94 } 95 96 } 97 98 public void ejbStore() { 99 String testname = (String )entityContext.getPrimaryKey(); 101 PreparedStatement storePstmt = null; 102 Connection 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 e) { 113 throw new javax.ejb.EJBException ("Failed to store bean to database "+testname); 114 115 } finally { 116 try{ 117 storePstmt.close(); 118 conn.close(); 119 } catch(Exception e) { 120 System.err.println("ejbStore: Ignore exception:"+e); 121 } 122 } 123 } 124 125 public void ejbRemove() { 126 String testname = (String )entityContext.getPrimaryKey(); 128 PreparedStatement removePstmt = null; 129 Connection conn = null; 130 try { 131 conn = getConnection(); 132 removePstmt = conn.prepareStatement("delete from "+tableName+" where c_testname=?"); 133 removePstmt.setString(1, testname ); 135 removePstmt.executeUpdate(); 136 } catch (SQLException e) { 137 throw new javax.ejb.EJBException ("Failed to delete bean from database "+testname); 138 } finally { 139 try{ 140 removePstmt.close(); 141 conn.close(); 142 } catch(Exception e) { 143 System.err.println("ejbRemove: Ignore exception:"+e); 144 } 145 } 146 } 147 148 149 public String ejbCreate(String name, int info, int num) throws CreateException { 150 151 super.ejbCreate(name, info, num); 152 153 PreparedStatement createPstmt = null; 154 Connection conn = null; 155 156 try { 158 conn = getConnection(); 159 createPstmt = conn.prepareStatement("insert into "+tableName+" values (?, ?, ?)"); 160 createPstmt.setString(1, testname); 162 createPstmt.setInt(2, info); 163 createPstmt.setInt(3, numtest); 164 createPstmt.executeUpdate(); 165 } catch (SQLException e) { 166 throw new javax.ejb.EJBException ("Failed to create bean in database "+testname); 167 } finally { 168 try{ 169 createPstmt.close(); 170 conn.close(); 171 } catch(Exception e) { 172 } 175 } 176 return name; 177 } 178 179 public void ejbPostCreate(String name, int info, int num) { 180 super.ejbPostCreate(name, info, num); 181 } 182 183 184 185 public String ejbFindByPrimaryKey(String name ) throws ObjectNotFoundException , FinderException { 186 PreparedStatement loadPstmt = null; 188 Connection 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 rs = loadPstmt.executeQuery(); 194 if (rs.next() == false) { 195 throw new javax.ejb.ObjectNotFoundException ("primary key : "+name); 196 } 197 } catch (SQLException e) { 198 throw new javax.ejb.FinderException ("Failed to executeQuery " +e); 199 } finally { 200 try{ 201 loadPstmt.close(); 202 conn.close(); 203 } catch(Exception e) { 204 } 207 } 208 return name; 209 } 210 211 212 public String ejbFindByTestName(String name) 213 throws ObjectNotFoundException , FinderException { 214 PreparedStatement loadPstmt = null; 216 Connection 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 rs = loadPstmt.executeQuery(); 223 if (rs.next() == false) { 224 throw new javax.ejb.ObjectNotFoundException ("primary key : "+name); 225 } 226 } catch (SQLException e) { 227 throw new javax.ejb.FinderException ("Failed to executeQuery " +e); 228 } finally { 229 try{ 230 loadPstmt.close(); 231 conn.close(); 232 } catch(Exception e) { 233 } 236 } 237 return name; 238 } 239 240 public Enumeration ejbFindInfoForNum(int num) throws FinderException { 241 PreparedStatement loadPstmt = null; 243 Connection conn = null; 244 Vector pkV = new Vector (); 245 try { 246 conn = getConnection(); 247 loadPstmt = conn.prepareStatement("select c_testname from "+tableName+" where c_numtest=?"); 248 loadPstmt.setInt(1, num); 249 ResultSet rs = loadPstmt.executeQuery(); 250 251 while(rs.next()) { 252 String pk = rs.getString(1); 253 pkV.addElement(pk); 254 255 } 256 257 } catch (SQLException e) { 258 throw new javax.ejb.FinderException ("Failed to executeQuery " +e); 259 } finally { 260 try{ 261 loadPstmt.close(); 262 conn.close(); 263 } catch(Exception e) { 264 } 267 } 268 return (pkV.elements()); 269 } 270 271 public Collection ejbFindInCollection() throws FinderException { 272 PreparedStatement loadPstmt = null; 274 Connection conn = null; 275 Vector pkV = new Vector (); 276 try { 277 conn = getConnection(); 278 loadPstmt = conn.prepareStatement("select c_testname from "+tableName+" where c_numtest =?"); 279 loadPstmt.setInt(1, 8); 280 ResultSet rs = loadPstmt.executeQuery(); 281 while (rs.next()) { 282 String pk = rs.getString(1); 283 pkV.addElement(pk); 284 } 285 } catch (SQLException e) { 286 throw new javax.ejb.FinderException ("Failed to executeQuery " +e); 287 } finally { 288 try{ 289 loadPstmt.close(); 290 conn.close(); 291 } catch(Exception e) { 292 } 295 } 296 return (pkV); 297 } 298 299 public Enumeration ejbFindInfo_beetwen(int p1, int p2) throws FinderException { 300 Vector pkC = new Vector (); 302 Connection conn = null; 303 PreparedStatement 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 rs = loadPstmt.executeQuery(); 313 while (rs.next()) { 314 String pk = rs.getString(1); 315 pkC.addElement(pk); 316 } 317 } catch (Exception e) { 318 throw new javax.ejb.FinderException ("Failed to find bean from database (findInfo_beetwen)"); 319 } finally { 320 try{ 321 loadPstmt.close(); 322 conn.close(); 323 } catch(Exception e) { 324 } 327 } 328 return(pkC.elements()); 329 } 330 331 public Enumeration ejbFindEqualOne(int p1) throws FinderException { 332 Vector pkC = new Vector (); 334 Connection conn = null; 335 PreparedStatement 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 rs = loadPstmt.executeQuery(); 343 while (rs.next()) { 344 String pk = rs.getString(1); 345 pkC.addElement(pk); 346 } 347 } catch (Exception e) { 348 throw new javax.ejb.FinderException ("Failed to find bean from database (findInfo_beetwen)"); 349 } finally { 350 try{ 351 loadPstmt.close(); 352 conn.close(); 353 } catch(Exception e) { 354 } 357 } 358 return(pkC.elements()); 359 } 360 361 public Enumeration ejbFindEqualTwo(int p1, int p2) throws FinderException { 362 Vector pkC = new Vector (); 364 Connection conn = null; 365 PreparedStatement 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 rs = loadPstmt.executeQuery(); 373 while (rs.next()) { 374 String pk = rs.getString(1); 375 pkC.addElement(pk); 376 } 377 } catch (Exception e) { 378 throw new javax.ejb.FinderException ("Failed to find bean from database (findInfo_beetwen)"); 379 } finally { 380 try{ 381 loadPstmt.close(); 382 conn.close(); 383 } catch(Exception e) { 384 } 387 } 388 return(pkC.elements()); 389 } 390 391 private Connection getConnection() throws java.sql.SQLException { 392 if (dataSource == null) { 393 Context initialContext = null; 395 try { 396 initialContext = new InitialContext (); 397 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/BMP"); 398 } catch (Exception e) { 399 throw new javax.ejb.EJBException ("Pb with naming context "); 400 } 401 } 402 Connection ret = dataSource.getConnection(); 403 if (ret == null) { 404 throw new javax.ejb.EJBException ("dataSource.getConnection() returned null"); 405 } 406 return ret; 407 } 408 409 public void ejbSelect() { 411 } 412 413 } 414 | Popular Tags |