Hibernate

adplus-dvertising
Fetch Data into a table in swing using hibernate
Previous Home Next

In this session we will learn how to fetch data from the database and how will show in a table using JFrame. Here in this session we will use Hibernate and swing with Netbeans and MySQL also will be workable here.

So as we have told that in swing we can insert data by the help of the hibernate. In the hibernate we all know that hibernate work with the POJO class that help us to make data to workable with the database

For show the data from the database on a table formate we need to follow the some basic step. Those step will tell us how to fetch data on a Form using hibernate. All those step are given below:

Step 1: Here we will use that application which we discussed in the last example of the swing. So open the same application in the panel of the netbeans and same database will be workable here. In our last session we learn how to insert data into one or more table using hibernate.

Step 2. Now create a JFrame Form under the hibernateapplicationwithswing. Go in the file menu wizard -> click on new file -> click on swing gui forms and then click on the JFrame Form -> click on next -> type the name of the given place (FetchData) -> click on finish.

Step 3. Now add a table in the Form. Go in the menu wizard -> click on Window -> IDE Tool -> Palette -> drag table on to the form. Now click on the properties -> click on model -> and delete all item which are listed in the table.Step 4. Now go in the source of the Jform. and edit (put the name of the table go in navigation and the put name of table).

Step 4. Now go in the source of the Jform. and edit (put the name of the table go in navigation and the put name of table).


private void tableAncestorAdded(javax.swing.event.AncestorEvent evt) {                                    
        SessionFactory sf = util.HibernateUtil.getSessionFactory();
             Session s = sf.openSession();
             Transaction tr = s.beginTransaction();
             Vector<String> th = new Vector<String>();
             Vector tableData = new Vector();
             Vector<Object> ow = new Vector<Object>();
             th.add("S.no");
             th.add("First name");
             th.add("last name");
             th.add("Address");
             
             Criteria cr = s.createCriteria(r4r.InserSwingData.class);
             java.util.List<r4r.InserSwingData> ds = cr.list();
            
             for(r4r.InserSwingData dd : ds)
                 
             {
                 ow = new Vector<Object>();
                 ow.add(dd.getId());
                 ow.add(dd.getFirstName());
                 ow.add(dd.getLastName());
                 ow.add(dd.getAddress());
                 tableData.add(ow);
             
             }
                
             table.setModel(new DefaultTableModel(tableData, th));
    }   

Step 5. Now buid and run the application.

Previous Home Next