Validation of the data using hibernate and servlet with jsp by
R4R Team
In the Hibernate we can validate the data by using the input on the jsp page and from the database table. In this process a user insert an data into the given text field of the jsp page and that data check from the database if text field value exists from the database then our action will perform which we want to do either action will not be processed.
We can do this type of activity by procedure in database and using a method and another is hibernate which help to make this action direct. To understand this we need to see the below given example which we have given:
For making the Validation Application we need to follow the some step which tells us how to work with the Hibernate.
Step 1. Follow the step of the last create application of multi table inserting database. That is shown in the below link.
Step 2. On that same application taken and create first of all database table One for store the direct value and second the login for the validation.
Table which we use in this application code is given below:
create table Reg1
(
id int not null primary key auto_increment,
fname varchar(50),
lname varchar(50),
quali varchar(50),
mid varchar(50),
uid varchar(50),
pass varchar(50)
)
create table Login1
(
id int not null primary key auto_increment,
mid varchar(59),
uid varchar(50),
pass varchar(50)
)
Step 3. Now create a jsp page like insert page Page. Code of that page is given below:
Step 5. Now we create the reverseeng file and POJO class as we have discussed in the last example of ours.
Step 6. Now we Prepare the action page for inserting the database Name is InsertData code of that action page is given below. Here in the given code we show the selecte page (Taking servlet action is shown in the last application(click here for last application to show how to take the servlet)):
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
Regi1 r = new Regi1(fname, lname, mid, uid, pass);
Login1 l = new Login1(mid, uid, pass);
s.save(r);
s.save(l);
tx.commit();
s.close();
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet InsertData</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet InsertData at " + request.getContextPath() + "</h1>");
// out.println("<h1><a href="login.jsp">click here for login</a>"+fname+"</h1>");
out.println("</body>");
out.println("</html>");
}
}
Step 7. Now build and run the application.
Step 8. Now create the action of the Login page using validation. Code of the action page is givne page is given below. Here we will take also servlet. (It is main selected part of the action)
int a = r4r.Admin.checkId(request.getParameter("uid"),request.getParameter("pass"));
if(a!=0)
try (PrintWriter out = response.getWriter()) {
out.println("ok");
}
else
try (PrintWriter out = response.getWriter()) {
out.println("not ok");
}
Step 9. For the connectivity we need to create a method page which hold the all conectivity panel of the action name is we use here admin java class. Which code we use in this class that is also given below:
public class Admin
{
public static int checkId(String u,String ps)
{
try
{
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
Transaction tr = s.beginTransaction();
//Criteria cr = s.createCriteria(r4r.Login1.class).add(Restrictions.eq( "mid", u ));
Query query = s.createQuery("select count(*) from Login1 where uid = "+u+" && pass = "+ps+"");
query.executeUpdate();
return query.executeUpdate();
}catch(Exception ex) { return 0; }
// return 0;
}
}
Setp 10. Right click on the node of the application and run the application of reaching on the Login page.
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!