Fetch Data on to a specific field and insert data into the database using hibernate by
R4R Team
In hibernate when we want to fetch data on a specific field of the form then we need to perform some basic actions those we will discussed here. In hibernate we perform all the required action using database without sql procedure insted of we use the POJO class. By the help of the pojo class we perform these type of the action. For the making this type of the action we need to follow the some basic stpe which we have given below:
Step 1. Take a project of java application basically we take like HibernateApplicationUsingSwing as we discussed in our last example.
Step 2. Now All Of First We Need To Create A Package To Store The Our Pojo Class. Go In File Menu Wizard -> Click On New File -> Search Java In The Table And Opposite Side Package -> Put The Name On The Required Place -> Click On Finish.
Step 3. Now We Need To Add The Hibernate Cfg File In Our Project For The Connectivity Of The Database. Go In The File Menu Wizard -> New File -> Click On Hibernate (Search In Table) -> Hibernate Cfg File Click -> Connect The Database -> Finish.
Above code is shown 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 Jframe Form . Go In File Menu Wizard -> New File -> Click On Swing Jframe Form -> Jfrme Form -> Put The Required Name On The Place -> Select To Package -> Click On Finish.
Step 5. Now Create A Table Into The Database.
Above image code is given below:
create table FetchCombo
(
id int not null auto_increment primary key,
fname varchar(50),
address varchar(50),
state varchar(50),
city varchar(50),
pin int(10)
);
Above image code is below:
create table tblcity
(
CITY_NAME varchar(50) DEFAULT NULL primary key,
STATE_NAME varchar(40) DEFAULT NULL
);
Above image code is below:
CREATE TABLE tblstate
(
STATE_NAME varchar(40) not null,
PRIMARY KEY ('STATE_NAME')
);
All both tblstate and tblcity insert code of database query download form below link:
Step 6. Now We Need To Create A Reverse Eng File. Go In File Menu Wizard -> New File -> Search To Hibernate -> Click On Reverse Eng File -> Select To Table -> Click On Finsh.
Above image code is 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="FetchCombo"/>
<table-filter match-name="tblcity"/>
<table-filter match-name="tblstate"/>
</hibernate-reverse-engineering>
Step 7. Now We Need To Create Pojo Class. Go In File Menu Wizard -> Click On Hibernate -> Click On Pojo Class -> Select To Package -> Click To Finish.
Above image code is below:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
public class Tblstate implements java.io.Serializable {
private String stateName;
public Tblstate() {
}
public Tblstate(String stateName) {
this.stateName = stateName;
}
public String getStateName() {
return this.stateName;
}
public void setStateName(String stateName) {
this.stateName = stateName;
}
}
Step 8. Here We Requreid One More Action File To Store The File Action. Create Name Admin Which Hold All Function. Go In File Menu Wizard -> Click On New File -> Click On Java -> Click On Java Class -> Put The Required Name On The Place -> Click On Finish.
Above image code is shown here below:
package r4r;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
public class Admin
{
public static List<String> getState()
{
List<String> a = new ArrayList<String>();
try
{
SessionFactory sf = new Configuration().configure().buildSessionFactory();
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
Transaction tr = s.beginTransaction();
FetchCombo fc = new FetchCombo(fname.getText(), address.getText(), state.getSelectedItem().toString(), city.getSelectedItem().toString(), Integer.parseInt(pin.getText()));
JOptionPane.showMessageDialog(this, "One Record Inserted");
s.save(fc);
tr.commit();
s.close();
s.clear();
}
Step 9. Now build and run the application.
Now check the database table is record inserted or not. which is given below:
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!