java-如何在JPA中为电子邮件返回多个列?
我编写了一个程序,让我的程序无休止地从我的java-如何在JPA中为电子邮件返回多个列?,java,jpa,Java,Jpa,我编写了一个程序,让我的程序无休止地从我的emailqueue表中提取记录,以便它可以处理记录并将电子邮件发送给外部用户。首先,代码如下: MySqlConnect con=new MySqlConnect(); public PreparedStatement preparedStatement = null; public Connection con1 = con.connect(); //pick up queue and send email
emailqueue
表中提取记录,以便它可以处理记录并将电子邮件发送给外部用户。首先,代码如下:
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();
}
如您所见,我使用JDBC驱动程序获取表中每一列的值,并将它们传递给sendmail
,在那里它可以发送电子邮件。当然,到目前为止,一切工作都完美无缺
现在,我想通过使用JPA实现同样的目标,因为我想了解更多关于JPA的信息,而且我对JPA是新手。因此,我在我的电子邮件表中添加了一个数据库映射,如下所示:
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();
}
Emailqueue.java
@Entity
@Table(name = "emailqueue")
public class Emailqueue {
private long ulnodeid;
private String user;
private String subject;
private String recipient;
private String content;
private String status;
@Id
@Column(name = "ID")
public long getUlnodeid() {
return ulnodeid;
}
public void setUlnodeid(long ulnodeid) {
this.ulnodeid = ulnodeid;
}
@Basic
@Column(name = "USER")
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
@Basic
@Column(name = "SUBJECT")
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
@Basic
@Column(name = "RECIPIENT")
public String getRecipient() {
return recipient;
}
public void setRecipient(String recipient) {
this.recipient = recipient;
}
@Basic
@Column(name = "CONTENT")
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Basic
@Column(name = "STATUS")
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Emailqueue that = (Emailqueue) o;
return ulnodeid == that.ulnodeid &&
user == that.user &&
Objects.equals(subject, that.subject) &&
Objects.equals(recipient, that.recipient) &&
Objects.equals(content, that.content) &&
Objects.equals(status, that.status);
}
@Override
public int hashCode() {
return Objects.hash(ulnodeid, user,subject,recipient,content,status);
}
}
我编写了一个使用Entity Manager从表中检索记录的方法,如下所示:
public Emailqueue getrecords() {
try {
String sql = "select object(o) from Emailqueue as o where " +
"status='" + "Pending" + "'";
List resList = em.createQuery(sql).getResultList();
if (resList == null) {
throw new Exception("Error with selection query.");
}
if (resList.size() > 0) {
return (Emailqueue) resList.get(0);
}
// 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;
}
}
尝试将查询调用替换为:
List res=em.createQuery(“从电子邮件队列e中选择e,其中e.status='Pending'”,
Emailqueue.class).getResultList();
这将返回作为Java对象(实体)的匹配实例列表,您可以通过调用getter/setter轻松处理这些实例,例如(使用问题中的sendmail
方法):
for(Emailqueue eq:res){
sendStatus=sendmail(eq.getRecipient()、eq.getSubject()、eq.getContent()、eq.getId()、eq.getUsername());
}
或者,最好将
sendmail
的签名更改为sendmail(Emailqueue eq)
,并调用方法中的getter。尝试将查询调用替换为:
List res=em.createQuery(“从电子邮件队列e中选择e,其中e.status='Pending'”,
Emailqueue.class).getResultList();
这将返回作为Java对象(实体)的匹配实例列表,您可以通过调用getter/setter轻松处理这些实例,例如(使用问题中的sendmail
方法):
for(Emailqueue eq:res){
sendStatus=sendmail(eq.getRecipient()、eq.getSubject()、eq.getContent()、eq.getId()、eq.getUsername());
}
或者,最好将
sendmail
的签名更改为sendmail(Emailqueue eq)
,并调用该方法中的getter。尝试将getrecord
方法替换为一些spring
方法-查看没有spring还有其他替代方法吗,也可以不使用jdbc,并且能够以其他方式获得结果集。尝试用一些spring
方法替换getrecord
方法-看看有没有其他没有spring的替代方法,也可以不使用jdbc,并且能够以其他方式获得结果集。我这样做了,但如何将参数传递给sendmessage?已更新答案。您的解决方案有效,但如何确保只要有新记录插入到emailqueue表中,该解决方案就会一直运行?如果有新记录插入到该表中,则需要重复运行该例程。如果此插入发生在应用程序内部,则可以使用应用程序范围的通知。如果它发生在外部,我想定期轮询可以完成这项工作。你可以看一看,例如。目前,我只是使用while(true)循环来保持它运行,但我担心当表没有记录时,它可能会对CPU造成负担,所以我添加了Thread.Sleep以最小化税收。我这样做了,但如何将参数传递给sendmessage?已更新答案。您的解决方案有效,但如何确保只要有新记录插入到emailqueue表中,该解决方案就会一直运行?如果有新记录插入到该表中,则需要重复运行该例程。如果此插入发生在应用程序内部,则可以使用应用程序范围的通知。如果它发生在外部,我想定期轮询可以完成这项工作。您可以看一看,例如。目前,我只是使用while(true)循环来保持它运行,但我担心当表没有记录时,它可能会对CPU造成负担,因此我添加了Thread.Sleep以最小化税收。