New->Hibernate Mapping Wizard(This file should in default package)
then add mapping class in to this XML(last section in XML file)
Add->Java file ( into default package)
InsertUser.java
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
/**
*
* @author Gowthaman
*/
@Entity
@Table(name = "User", catalog = "Users")
public class InsertUser implements Serializable {
private static final long serialVersionUID = 1L;
@Id
// @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="user")
private String user;
@Column(name="password")
private String pass;
public void setUser(String user){
this.user=user;
}
public void setPass(String pass){
this.pass=pass;
}
}
This file map to hibernate that is called Annotation
create Database in MYSQL
schema as Users
create table user
(
user Varchar(25),
password Varchar(25)
);
Create anther Java file for insert data into database
New->Java File(add into default package)
AddUser.java
import javax.servlet.*;
import javax.servlet.http.*;
public class AddUser extends HttpServlet{
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
String user = request.getParameter("user").toString();
String pass = request.getParameter("pass").toString();
SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
Session s1 = sf.openSession();
InsertUser cs=new InsertUser();//create object for InsertUser.java
cs.setUser(user);
cs.setPass(pass);
s1.beginTransaction();
s1.save(cs);
s1.getTransaction().commit();
s1.close();
sf.close();
out.println("
" + insert successfully + "
"); }
catch (Exception e) {
e.printStackTrace();
out.println("error:- "+e.getMessage());
}
}
}
web.xml(edit XML file in web-inf folder add the code last section
index.jsp