Updation the data in the database table using Hibernate and swing by
R4R Team
In this tutorial of the hibernate we have learn how to insert data into the database and we have also learn about the how can we insert data into one or more than table. click here In all above example which we have already discussed that told us how to fetch data from the data base on the specific form of the Swing JFrame Form. But now we will learn how to update data form the database using the Hibernate and MYSQL database on the Netbeans. When we have learn to all operation insert,fetch, fetch specific then we can understand easyly to this example so let's start.
Here in this session we will create update data activity which we perform the swing using hibernate. In this tutorial we will learn how to fetch the data and update that data by the activity using Id which we make in the database table. To do this kind of the activity we need to perform by the NetBeans and using MySql.
Step 1. Now we for making the application we need to create a database into the MySql. Now we create the table for connectivity the application and update the database into that table.
Above image selected code is given here:
create table InsertSwingData
(
id int not null primary key auto_increment,
fname varchar(50),
lname varchar(50),
address varchar(50)
)
Step 2. First of all here we will take as we have already taken the application to create a action using netbeans. For this we need to go into the Filemenuwizard -> click new project -> select to the java and then java application -> put the requiredname on the given field -> click on finish.
Step 3. Now we need to create a hibernate configuration file to connectivity the database. Go into the filemenuwizard -> new file -> click on Hibernate and then click on hibernate configuration file -> select the database and then click on finish.
Above image code is 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 specific file. Go into the filemenuwizard -> click on new file -> java and then click on java package and put the required name on the given place.
Step 5. Now we need to create a hibernate reveng file. Go in the file menu wizard -> click on new file -> click on hibernate -> select to hibernate revenge file -> click on finsh.
Above image code is shown here 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="InserSwingData"/>
</hibernate-reverse-engineering>
Step 6. Now we need to create a POJO class. For this we need to go into the file menu wizard -> click on the new file -> select to hibernate -> click on Hibernate POJO class -> select to package -> click on finish.
1. InsertSwingData.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
public class InserSwingData implements java.io.Serializable {
private Integer id;
private String firstName;
private String lastName;
private String address;
public InserSwingData() {
}
public InserSwingData(String firstName, String lastName, String address) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
}
Step 7. Now we also requred to create Hibernate Util file. for this we need to go into the file menu wizard -> click on new file -> search to hibernate -> click on hibernate utill file -> click on finish.
Above image code is shown here below:
package r4r;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Step 8. Now we need to create a Swing JForm. For this we need to go into the File menu wizard -> click on new file -> click on Swing JForm -> put the required name on the place -> click on finish.
Step 9. Now we need to perform the submit action on the submit button.
Hibernate-application-using-swing-27.gif
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
InserSwingData i = new InserSwingData(fname.getText(), lname.getText(), address.getText());
JOptionPane.showMessageDialog(this, "One Record Inserted");
s.save(i);
tx.commit();
s.close();
clear();
}
void clear()
{
fname.setText("");
lname.setText("");
address.setText("");
}
Step 10. Now we need to create a another page for update the data into the database. As take for Step 6 .
Step 11. Now we need to create a java class for performing the action and working here for fetching the Id form the database for the update the data. For this we will go into the file menu wizard -> click on new file -> click on java -> click on java class -> put the name on the required place -> click on finish.
package hibernateapplicationusingswing.update;
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;
public class Admin
{
public static List<String> getId()
{
List<String> a = new ArrayList<String>();
try
{
SessionFactory sf = 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!