Monday 17 November 2014

Show records without class in hibernate and struts

Show records without class in hibernate and struts

Struts and Hibernate if you need to show all records from database table then you need to create a pojo class...in some cases you not need to use pojo class then how can you achieve to show all records? I have faced the same problem and overcome it with listed below code.


In query.hbm.xml file
<sql-query name="TestTestTest">
select user_name, user_id, password, cd, user_status,CAST(from_dt AS CHAR) as from_date from user_info
</sql-query>

In your action class

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.
HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.*;
import plugins.HibernateSessionFactory;

Session ssn = null;
ssn = HibernateSessionFactory.getSession();
ssn.setCacheMode(CacheMode.GET);
Query qry = ssn.getNamedQuery("TestTestTest");
List <Object[]>containerResults = qry.list();
for(java.lang.Object[] result : containerResults) {
    System.out.print("\n"+result[0]+"======"+result[1]+"======"+result[2]+"======"+result[3]+"======"+result[4]+"======"+result[5]);
}

No comments:

Post a Comment