Here We are going to lean first hibernate sample application
we can any database like oracle,mysql,sqlserver...etc
But here we are using mysql with database name is abc.
HibernateConfiguration file:
hiberante.cfg.xml
we can any database like oracle,mysql,sqlserver...etc
But here we are using mysql with database name is abc.
HibernateConfiguration file:
hiberante.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/abc</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="emp.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Hibernate Mapping file:
emp.hbm.xml
emp.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="emp1"
table="emp1">
<id name="empno" column="empno">
<generator
class="assigned"></generator>
</id>
<property name="ename" column="ename"></property>
<property name="sal" column="sal"></property>
</class>
Hibernate POJO class:
emp1.java
emp1.java
public class emp1 {
private int empno,sal;
private String ename;
public void setEmpno(int empno) {
this.empno = empno;
}
public int getEmpno() {
return empno;
}
public void setSal(int sal) {
this.sal = sal;
}
public int getSal() {
return sal;
}
public void setEname(String
ename) {
this.ename = ename;
}
public String getEname() {
return ename;
}
}
Client application:
client.java
client.java
public class
client {
public static
void main(String arg[]) throws Exception
{
Configuration cfg=new Configuration();
cfg=cfg.configure("hibernate.cfg.xml");
SessionFactory sf=cfg.buildSessionFactory();
Session s=sf.openSession();
Transaction t=s.beginTransaction();
emp1 obj=new emp1();
obj.setEmpno(3);
obj.setEname("abc");
obj.setSal(5000);
s.save(obj);
t.commit();
s.clear();
}
}
if you create above sample project in editplus or notepad place all files in source folder only..
i.e
myapp(floder)
|
|--->hibernate.cfg.xml
|--->emp.hbm.xml
|--->emp1.java
|--->client.java
Complie the code:
d:\java\hibernate>javac emp1.java
d:\java\hibernate>javac client.java
execute the code
d:\java\hibernate>java client
for sample project click here
Add following jar filesantlr-2.7.6.jar
asm-3.1.jar
cglib-2.2.jar
commons-collections-3.1.jar
commons-logging-1.0.4.jar
commons-logging-1.0.4.jar
dom4j.1.6.1.jar
hibernate3.jar
jta-1.1.jar
slf4j-api-1.6.1.jar
slf4j-simple-1.6.4.jar
For Database if it is oracle you can add ojdbc14.jar
for MySql you can add mysql-5.1.10.jar
Configuration file for mysql
(if it is mysql database inside you have to create abc database)
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/abc</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="emp.hbm.xml"/>
</session-factory>
</hibernate-configuration>
For Retrieving The Data
public static void main(String[] args) {
For Retrieving The Data
public static void main(String[] args) {
Configuration cfg=new Configuration();
cfg=cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session s=sf.openSession();
emp1 obj=new emp1();
obj=(emp1)s.load(emp1.class, 1);
System.out.println("obj no is "+obj.getEmpno());
System.out.println("obj name is "+obj.getEname());
System.out.println("obj sal is "+obj.getSal());
}
Update Data from Database
public static void main(String arg[])
{
Configuration cfg=new Configuration();
cfg=cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session s=sf.openSession();
emp1 obj=new emp1();
obj=(emp1)s.load(emp1.class, 1);
obj.setEname("sri");
s.save(obj);
Transaction t=s.beginTransaction();
t.commit();
System.out.println("finished");
Delete Data from Database
public static void main(String arg[])
{
Configuration cfg=new Configuration();
cfg=cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session s=sf.openSession();
emp1 obj=new emp1();
obj=(emp1)s.load(emp1.class, 1);
s.delete(obj);
Transaction t=s.beginTransaction();
t.commit();
System.out.println("finished");
}
No comments:
Post a Comment