Advertisement
uopspop

Untitled

Mar 28th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. package point;
  2.  
  3. import javax.jdo.JDOHelper;
  4. import javax.jdo.PersistenceManager;
  5. import javax.jdo.PersistenceManagerFactory;
  6. import javax.persistence.*;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.Properties;
  11.  
  12. public class Main_Cook {
  13. public static void main(String[] args) {
  14. EntityManagerFactory emf = Persistence.createEntityManagerFactory("/Users/stsai/Desktop/points007.odb");
  15. EntityManager em = emf.createEntityManager();
  16.  
  17. // Store 1000 Cook objects in the database:
  18. em.getTransaction().begin();
  19. for (int i = 1000; i < 1500; i++) {
  20. Cook c = new Cook(i, i, i);
  21. em.persist(c);
  22. }
  23. em.getTransaction().commit();
  24.  
  25. // Find the number of Cook objects in the database:
  26. Query q1 = em.createQuery("SELECT COUNT(c) FROM Cook c");
  27. System.out.println("Total Cooks: " + q1.getSingleResult());
  28.  
  29. // Find the average X value:
  30. Query q2 = em.createQuery("SELECT AVG(c.x) FROM Cook c");
  31. System.out.println("Average X: " + q2.getSingleResult());
  32.  
  33. // Retrieve all the Cook objects from the database:
  34. TypedQuery<Cook> query =
  35. em.createQuery("SELECT c FROM Cook c", Cook.class);
  36. List<Cook> results = query.getResultList();
  37. for (Cook c : results) {
  38. System.out.println(c);
  39. }
  40.  
  41. // Close the database connection:
  42. em.close();
  43. emf.close();
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement