Tuesday, November 16, 2010

Hibernate Insert records into table

Problem:

To insert data into student table in navalady database.

Solution:


Procedure:


Step 1:

Create Hibernate Reverse Engineering wizard

Step 2:

Create HibernateUtil.java for Creating SessionFactory.

I have created HibernateUtil.java named as NewHibernateUtil.java.

Include following coding into NewHibernateUtil.java

package Hiber;

import java.sql.*;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.io.*;

public class NewHibernateUtil
{
public static final SessionFactory sessionFact;
static
{
try
{
sessionFact = new Configuration().configure().buildSessionFactory();
}
catch(Throwable e)
{
System.out.println("SessionFactory creation failed." + e);
throw new ExceptionInInitializerError(e);
}
}
public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException
{
Session sess = (Session) session.get();
if(sess == null)
{
sess = sessionFact.openSession();
session.set(sess);
}
return sess;
}
public static void SessionClose() throws Exception
{
Session s = (Session) session.get();
if (s != null)
{
s.close();
session.set(null);
}
}
}

Step 3:
Create POJO class and mapping files

Step 4:

Create jsp file named as HCQinsert.jsp

Include following coding into the HCQinsert.jsp


< %@page import="java.util.Date"% >
< %@page import="Hiber.Student"% >
< %@page import="org.hibernate.*" % >
< %@page import="org.hibernate.cfg.*" % >
< %@page import="Hiber.NewHibernateUtil" % >
< html >
< head >
< title > Hibernate Native SQL-Insert into table < /title >
< /head >
< body >
< %! Session sz; % >
< %
try
{
sz=NewHibernateUtil.currentSession();
Transaction tz=sz.beginTransaction();

Student st=new Student();
st.setStuname("Nithya");
st.setStuno(1003);
st.setTamil(69);
st.setEnglish(64);
st.setMaths(90);
st.setScience(79);
st.setSocialscience(87);
Date d=new Date("1999/06/25");
st.setJoindate(d);
sz.save(st);

out.println("Successfully Inserted into the Student table in Navalady Datebase");
tz.commit();

}
catch(Exception e)
{
sz.close();

out.println("Error Occur");
}

% >
< /body >
< /html >

output



Now we have to check using mysql database using following query.

mysql > select * from student;

Monday, November 15, 2010

Hibernate Insert records into table



Problem:
               To insert data into student table in navalady database.

Solution:

Procedure:

Step 1:
            Create Hibernate Reverse Engineering wizard

Step 2:
            Create HibernateUtil.java for Creating SessionFactory.

            I have created HibernateUtil.java named as NewHibernateUtil.java.

            Include following coding into NewHibernateUtil.java

package Hiber;

import java.sql.*;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.io.*;

public class NewHibernateUtil
{
  public static final SessionFactory sessionFact;
  static
  {
    try
    {
      sessionFact = new Configuration().configure().buildSessionFactory();
    }
    catch(Throwable e)
    {
      System.out.println("SessionFactory creation failed." + e);
      throw new ExceptionInInitializerError(e);
    }
  }
  public static final ThreadLocal session = new ThreadLocal();

  public static Session currentSession() throws HibernateException
  {
    Session sess = (Session) session.get();
    if(sess == null)
    {
      sess = sessionFact.openSession();
      session.set(sess);
    }
    return sess;
  }
  public static void SessionClose() throws Exception
  {
    Session s = (Session) session.get();
    if (s != null)
    {
        s.close();
        session.set(null);
      }
  }
}

Step 3:
            Create POJO class and mapping files

Step 4:

            Create jsp file named as HCQinsert.jsp

Include following coding into the HCQinsert.jsp


