Validate data from the database using Hibernate and Struts by R4R Team

Hibernate is one of the best Java ORM tool in the current market. Recently I have written a lot for hibernate framework. So here we will discuss about the hibernate and struts application using mysql database. as we have discussed that Hibernate support the all activity using insert, update, delete, and it is also work for the many to one query and also work one to many query. For making the simple example we have to use the NetBeans. and also required to follow the some basic step which i have discussed here to create a validation application on the NetBeans togethar hibernate and struts.

Step 1. First of all we need to create a database and a table in the the MySql Database. Each and every application which is connected to the database we have to first create his own database and which type of activity we want to use in the application that should be done in the database.

(a) create database r4r

(b) create table ValidateData
id int not null primary key auto_increment,
uname varchar(10),
pass varchar(10),
email varchar(50) unique,
dor date

(c) now we need to inset value into this table

insert into ValidateData (uname,pass,email,dor) values ('admin','admiin','','2015-02-11');

Step 2. On the second step we need to do is that Take a new simple project in the netbeans put the name of the project which you want.

Step 3. Now take the hibernate configure file in the project. Go in the menu click on the new file search the hibernate and click on the hibernate configure file and select the database which we created and then finish.

After the finish we find the below code of the hibernate configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "">
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/r4r?zeroDateTimeBehavior=convertToNull</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <mapping resource="r4r/Users.hbm.xml"/>

Step 4. Now we need the jar file of the struts2 to attatch in the project. 

Step 5. Now we need to create a jsp file by going in the menu and taking the new file and select to the web and then jsp page. It work for only the desigin part. As we want to show the first part that can we can do. put the requried name on given place and finish.

after the change into the code we found the below code(index.jsp):

<%@taglib uri="/struts-tags" prefix="s"%>
<s:form action="login">
    <s:textfield name="name" label="Name"/>
    <s:password name="password" label="Password"/>
    <s:submit value="Login"/>

Step 6. Now we need to create two package one to store the action class of java of the application and anothre for the pojo class of the hibernate.

Step 7. Now we need to create hibernate revenge file to process the action which we want to perform. The all pojo class will be store inside the r4r_pojo package.

Step 8. Now we can create a three java class to perform the action.

That will be as shown below:


package dao;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
    private static final SessionFactory sessionFactory=buildSessionFactory();
   public static SessionFactory buildSessionFactory(){
        try {
            return new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
    public static SessionFactory getSessionFactory() {
        return sessionFactory;


package dao;

import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
    private static final long serialVersionUID = 1L;
    private String name;
    private String password;
    User_DAO dao=new User_DAO();
    public void validate(){
            this.addFieldError("name", "Name is required");
            this.addFieldError("password", "Password is required");
    public String execute(){
            return "success";
            this.addActionError("Invalid username and password");
            return "error";
    public String getName() {
        return name;
    public void setName(String name) { = name;
    public String getPassword() {
        return password;
    public void setPassword(String password) {
        this.password = password;


package dao;

import r4r.Users;
import java.util.Iterator;
import java.util.List;
import org.hibernate.*;
public class User_DAO {
    public boolean find(String name, String password) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String SQL_QUERY = " from Users u where u.uname='" + name + "' and u.pwd='" + password + "'";
        Query query = session.createQuery(SQL_QUERY);
        Iterator<Users> it = query.iterate();
        List<Users> list = query.list();
        if (list.size() > 0) {
            return true;
        return false;

Step 9. Now we create the struts xml file to process the action. It always lives into the default package.

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

<!-- Author: sarvesh -->
    <package name="default" extends="struts-default">
        <action name="login" class="dao.LoginAction">
            <result name="success">/hello.jsp</result>
            <result name="input">/index.jsp</result>
            <result name="error">/index.jsp</result>

Step 10. Now here we also required an another file of jsp. procedure will be same as we discussed in the last step.

This will work when our action will got sucess(hello.jsp).

<%@taglib uri="/struts-tags" prefix="s"%>
<b>Welcome,<s:property value="name"/></b>

Step 11. At last of the application one work is be done that is the build and run the projcet.

Download as you want to download about the application example:

Leave a Comment:
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!