New->Hibernate Mapping Wizard(This file should in default package)
then add mapping class in to this XML(last section in XML file)
After this add Hibernate mapping wizard
set Hibernate mapping wizard as Users.hbm
class to map :com.Users(package_name.class_name)
select configure file:hibernate.cfg.xml
database table:Users(table_name)
Then click on xml view for Users.hbm add those steps
ADD Java file( Users.java)
package com;
public class Users {
private int id;
private String user;
private String pass;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
Then create Table
create table users
(
userid int auto_increment primary key,
user_name varchar(40),
pasword varchar(40)
)
Then Create servlet file(saveUser.java)
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
public class saveUser extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
String user=request.getParameter("userName");
String pass=request.getParameter("passName");
Users Objectuser = new Users();
Objectuser.setUser(user);
Objectuser.setPass(pass);
try
{
Transaction tx=null;
SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sf.openSession();
tx=session.beginTransaction();
session.save(Objectuser);
session.getTransaction().commit();
session.close();
out.println("Inserted");
}
catch (Exception e) {
System.out.println("Error :"+e.getMessage());
out.println("Error :"+e.getMessage());
}
}
}
Finally create JSP Page