Let us create a hibernate web Application. Here we need JSP file for presentation.
There are steps given below for the web app creation.
index.jsp
This page will show a registration form. It takes the input from the form and sends it to the register.jsp page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<form action="register.jsp" method="post"> Name: <input type="text" name="name"/><br><br/> Mobile No: <input type="text" name="phnno"/><br></br> Password: <input type="password" name="password"/><br><br/> Email ID: <input type="text" name="email"/><br><br/> <input type="submit" value="register"/> </form> |
register.jsp
The register.jsp file will get all the information from index.jsp and stores it in the object (s) of Student class.
1 2 3 4 5 6 7 8 9 10 11 12 |
<%@pageimport="com.hibernate.webap.StudentDao"%> <jsp:useBean id="s" class="com.hibernate.webap.Student"> </jsp:useBean> <jsp:setProperty property="*" name="s"/> <% int i=StudentDao.register(s); if(i>0) out.print("Registration is successfull"); %> |
Student.java
It is a bean class. All the variables of bean class are set as private with public setter and getter methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
public class Student { private int id,phnno; private String name,email,password; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getPhnno() { return phnno; } public void setPhnno(int phnno) { this.phnno = phnno; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } |
StudentDao.java
It contains the method to store the object of Student class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import org.hibernate.cfg.Configuration; import org.hibernate.Session; import org.hibernate.SessionFactory; public class StudentDao { public static int register(Student student){ int i=0; Configuration cfg= new Configuration(); cfg.configure("hibernate.cfg.xml"); SessionFactory factory= cfg.buildSessionFactory(); Session session = factory.openSession(); session.beginTransaction(); i=(Integer)session.save(student); session.getTransaction().commit(); session.close(); return i; } } |
student.hbm.xml
It maps the object of Student class with the database table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 5.3//EN" "<a href="http://hibernate.sourceforge.net /hibernate-mapping-5.3.dtd"> http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd</a>"> <hibernate-mapping> <class name="com.hibernate.webap.Student" table="student001"> <id name="id"> <generator class="increment"></generator> </id> <property name="name"></property> <property name="password"></property> <property name="phnno"></property> <property name="email"></property> </class> </hibernate-mapping> |
hibernate.cfg.xml
It contains information about the mapping file and relational database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 5.3//EN" "http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.dialect">org.hibernate.dialect .MySQL5Dialect</property> <property name="hibernate.connection.driver_class">com.mysql. jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:// localhost:3306/test2</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <mapping resource="student.hbm.xml"></mapping> </session-factory> </hibernate-configuration> |
Output

