Fetch Data into a table in swing using hibernate by R4R Team

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:

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.

Click below link to show the last example:

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).

Above selected code is shown below:

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("First name");
             th.add("last name");
             Criteria cr = s.createCriteria(r4r.InserSwingData.class);
             java.util.List<r4r.InserSwingData> ds = cr.list();
             for(r4r.InserSwingData dd : ds)
                 ow = new Vector<Object>();
             table.setModel(new DefaultTableModel(tableData, th));

Step 5. Now buid and run the application. 

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!