CRUD Operation using Hibernate Session1

0


hibernate.cfg.xml:-
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hdb1</property>
<property name="connection.username">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
 <mapping resource="hibernate.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
hibernate.hbm.xml:-
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="scs.Empolyee" table="tbl_emp">
<id name="empid" column="empid">
<generator class="assigned"></generator>
</id>
<property name="empname" />
</class>
</hibernate-mapping>
POJO CLASS:-
package scs;
public class Employee {
   private int empid;
   private String empname;
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}
 }
Empmain.java:-
package scs;
import java.util.Scanner;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class EmpMain {
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sf = cfg.buildSessionFactory();
Session ses = sf.openSession();
Empolyee obj = new Empolyee();
Scanner sc = new Scanner(System.in);
System.out.println("Enter id for insertion ");
int id=sc.nextInt();
System.out.println("Enter name for insertion ");
String sname=sc.next();
obj.setEmpid(id);
obj.setEmpname(sname);
Transaction tx = ses.beginTransaction();
ses.save(obj);
tx.commit();
ses.close();
System.out.print("Data Submitted Successfully");

}
}
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

package scs;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class ShowEmployee {
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sf = cfg.buildSessionFactory();
Session ses = sf.openSession();
Query q = ses.createQuery("from Empolyee e");
List lst = q.list();
Iterator it = lst.iterator();
while(it.hasNext())
{
Empolyee obj = (Empolyee)it.next();
System.out.println(obj.getEmpid()+" ,"+obj.getEmpname());
}
ses.close();
}
}
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

package scs;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import java.util.*;
public class UpdateEmployee {
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sf = cfg.buildSessionFactory();
Session s = sf.openSession();
Empolyee obj = new Empolyee();
Transaction tx = s.beginTransaction();
Scanner sc = new Scanner(System.in);
System.out.println("Enter id for updation ");
int id=sc.nextInt();
System.out.println("Enter name for updation ");
String sname=sc.next();
obj.setEmpid(id);
obj.setEmpname(sname);
s.update(obj);
tx.commit();
s.close();

}
}
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

package scs;
import java.util.Scanner;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class DeleteEmployee {
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sf = cfg.buildSessionFactory();
Session ses = sf.openSession();
Empolyee obj = new Empolyee();
Transaction tx = ses.beginTransaction();
Scanner sc = new Scanner(System.in);
System.out.println("Enter id for Deletion ");
int id=sc.nextInt();
Object o = ses.get(Empolyee.class, id);
Empolyee emp = (Empolyee)o;
ses.delete(emp);
tx.commit();
ses.close();

}
}
Tags

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)