&lt %@page import="java.util.Date"% &gt
&lt %@page import="Hiber.Student"% &gt
&lt %@page import="org.hibernate.*" % &gt
&lt %@page import="org.hibernate.cfg.*" % &gt
&lt %@page import="Hiber.NewHibernateUtil" % &gt
&lt html &gt
    &lt head &gt
        &lt title &gt Hibernate Native SQL-Insert into table &lt /title &gt
    &lt /head &gt
    &lt body &gt
        &lt %! Session sz; % &gt
        &lt %
            try
            {
               sz=NewHibernateUtil.currentSession();
               Transaction tz=sz.beginTransaction();
              
               Student st=new Student();
               st.setStuname("Nithya");
               st.setStuno(1003);
               st.setTamil(69);
               st.setEnglish(64);
               st.setMaths(90);
               st.setScience(79);
               st.setSocialscience(87);
               Date d=new Date("1999/06/25");
               st.setJoindate(d);
               sz.save(st);

               out.println("Successfully Inserted into the Student table in Navalady Datebase");
               tz.commit();
              
            }
            catch(Exception e)
            {
                sz.close();
               
                out.println("Error Occur");
            }
          
         % &gt
    &lt /body &gt
&lt /html &gt

Hibernate Criteria Query-OR Clause

Hibernate Criteria Query – Using OR clause.

Aim:
            To check more than one condition using OR clause.

Example:

Problem:

            To get student name who get tamil mark between greater than 50 or less than70.

Solution:

Step 1:
            Create Hibernate Reverse Engineering wizard

Step 2:
            Create HibernateUtil.java

Step 3:
            Create POJO class and mapping files

Step 4:

            Create jsp file named as OrExample.jsp

Include following coding into the OrExample.jsp


< %@page import="org.hibernate.*" % >
< %@page import="org.hibernate.cfg.*" % >
< %@page import="org.hibernate.criterion.*" % >
< %@page import="java.util.*" % >
< %@page import="Hiber.Student" % >
< html >
< head >
< title > Hibernate Criteria Query-Or Example < /title >
< /head >
< body >
< %
try
{
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session s=sessionFactory.openSession();

Criteria c=s.createCriteria(Student.class);
c.add(Expression.or(Expression.gt("tamil",new Integer(50)), Expression.lt("tamil", new Integer(70))));
List list=c.list();
Iterator it=list.iterator();
while(it.hasNext())
{
Student s1=(Student)it.next();
out.println(s1.getStuname());
}
}
catch(Exception e)
{

}
% >
< /body >
< /html >

Output










To check using mysql database.

Using following mysql query

mysql > select stuname from student where tamil >50 or tamil <70;

Sunday, November 14, 2010

Hibernate Criteria Query-Lessthan

Hibernate Criteria Expression- lessthan


Aim:
            To learn how to use lessthan in our hibernate.

Problem:

To get stuname from table student in navalady database who get less than 70 marks in tamil using lessthan criteria expression.

Solution:

Step 1:
            Create Hibernate Reverse Engineering wizard

Step 2:
            Create HibernateUtil.java

Step 3:
            Create POJO class and mapping files

Step 4:

            Create jsp file named as LessThanEx.jsp

Include following coding into the LessThanEx.jsp



< %@page import="org.hibernate.*" % >
< %@page import="org.hibernate.cfg.*" % >
< %@page import="org.hibernate.criterion.*" % >
< %@page import="java.util.*" % >
< %@page import="Hiber.Student" % >
< html >
< head >
< title > Hibernate Criteria Expression-Less Than < /title >
< /head >
< body >
< %
try
{
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session s=sessionFactory.openSession();

Criteria c=s.createCriteria(Student.class);
c.add(Expression.lt("tamil",new Integer("70")));

List list=c.list();
Iterator it=list.iterator();

while(it.hasNext())
{
Student s1=(Student)it.next();
out.println(s1.getStuname());
}


}
catch(Exception e)
{
out.println(e);
}
% >
< /body >
< /html >

Output:

Step 5:

Manually check using mysql query.

mysql > select stuname from student where tamil < 70;

Hibernate Criteria Query-GreaterThanEqual

Hibernate Criteria Expression- greater than and Equal


Aim:
            To learn how to use greater than and Equal in our hibernate.

Problem:

To get stuname from table student in navalady database who get greater than and Equal 70 marks in tamil using greater than and Equal criteria expression.

Solution:

