1 package org.apache.ojb.tutorial2; 2 3 17 18 import java.util.List ; 19 20 import org.odmg.Implementation; 21 import org.odmg.Database; 22 import org.odmg.Transaction; 23 import org.odmg.OQLQuery; 24 25 28 public class UCDeleteProduct extends AbstractUseCase 29 { 30 35 public UCDeleteProduct(Implementation odmg) 36 { 37 super(odmg); 38 } 39 40 45 public String getDescription() 46 { 47 return "Delete a product entry"; 48 } 49 50 53 public void apply() 54 { 55 String in = readLineWithMessage("Delete Product with id:"); 56 int id = Integer.parseInt(in); 57 58 61 String oqlQuery = "select del from " + Product.class.getName() + " where id = " + id; 63 64 Database db = odmg.getDatabase(null); Transaction tx = null; 66 67 try 68 { 69 tx = odmg.newTransaction(); 71 tx.begin(); 72 73 OQLQuery query = odmg.newOQLQuery(); 75 76 query.create(oqlQuery); 77 78 List result = (List )query.execute(); 79 Product toBeDeleted = (Product)result.get(0); 80 81 db.deletePersistent(toBeDeleted); 83 tx.commit(); 85 } 86 catch (Throwable t) 87 { 88 tx.abort(); 90 t.printStackTrace(); 91 } 92 } 93 } 94 | Popular Tags |