/
EmpDAO.java
151 lines (135 loc) · 3.98 KB
/
EmpDAO.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//DERBY TABLE:- Employee_IUD
//EID-->primary key(number)
//ENAME--varchar 20
//ESAL--double
//GENDER--varchar 20
package Employee;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
*
* @author MSLCELTP1800
*/
public class EmpDAO {
private int empID;
private String empName;
private String empGender;
private double empSalary;
public EmpDAO(int empID, String empName, String empGender, double empSalary) {
this.empID = empID;
this.empName = empName;
this.empGender = empGender;
this.empSalary = empSalary;
}
public EmpDAO(){
}
/**
* @return the empID
*/
public int getEmpID() {
return empID;
}
/**
* @param empID the empID to set
*/
public void setEmpID(int empID) {
this.empID = empID;
}
/**
* @return the empName
*/
public String getEmpName() {
return empName;
}
/**
* @param empName the empName to set
*/
public void setEmpName(String empName) {
this.empName = empName;
}
/**
* @return the empGender
*/
public String getEmpGender() {
return empGender;
}
/**
* @param empGender the empGender to set
*/
public void setEmpGender(String empGender) {
this.empGender = empGender;
}
/**
* @return the empSalary
*/
public double getEmpSalary() {
return empSalary;
}
/**
* @param empSalary the empSalary to set
*/
public void setEmpSalary(double empSalary) {
this.empSalary = empSalary;
}
public boolean insert(){
Session session = HibernateUtil.getSessionFactory();
Transaction transaction = session.beginTransaction();
session.save(this);
transaction.commit();
return true;
}
public boolean update(int oid){
Session session = HibernateUtil.getSessionFactory();
Transaction transaction = session.beginTransaction();
EmpDAO e = (EmpDAO) session.get(EmpDAO.class, this.empID);
session.evict(e);
e.setEmpGender(this.empGender);
e.setEmpID(this.getEmpID());
e.setEmpName(this.empName);
e.setEmpSalary(this.empSalary);
session.update(e);
transaction.commit();
return true;
}
public static EmpDAO display(int id){
Session session = HibernateUtil.getSessionFactory();
Transaction transaction = session.beginTransaction();
EmpDAO e = (EmpDAO) session.get(EmpDAO.class, id);
System.out.println("Here!!!!!---------------->"+e.empName);
transaction.commit();
return e;
}
public boolean delete(int id){
Session session = HibernateUtil.getSessionFactory();
Transaction transaction = session.beginTransaction();
EmpDAO e = (EmpDAO)session.get(EmpDAO.class, id);
session.delete(e);
transaction.commit();
return true;
}
public List displayAll(){
List employees = new ArrayList();
Session session = HibernateUtil.getSessionFactory();
Transaction transaction = session.beginTransaction();
Criteria c = session.createCriteria(EmpDAO.class);
Iterator itr = c.list().iterator();
while(itr.hasNext()){
EmpDAO e = (EmpDAO)itr.next();
employees.add(e);
}
return employees;
}
@Override
public String toString() {
return "empID= " + empID + ", empName= " + empName + ", empGender= " + empGender + ", empSalary= " + empSalary ;
}
}