Step 1:
            Create Hibernate Reverse Engineering wizard

Step 2:
            Create HibernateUtil.java

Step 3:
            Create POJO class and mapping files

Step 4:

            Create jsp file named as GreaterEqual.jsp

Include following coding into the GreaterEqual.jsp


< %@page import="org.hibernate.*" % >
< %@page import="org.hibernate.cfg.*" % >
< %@page import="org.hibernate.criterion.*" % >
< %@page import="java.util.*" % >
< %@page import="Hiber.Student" % >
< html >
< head >
< title > Hibernate Criteria Expression-GreaterThan Equal < /title >
< /head >
< body >
< %
try
{
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session s=sessionFactory.openSession();

Criteria c=s.createCriteria(Student.class);
c.add(Expression.ge("tamil",new Integer("70")));

List list=c.list();
Iterator it=list.iterator();

while(it.hasNext())
{
Student s1=(Student)it.next();
out.println(s1.getStuname());
}


}
catch(Exception e)
{
out.println(e);
}
% >
< /body >
< /html >


Output:



To check using mysql using following query

mysql > select stuname from student where tamil > =70;

Note:
All example programs in our blog are successfully run. So you copy program and paste to your IDE and run for your experience.

Hibernate Criteria Query-GreaterThan

Hibernate Criteria Expression- greater than


Aim:
            To learn how to use greater than in our hibernate.

Problem:

To get stuname from table student in navalady database who get greater than 70 marks in tamil using greater than criteria expression.

Solution:

Step 1:
            Create Hibernate Reverse Engineering wizard

Step 2:
            Create HibernateUtil.java

Step 3:
            Create POJO class and mapping files

Step 4:

            Create jsp file named as GreaterThan.jsp

Include following coding into the GreaterThan.jsp


< %@page import="org.hibernate.*" % >
< %@page import="org.hibernate.cfg.*" % >
< %@page import="org.hibernate.criterion.*" % >
< %@page import="java.util.*" % >
< %@page import="Hiber.Student" % >
< html >
< head >
< title > Hibernate Criteria Expression-Greater Than < /title >
< /head >
< body >
< %
try
{
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session s=sessionFactory.openSession();

Criteria c=s.createCriteria(Student.class);
c.add(Expression.gt("tamil",new Integer("70")));

List list=c.list();
Iterator it=list.iterator();

while(it.hasNext())
{
Student s1=(Student)it.next();
out.println(s1.getStuname());
}


}
catch(Exception e)
{
out.println(e);
}
% >
< /body >
< /html >

Output:


To check using mysql using following query

mysql > select stuname from student where tamil > 70;

Hibernate Criteria Expression- lessthanequal

Hibernate Criteria Expression- lessthanequal


Aim:
            To learn how to use lessthanequal in our hibernate.

Problem:

To get stuname from table student in navalady database who get less than and equal 77 marks in tamil using lessthan criteria expression.

Solution:

Step 1:
            Create Hibernate Reverse Engineering wizard

Step 2:
            Create HibernateUtil.java

Step 3:
            Create POJO class and mapping files

Step 4:

            Create jsp file named as LessThanEq.jsp

Include following coding into the LessThanEq.jsp



< %@page import="org.hibernate.*" % >
< %@page import="org.hibernate.cfg.*" % >
< %@page import="org.hibernate.criterion.*" % >
< %@page import="java.util.*" % >
< %@page import="Hiber.Student" % >
< html >
< head >
< title > Hibernate Criteria Expression-Less Than and Equal < /title >
< /head >
< body >
< %
try
{
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session s=sessionFactory.openSession();

Criteria c=s.createCriteria(Student.class);
c.add(Expression.le("tamil",new Integer("77")));

List list=c.list();
Iterator it=list.iterator();

while(it.hasNext())
{
Student s1=(Student)it.next();
out.println(s1.getStuname());
}


}
catch(Exception e)
{
out.println(e);
}
% >
< /body >
< /html >

Output:


To manually check using mysql query.

mysql > select stuname from student where tamil < =77;