我有一个类 (SQLRequests),它连接到 SQL 数据库并从表中获取某些信息。它们存储在结果集中(rsUpdate 和 rsNew)。下面是方法,我添加了一些代码以确保提取正确的数据。
public void ProcessSQLUpdate (Connection conn)
{
try
{
Statement stmt = conn.createStatement();
String sql = SQLDataAdaptor.SELECT_PROCESS_SQL_UPDATE;
ResultSet rsUpdate = stmt.executeQuery(sql);
while(rsUpdate.next ())
{
System.out.println("Applix Number: " + rsUpdate.getString(2) + " " + ("Change: " + rsUpdate.getString(1)));
logger.info("Applix Number: " + rsUpdate.getString(2) + " " + ("Change: " + rsUpdate.getString(1)));
}
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}
我想通过不同类 (EmailSender) 中的电子邮件方法发送此信息,但我不知道如何将此信息添加到其中。
public void sendEmail () throws PollingException
{
Properties props = new Properties();
PollingProperties properties = PollingProperties.getInstance();
props.put("mail.smtp.host", (properties.getProperty(PollingProperties.POL_EMAIL_SMTP)));
Date date = new Date();
try {
Session mailSession = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(mailSession);
message.setSubject (properties.getProperty(PollingProperties.POL_EMAIL_SUBJECT));
message.setFrom(new InternetAddress(properties.getProperty(PollingProperties.POL_EMAIL_FROM)));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(properties.getProperty(PollingProperties.POL_EMAIL_TO)));
message.setText("Applix Update for " + date +
"\n\n New Rows: " [rsUpdate info here]+
"\n\n Updated Rows:");
Transport.send(message);
希望这是有道理的
最佳答案
您正在寻找的是 DTO: Data Transfer Object .
DTO 是一种设计模式,用于减少两层(或两个方法...)之间的调用冗余,方法是使用包含所有所需字段的对象,并作为参数传递,以避免多次调用或在目标方法上使用庞大的构造函数。
例如,如果您正在查询某人的详细信息,并想要调用一个方法来打印它们,您现在可以这样做:
// Query the db and fill the resultset, then
String firstName = rs.getString("firstName");
String lastName = rs.getString("lastName");
int age = rs.getString("age");
// close the connection, the resultset etc, and then
printPersonDetail(firstName); // first call
printPersonDetail(lastName); // second call
printPersonDetail(String.valueOf(age)); // another call
还有其他地方
private static void printPersonDetail(String something){
System.out.println(something);
}
<小时/>
使用 DTO,您可以创建一个反射(reflect)您需要表示的实体的对象,在本例中是人:
public Class PersonDTO{
String firstName;
String lastName;
int age;
/* Generate Getters and Setters with your IDE,
eg. in Eclipse: "ALT + SHIFT + S" -> "Generate Getters and Setters" */
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
和你的类(class)
// Query the db and fill the resultset, then
PersonDTO person = new PersonDTO();
person.setFirstName(rs.getString("firstName"));
person.setLastName(rs.getString("lastName"));
person.setAge(rs.getString("age"));
// close the connection, the resultset etc, and then
printPersonDetail(person); // only call: you are passing a DTO as parameter
还有其他地方
private static void printPersonDetail(PersonDTO person){
System.out.println(person.getFirstName());
System.out.println(person.getLastName());
System.out.println(person.getAge());
}
<小时/>
这是一个简单的例子,但我希望能帮助你理解这个想法。使用 DTO 设计更大的实体,并与其他类交换它们的值;仅在其中放入值,根本不添加任何逻辑,并固定您的方法来接收实体而不是接收单个值。
注意:完成后始终关闭结果集,始终在 getString()
中使用名称而不是索引,可能会使用本世纪的一些东西,例如 Spring 的 Resultsets 和 RowMappers。
关于java - 从另一个类java中的resultSet访问数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17947762/