在Java的Hibernate框架中对数据库数据进行查询操作

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

这篇文章主要介绍了Java的Hibernate框架中对数据库数据进行查询操作的方法,Hibernate 是Java的SSH三大web开发框架之一
能列表:
DISTINCT关键字只计算在该行设定的唯一值。

下面的查询将只返回唯一的计数:
最后,我们将创建应用程序类的main()方法来运行,我们将使用Criteria查询的应用程序:
?
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 importjava.util.List;
importjava.util.Date;
importjava.util.Iterator;
importorg.hibernate.HibernateException;
importorg.hibernate.Session;
importorg.hibernate.Transaction;
importorg.hibernate.SessionFactory;
importorg.hibernate.Criteria;
importorg.hibernate.criterion.Restrictions;
importorg.hibernate.criterion.Projections;
importorg.hibernate.cfg.Configuration;
publicclassManageEmployee {
privatestaticSessionFactory factory;
publicstaticvoidmain(String[] args) {
try{
factory = newConfiguration().configure().buildSessionFactory();
}catch(Throwable ex) {
System.err.println("Failed to create sessionFactory object."+ ex);
thrownewExceptionInInitializerError(ex);
}
ManageEmployee ME = newManageEmployee();
/* Add few employee records in database */
Integer empID1 = ME.addEmployee("Zara", "Ali", 2000);
Integer empID2 = ME.addEmployee("Daisy", "Das", 5000);
Integer empID3 = ME.addEmployee("John", "Paul", 5000);
Integer empID4 = ME.addEmployee("Mohd", "Yasee", 3000);
/* List down all the employees */
ME.listEmployees();
/* Print Total employee's count */
ME.countEmployee();
/* Print Toatl salary */
ME.totalSalary();
}
/* Method to CREATE an employee in the database */
public Integer addEmployee(String fname, String lname, int salary){
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
Session session = factory.openSession();
Transaction tx = null;
Integer employeeID = null;
try{
tx = session.beginTransaction();
Employee employee = new Employee(fname, lname, salary);
employeeID = (Integer) session.save(employee);
mit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
return employeeID;
}
/* Method to READ all the employees having salary more than 2000 */ public void listEmployees( ){
Session session = factory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
Criteria cr = session.createCriteria(Employee.class);
// Add restriction.
cr.add(Restrictions.gt("salary", 2000));
List employees = cr.list();
for (Iterator iterator =
employees.iterator(); iterator.hasNext();){ Employee employee = (Employee) iterator.next();
System.out.print("First Name: " + employee.getFirstName());
System.out.print(" Last Name: " + employee.getLastName());
System.out.println(" Salary: " + employee.getSalary());
}
mit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
}
/* Method to print total number of records */
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
public void countEmployee(){
Session session = factory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
Criteria cr = session.createCriteria(Employee.class);
// To get total row count.
cr.setProjection(Projections.rowCount());
List rowCount = cr.list();
System.out.println("Total Coint: " + rowCount.get(0) );
mit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
}
/* Method to print sum of salaries */
publicvoidtotalSalary(){
Session session = factory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
Criteria cr = session.createCriteria(Employee.class);
// To get total salary.
cr.setProjection(Projections.sum("salary"));
List totalSalary = cr.list();
System.out.println("Total Salary: "+ totalSalary.get(0) );
mit();
}catch(HibernateException e) {
if(tx!=null) tx.rollback();
e.printStackTrace();
}finally{
session.close();
}
}
}。

相关文档
最新文档