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','admin@r4r.in','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" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
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:actionerror/>
<s:form action="login">
<s:textfield name="name" label="Name"/>
<s:password name="password" label="Password"/>
<s:submit value="Login"/>
</s:form>
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:
(1) HibernateUtil.java
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();
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in 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!