Inserting Data into One or More than One table in the database Using Hibernate and JSP by
R4R Team
Here we will discuss that how to work with the struts and hibernate. We can work with the hibernate and struts together. One example we will discuss here below. We will use here NetBenas and Mysql with Hibernate. We follow the some basic step that are given below:
Step 1. First of all we need to create a web application for this we go in the file menu wizard -> click on the new Project -> click on java web and then java web application -> put the name on the required field -> select the server which we want to use here to build the project -> first select to hibernate and then select to struts framework. (if we do not add the struts framework then we can use this by the adding jar file directly) -> now click on finish.
Above code is given below:
<?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 to create a package to store the action and pojo class. for this we need to go in the file menu wizard and -> click on new file -> search java and the opposite side click on package -> type the name of the package which we want -> click on finish.
Step 5. Now we need to create reverse eng file . Go in the file menu wizard -> new file click on -> hibernate and reverse eng file -> select table which we created in the database -> click on finsih.
Above selected code is given below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
<schema-selection match-catalog="hibernate_pro"/>
<table-filter match-name="AddStruts"/>
</hibernate-reverse-engineering>
Step 5. Now create a pojo class on the created package. click on file menu wizard -> new file -> hibernate -> hibernate pojo class -> select package -> click on finish.
AddStrust.hbm.xml
Above code is given below:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
public class AddStruts implements java.io.Serializable {
private Integer id;
private String fname;
private String lname;
private String mob;
private String address;
private String state;
private String city;
private Integer pin;
public AddStruts() {
}
public AddStruts(String fname, String lname, String mob, String address, String state, String city, Integer pin) {
this.fname = fname;
this.lname = lname;
this.mob = mob;
this.address = address;
this.state = state;
this.city = city;
this.pin = pin;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFname() {
return this.fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return this.lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getMob() {
return this.mob;
}
public void setMob(String mob) {
this.mob = mob;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
public Integer getPin() {
return this.pin;
}
public void setPin(Integer pin) {
this.pin = pin;
}
}
Step 6. Now we need to create a java class for the creating the session with the hibernate. go in file menu wizard -> click on new file -> java -> java class -> put the name on the required field -> select to package -> finish.
Step 6. Now we need to create a action class. go in file menu wizard -> click on new file -> java -> java class -> put the name on the required field -> select to package -> finish.
above selected code is shown here.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package R4R_Dao;
/**
*
* @author sarvesh
*/
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import R4R.AddStruts;
public class AddData extends ActionSupport implements ModelDriven, Preparable {
AddStruts employee;
SessionFactory sessionFactory = null;
Session session = null;
@Override
public String execute() {
try {
sessionFactory = 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!