Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.Date;
  3. import java.util.Iterator;
  4.  
  5. import org.hibernate.HibernateException;
  6. import org.hibernate.Session;
  7. import org.hibernate.Transaction;
  8. import org.hibernate.SessionFactory;
  9. import org.hibernate.cfg.Configuration;
  10.  
  11. public class ManageEmployee {
  12. private static SessionFactory factory;
  13. public static void main(String[] args) {
  14.  
  15. try {
  16. factory = new Configuration().configure().buildSessionFactory();
  17. } catch (Throwable ex) {
  18. System.err.println("Failed to create sessionFactory object." + ex);
  19. throw new ExceptionInInitializerError(ex);
  20. }
  21.  
  22. ManageEmployee ME = new ManageEmployee();
  23.  
  24. /* Add few employee records in database */
  25. ME.addStudent("Zara", 001, 25);
  26. ME.addStudent("Daisy", 002, 30);
  27. ME.addStudent("John", 003, 19);
  28.  
  29.  
  30. }
  31.  
  32. /* Method to CREATE an employee in the database */
  33. public void addStudent(String name, int number, int age){
  34. Session session = factory.openSession();
  35. Transaction tx = null;
  36. Integer employeeID = null;
  37.  
  38. try {
  39. tx = session.beginTransaction();
  40. Student employee = new Student(name, number, age);
  41. employeeID = (Integer) session.save(employee);
  42. tx.commit();
  43. } catch (HibernateException e) {
  44. if (tx!=null) tx.rollback();
  45. e.printStackTrace();
  46. } finally {
  47. session.close();
  48. }
  49. }
  50.  
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement