-
Notifications
You must be signed in to change notification settings - Fork 0
/
HibernateDbHandler.java
101 lines (96 loc) · 4.29 KB
/
HibernateDbHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import org.hibernate.*;
import org.hibernate.cfg.*;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import java.util.ArrayList;
import java.util.List;
class HibernateDbHandler {
public void addRecord(int rollNumber, String name, int physicsMarks, int chemistryMarks, int mathsMarks) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sfact = cfg.buildSessionFactory();
Session session = null;
Transaction transac = null;
try {
session = sfact.openSession();
transac = session.beginTransaction();
Student s = new Student(rollNumber, name, physicsMarks, chemistryMarks, mathsMarks);
session.save(s);
transac.commit();
JOptionPane.showMessageDialog(new JDialog(), "1 record added successfully.", "Record added", JOptionPane.INFORMATION_MESSAGE);
} catch(Exception e) {
transac.rollback();
JOptionPane.showMessageDialog(new JDialog(), e, "Record Insertion Error", JOptionPane.ERROR_MESSAGE);
} finally {
session.close();
}
}
public void deleteRecord(int rollNumber) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sfact = cfg.buildSessionFactory();
Session session = null;
Transaction transac = null;
try {
session = sfact.openSession();
transac = session.beginTransaction();
Student s = (Student) session.get(Student.class, rollNumber);
if (s != null) {
session.delete(s);
transac.commit();
JOptionPane.showMessageDialog(new JDialog(), "Record of Roll No. " + rollNumber + " deleted.", "Record deleted", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(new JDialog(), "Record of Roll No. " + rollNumber + " not found.", "Record deletion failed", JOptionPane.ERROR_MESSAGE);
}
} catch(Exception e) {
transac.rollback();
JOptionPane.showMessageDialog(new JDialog(), e, "Record Deletion Error", JOptionPane.INFORMATION_MESSAGE);
} finally {
session.close();
}
}
public List<Student> getRecords() {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sfact = cfg.buildSessionFactory();
Session session = null;
List<Student> studentList = new ArrayList<Student>();
try {
session = sfact.openSession();
studentList = session.createQuery("from Student").list();
} catch(Exception e) {
JOptionPane.showMessageDialog(new JDialog(), e, "Error in fetching records", JOptionPane.ERROR_MESSAGE);
} finally {
session.close();
}
return studentList;
}
public void updateRecord(int rollNumber, String name, int physicsMarks, int chemistryMarks, int mathsMarks) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sfact = cfg.buildSessionFactory();
Session session = null;
Transaction transac = null;
try {
session = sfact.openSession();
transac = session.beginTransaction();
Student s = (Student) session.get(Student.class, rollNumber);
if (s != null) {
s.setRollNumber(rollNumber);
s.setName(name);
s.setPhysicsMarks(physicsMarks);
s.setChemistryMarks(chemistryMarks);
s.setMathsMarks(mathsMarks);
transac.commit();
JOptionPane.showMessageDialog(new JDialog(), "Record of Roll No. " + rollNumber + " updated.", "Record updated", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(new JDialog(), "Record of Roll No. " + rollNumber + " not found.", "Record updation failed", JOptionPane.ERROR_MESSAGE);
}
} catch(Exception e) {
transac.rollback();
JOptionPane.showMessageDialog(new JDialog(), e, "Record Updation Error", JOptionPane.ERROR_MESSAGE);
} finally {
session.close();
}
}
}