Hibernate

adplus-dvertising
Deletion the data from the table Using Hibernate and swing
Previous Home Next

As we have discussed in the all above example the inserting data value process. Here we do as we done in our last examples. For checking the last example to inserting the value and displaying the value in the form we can also visit on the given page. For the visit on the last page of example we need to click on the given link that is as click here

Now here also we do the same thing for the deletion the operation. Here only we need to take a form to perform the action page and a java class to store the action method. And all other method will be same as we have discussed in out last solved example to inserting data and updating the data also given fetching the data on the form.

First of all we need to take the Swing JForm. Go into the File menu wizard -> new file -> put the name on the required field -> click on finsh. In this form we desing the form as we want to show on the our form also remember that value will be taken which we have taken into the Database table.

Now we need to perform the action code on the form. that is given below:

Admin.java//it shows the method to fethc the id of the inserted data form the database of the table.


 package swing.delete;

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();
        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)
        {
            //a.add(dd.getId());   
            a.add(Integer.toString(dd.getId()));
           
         }   
     }catch(Exception ex) { a.add(ex.getMessage()); }
     return a;
   }
 
    
}

DeletePage.java//this page will be taken in the project applicaation

//Below given code will help to fetch Id form the database.


public DeletePage() {
        initComponents();
        fname.setEditable(false);
        lname.setEditable(false);
        address.setEditable(false);
        List<String> a = Admin.getId();
           for(String s : a)
           Id.addItem(s);

//Below given code will help to perform the Id event change action


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);          
            fname.setText(r.getFirstName());
                lname.setText(r.getLastName());
                address.setText(r.getAddress());
                        
                                    
    }  

// Below given code will help us the make the action on the button delete which shows in the image


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
               // Id.removeAllItems();
                int n = new Integer(Id.getSelectedItem().toString());
                SessionFactory sf = r4r.HibernateUtil.getSessionFactory();
                Session s = sf.openSession();
                Transaction tr= s.beginTransaction();
                r4r.InserSwingData r = (r4r.InserSwingData)s.get(r4r.InserSwingData.class, n);
                 s.delete(r);
                tr.commit();
                JOptionPane.showMessageDialog(this, "One Record Deleted");
                clear();
    }                                        
       void clear()
       {
        //Id.removeAllItems();
        fname.setText("");
        lname.setText("");
        address.setText("");
       }

Now we need to build and run the project.

Previous Home Next