我正在编写一个充当服务的程序,并从电子邮件队列表中提取电子邮件,对其进行处理并将其发送出去。这是我做的事情,它确实工作正常。

MySqlConnect con = new MySqlConnect();
public PreparedStatement preparedStatement = null;
public Connection con1 = con.connect();

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

    try {

        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);
            }

            statement.close();
            rs.close();
        }

    } catch(Exception e) {
        e.printStackTrace();
        TerminalLogger.printMsg("Exception: "+e.toString());
    }

    con1.close();
    Thread.sleep(2000);
}


现在,我显然使用JDBC在循环中获取结果集并按如下所示对其进行处理。当然,我还需要在MySqlConnect.java属性中指定数据库连接。尽管所有这些工作都很好,但我想知道是否还有另一种方法可以在不使用JDBC的情况下实现相同的目标,即指定连接属性?

我当时在考虑Java持久性。我是新来的。

编辑

我被告知要使用JPA来实现这一目标,并且我是这样编写的:

public void email() throws Exception {

    try {

        while(true) {

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

            List<Object[]> list = em.createQuery(sql).getResultList();
            for (Object[] obj : list) {
                System.out.println(obj[0]);
                System.out.println(obj[1]);
                System.out.println(obj[2]);
                System.out.println(obj[3]);
                System.out.println(obj[4]);
            }
        }

    } catch(Exception e) {
        e.printStackTrace();
        TerminalLogger.printMsg("Exception: " + e.toString());
    }


从这里,我将需要的参数传递给方法。这种方法可行吗?

编辑2

如下所示是否有所不同:

String id = ejbCon.getSettingsFacade().getid();
String username = ejbCon.getSettingsFacade().getUser();
String subject = ejbCon.getSettingsFacade().getSubject();
String recipient = ejbCon.getSettingsFacade().getRecipient();
String content = ejbCon.getSettingsFacade().getContent();
String errormsg = sendEmail(recipient, subject, content, id,username);

public String getContent() {
    try {
        String sql="Select content FROM emailqueue WHERE status='Pending'";
        if (em == null) {
            throw new Exception("could not found subject");
        }

        return (String) em.createNativeQuery(sql).getSingleResult();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}


稍微了解一下该方法的外观,其他方法遵循相同的概念。

最佳答案

   List<EmailQueue> emailList = em.createQuery(sql).getResultList();
        for (EmailQueue obj : emailList) {
              String emailStatus = "DONE";
              String errormsg=sendEmail(obj.getRecipient(), obj.getSubject, obj.getContent(),obj.getId(),obj.getUsername());
                    if (!errormsg.equals("")) {
                        emailStatus = "FAILED"
                    }
                    TerminalLogger.printMsg("Status  : " + emailStatus);
        }

    }

10-06 09:54