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 File menu wizard -> click new project -> select to the java and then java application -> put the required name 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 file menu wizard -> 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">
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_pro</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <mapping resource="r4r/InserSwingData.hbm.xml"/>

Step 4. Now we need to create a package to store the specific file. Go into the file menu wizard -> 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">
  <schema-selection match-catalog="hibernate_pro"/>
  <table-filter match-name="InserSwingData"/>

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"
<!-- Generated 29 Dec, 2014 5:40:31 AM by Hibernate Tools 4.3.1 -->
    <class name="r4r.InserSwingData" table="InserSwingData" catalog="hibernate_pro" optimistic-lock="version">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        <property name="firstName" type="string">
            <column name="first_name" length="40" />
        <property name="lastName" type="string">
            <column name="last_name" length="40" />
        <property name="address" type="string">
            <column name="address" length="40" />

2. InsertSwingData.java

package r4r;
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();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
    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.


       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");
        void clear()

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>();   
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session s = sf.openSession();
        Transaction tr = s.beginTransaction();
        Criteria cr = s.createCriteria(r4r.InserSwingData.class);
        java.util.List<r4r.InserSwingData> ds = cr.list();
        for(r4r.InserSwingData dd : ds)
     }catch(Exception ex) { a.add(ex.getMessage()); }
     return a;

Step 12. Now we need to perform the action on the update button.

// Below code will help to fetch the Id from the database.
 public Update() {
       List<String> a = Admin.getId();
           for(String s : a)

//Below code will help us to make the action for Id event Change

 private void IdActionPerformed(java.awt.event.ActionEvent evt) {                                   
            Id = (JComboBox) evt.getSource();
            Object selected = Id.getSelectedItem();
            int n = new Integer (Id.getSelectedItem().toString());
            SessionFactory sf = r4r.HibernateUtil.getSessionFactory();
            Session s = sf.openSession();
            Transaction tx = s.beginTransaction();
            InserSwingData r = (InserSwingData)s.get(r4r.InserSwingData.class, n);          

//Below code help to make the action of update button for value update

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       if (!validateFields()) {
        int n = new Integer (Id.getSelectedItem().toString());
    String fn = fname.getText();
    String ln = lname.getText();
    String add = address.getText();
    SessionFactory sf = r4r.HibernateUtil.getSessionFactory();
    Session s = sf.openSession();
    Transaction tr = s.beginTransaction();
    InserSwingData r = (InserSwingData)s.get(r4r.InserSwingData.class, n);          
        //int sal = Integer.parseInt(request.getParameter("t4"));
        JOptionPane.showMessageDialog(this, "Record Updated");
       new Update().setVisible(true);

Step 13. Now build and run the application.

Step 14. Now click on update button.

Leave a Comment:
R4R Team
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!