因此,我正在编写一个充当服务的程序,并从电子邮件队列表中提取电子邮件,对其进行处理并将其发送出去。这是我做的事情,它确实可以正常工作。
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;
}
只知道该方法的外观,其他方法遵循相同的概念。
答案 0 :(得分:1)
使用JPA之前,您必须先阅读WHY JPA
如以上评论所述,Spring Batch和Spring JPA是您的用例的不错选择, 您可以在互联网上关注和研究有关内容,并关注官方文件
学习愉快,希望更多的用户会建议其他好的选择,您可以从中选择并应用于您的用例,评估他们的利弊
答案 1 :(得分:1)
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);
}
}