I wrote a small program to retrieve records in my email queue table to process and send email. While using JDBC to achieve this, as shown below:

  MySqlConnect con=new MySqlConnect();
    public PreparedStatement preparedStatement = null;

    public Connection con1 = con.connect();

    //pick up queue and send email
    public void email() throws Exception {


            while(true) {
                String sql = "SELECT id,user,subject,recipient,content FROM emailqueue WHERE status='Pending' ";
                PreparedStatement statement = con1.prepareStatement(sql);
                ResultSet rs = statement.executeQuery();

                while (rs.next()) {

                    String subject = rs.getString("subject");

                    String recipient = rs.getString("recipient");

                    String content = rs.getString("content");

                    String id = rs.getString("id");
                    String username = rs.getString("user");

                    String emailStatus = "DONE";
                    String errormsg=sendEmail(recipient, subject, content, id,username);
                    if (!errormsg.equals("")) {
                        emailStatus = "FAILED";

                    TerminalLogger.printMsg("Status  : " + emailStatus);



        }catch(Exception e){

            TerminalLogger.printMsg("Exception: "+e.toString());




The above works fine since it retrieve email records where the status is pending and pass the subject, content, recipient, etc to sendemail method and done.

I wanted to achieve the same goal but using JPA persistence instead. So I wrote this method:

public Object getrecords() {
        try {

            String sql = "select p.id,p.user,p.subject,p.recipient,p.content from Emailqueue p where " +

            List<Object[]> resList =(List<Object[]>) em.createQuery(sql).getResultList();
            if (resList == null) {
                throw new Exception("Error with selection query.");

            if (resList.size() > 0) {
                return resList;

           // msg = "Setting <" + name + "> not found.";

            return null;
        } catch (Exception e) {

            msg = CoreUtil.wrapMsg(CoreUtil.FUNC_ERROR,
                    this.getClass().getName(), "get(" + "Pending" + ")", e.getMessage());

            return null;

It has no issue retrieving the records and size in the table. And I called this method in the email(). Something like:

Object records = ejbCon.getSettingsFacade().getrecords();

From here, I can't figure how do I loop the records, to get each of the value in each field. For example, if there's 2 pending email records, I should need to retrieve each of its content, subject, recipient, etc and pass to sendemail method.

Also, retrieving these records is already taxing to my program since it takes long time to close the application compared to using JDBC (it's faster and I guess more efficient?).

So, I also need to consider the performance by doing using this method.


I should have clarified this better to explain what I'm trying to achieve. So I have that email queue table in this form:

id  user  subject content recipient           status
1   user1  test   example  [email protected]    Pending
2   user2  test2  example  [email protected]    Pending

So before this, I was using resultset in JDBC to obtain each individual field values for id:1 and pass them to the send method, and proceed with id:2 and do the same, an iteration. Now I want to achieve the same way by using the object I retrieve but I can't specify which fields value it's able to get. So I am stuck.

3 Answers

Zunaira Shafique On

There are many ways to achieve your desired result but the easiest way you can do is to create a parameterized constructor in your Emailqueue class and change your query to

    String sql = "select NEW 
                  Emailqueue(p.id,p.user,p.subject,p.recipient,p.content) from Emailqueue p 
    where " +
    List<Emailqueue> resList =(List<Emailqueue>) 

this way you can normally iterate over List using foreach loop.

윤현구 On

If i understood what is your correctly needs, Try to loop your resList. And i recommend to you don't get Object type.

List<Emailqueue> resList = em.createQuery(sql, Emailqueue.class).getResultList();

for (Emailqueue email : resList) {
     String subject = email.getSubject();
     // something do.

madqori On

Here the simple way to get data based on query.

  1. If you are using Entity Manager for query and use HQL, u can just simply return the class data. here is the sample :

TypedQuery q = man.createQuery("SELECT u FROM YourTable u WHERE u.col1 =:col1 and u.col2 =:col2", YourCalss.class) .setParameter("col1", col1) .setParameter("col2", col2); result = q.getResultList();

  1. If you want with custom result, then u can modify the current class of entity into another class.

List<CustomClass> result = null; EntityManager man = PersistenceUtilities.getEntityManagerFactory().createEntityManager(); try { TypedQuery q = man.createQuery("SELECT NEW " + CustomClass.class.getCanonicalName() + "(u.col1,u.col2) " + "FROM YourTable as u ", CustomClass.class); if (q.getResultList().size() > 0) { result = q.getResultList(); } } catch (Throwable t) { Main.logger.error(t.getMessage()); } finally { man.close(); } return result;