exp2
exp2
Hema Sundharam
ID NO : 2200030323
EXPERIMENT-2
Student.java:
package com.klu.EXP2;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "student2")
class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
this.id = id;
return name;
this.name = name;
return gender;
this.gender = gender;
return department;
this.department = department;
return dob;
this.dob = dob;
return contactnumber;
return cgpa;
this.cgpa = cgpa;
return nob;
this.nob = nob;
@Override
return "Student [id=" + id + ", name=" + name + ", gender=" + gender + ", department="
+ department + ", dob="
App.java:
package com.klu.EXP2;
import java.util.List;
import java.util.Scanner;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.query.Query;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Order;
while (true) {
System.out.println("1. Insert Student");
System.out.println("2. Fetch Student by ID");
System.out.println("3. Update Student");
System.out.println("4. Delete Student");
System.out.println("5. HQL: Display all student records with all columns");
System.out.println("6. HQL: Display all student records with specific columns");
System.out.println("7. HQL: Display names of students with CGPA greater than 7");
System.out.println("8. HQL: Delete a student by ID using parameter");
System.out.println("9. HQL: Update student details by ID using parameter");
System.out.println("10. HQL: Aggregate functions on CGPA column");
System.out.println("11. HCQL: Display specific columns from student records");
System.out.println("12. HCQL: Get 5th to 10th records");
System.out.println("13. HCQL: Apply various comparisons on CGPA column");
System.out.println("14. HCQL: Get records ordered by Student Name");
System.out.println("15. Exit");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
while (true) {
Student student = new Student();
System.out.print("Enter Name: ");
student.setName(sc.next());
System.out.print("Enter Gender: ");
student.setGender(sc.next());
System.out.print("Enter Department: ");
student.setDepartment(sc.next());
System.out.print("Enter Date of Birth (YYYY-MM-DD): ");
student.setDob(sc.next());
System.out.print("Enter Contact Number: ");
student.setContactnumber(sc.nextLong());
System.out.print("Enter CGPA: ");
student.setCgpa(sc.nextDouble());
System.out.print("Enter Number of Backlogs: ");
student.setNob(sc.nextInt());
t = s.beginTransaction();
s.save(student);
t.commit();
System.out.println("Inserted Data");
case 2:
System.out.print("Enter Student ID: ");
Long id = sc.nextLong();
Student fetchedStudent = s.get(Student.class, id);
if (fetchedStudent != null) {
System.out.println("ID: " + fetchedStudent.getId());
System.out.println("Name: " + fetchedStudent.getName());
System.out.println("Gender: " + fetchedStudent.getGender());
System.out.println("Department: " + fetchedStudent.getDepartment());
System.out.println("DOB: " + fetchedStudent.getDob());
System.out.println("Contact Number: " + fetchedStudent.getContactnumber());
System.out.println("CGPA: " + fetchedStudent.getCgpa());
System.out.println("Number of Backlogs: " + fetchedStudent.getNob());
} else {
System.out.println("Student not found");
}
break;
case 3:
System.out.print("Enter Student ID to update: ");
Long updateId = sc.nextLong();
Student updateStudent = s.get(Student.class, updateId);
if (updateStudent != null) {
System.out.print("Enter new Name: ");
updateStudent.setName(sc.next());
System.out.print("Enter new Gender: ");
updateStudent.setGender(sc.next());
System.out.print("Enter new Department: ");
updateStudent.setDepartment(sc.next());
System.out.print("Enter new Date of Birth (YYYY-MM-DD): ");
updateStudent.setDob(sc.next());
System.out.print("Enter new Contact Number: ");
updateStudent.setContactnumber(sc.nextLong());
System.out.print("Enter new CGPA: ");
updateStudent.setCgpa(sc.nextDouble());
System.out.print("Enter new Number of Backlogs: ");
updateStudent.setNob(sc.nextInt());
t = s.beginTransaction();
s.update(updateStudent);
t.commit();
System.out.println("Updated Data");
} else {
System.out.println("Student not found");
}
break;
case 4:
System.out.print("Enter Student ID to delete: ");
Long deleteId = sc.nextLong();
Student deleteStudent = s.get(Student.class, deleteId);
if (deleteStudent != null) {
t = s.beginTransaction();
s.delete(deleteStudent);
t.commit();
System.out.println("Deleted Data");
} else {
System.out.println("Student not found");
}
break;
case 5:
// HQL: Display all student records with all columns
List<Student> studentsAllColumns = s.createQuery("from Student", Student.class).list();
for (Student student : studentsAllColumns) {
System.out.println(student);
}
break;
case 6:
// HQL: Display all student records with specific columns
List<Object[]> studentsSpecificColumns = s.createQuery("select id, name, cgpa from
Student", Object[].class).list();
for (Object[] student : studentsSpecificColumns) {
System.out.println("ID: " + student[0] + ", Name: " + student[1] + ", CGPA: " + student[2]);
}
break;
case 7:
// HQL: Display names of students with CGPA greater than 7
List<String> studentNames = s.createQuery("select name from Student where cgpa > 7",
String.class).list();
for (String name : studentNames) {
System.out.println("Name: " + name);
}
break;
case 8:
// HQL: Delete a student by ID using parameter
System.out.print("Enter Student ID to delete using parameter: ");
Long deleteIdParam = sc.nextLong();
t = s.beginTransaction();
Query deleteQueryParam = s.createQuery("delete from Student where id = :id");
deleteQueryParam.setParameter("id", deleteIdParam);
int resultDeleteParam = deleteQueryParam.executeUpdate();
t.commit();
System.out.println("Number of records deleted: " + resultDeleteParam);
break;
case 9:
// HQL: Update student details by ID using parameter
System.out.print("Enter Student ID to update using parameter: ");
Long updateIdParam = sc.nextLong();
t = s.beginTransaction();
Query updateQueryParam = s.createQuery("update Student set name = :name, cgpa = :cgpa
where id = :id");
System.out.print("Enter new Name: ");
updateQueryParam.setParameter("name", sc.next());
System.out.print("Enter new CGPA: ");
updateQueryParam.setParameter("cgpa", sc.nextDouble());
updateQueryParam.setParameter("id", updateIdParam);
int resultUpdateParam = updateQueryParam.executeUpdate();
t.commit();
System.out.println("Number of records updated: " + resultUpdateParam);
break;
case 10:
// HQL: Aggregate functions on CGPA column
Query<Object[]> aggregateQuery = s.createQuery("select count(*), sum(cgpa), avg(cgpa),
min(cgpa), max(cgpa) from Student", Object[].class);
Object[] aggregateResults = aggregateQuery.uniqueResult();
System.out.println("Count: " + aggregateResults[0] + ", Sum: " + aggregateResults[1] + ", Avg:
" + aggregateResults[2] + ", Min: " + aggregateResults[3] + ", Max: " + aggregateResults[4]);
break;
case 11:
// HCQL: Display specific columns from student records
Criteria criteriaSpecificColumns = s.createCriteria(Student.class)
.setProjection(Projections.projectionList()
.add(Projections.property("id"))
.add(Projections.property("name"))
.add(Projections.property("cgpa")));
List<Object[]> specificColumnsList = criteriaSpecificColumns.list();
for (Object[] row : specificColumnsList) {
System.out.println("ID: " + row[0] + ", Name: " + row[1] + ", CGPA: " + row[2]);
}
break;
case 12:
// HCQL: Get 5th to 10th records
Criteria criteriaFifthToTenth = s.createCriteria(Student.class)
.setFirstResult(4) // 5th record (index starts from 0)
.setMaxResults(6); // Total 6 records (5th to 10th)
List<Student> fifthToTenthRecords = criteriaFifthToTenth.list();
for (Student student : fifthToTenthRecords) {
System.out.println(student);
}
break;
case 13:
// HCQL: Apply various comparisons on CGPA column
CriteriaBuilder cb = s.getCriteriaBuilder();
CriteriaQuery<Student> cq = cb.createQuery(Student.class);
Root<Student> root = cq.from(Student.class);
cq.where(predicate);
List<Student> comparisonResults = s.createQuery(cq).getResultList();
for (Student student : comparisonResults) {
System.out.println(student);
}
break;
case 14:
// HCQL: Get records ordered by Student Name
Criteria criteriaOrder = s.createCriteria(Student.class)
.addOrder(Order.asc("name")); // Ascending order
List<Student> ascendingOrderList = criteriaOrder.list();
for (Student student : ascendingOrderList) {
System.out.println(student);
}
criteriaOrder = s.createCriteria(Student.class)
.addOrder(Order.desc("name")); // Descending order
List<Student> descendingOrderList = criteriaOrder.list();
for (Student student : descendingOrderList) {
System.out.println(student);
}
break;
case 15:
System.out.println("Exiting...");
sc.close();
s.close();
sf.close();
return;
default:
System.out.println("Invalid choice");
break;
}
}
}
}
Hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate
Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property
name="connection.url">jdbc:mysql://localhost:3306/jfsd1</property>
<property name="connection.username">root</property>
<property name="connection.password">poushiM@1234</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping class="com.klu.EXP2.Student"/>
</session-factory>
</hibernate-configuration>
OUTPUT: