2013-07-30 32 views
1

我有一個類(SQLRequests),它連接到SQL數據庫並從表中獲取某些信息。這些存儲在結果集(rsUpdate和rsNew)中。下面是方法,我添加了一些代碼以確保正確的數據被抽出。在另一個類java中訪問resultSet中的數據

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

希望是有道理的

+0

格式的代碼正確 – exexzian

+1

你要什麼派?您可以將必要的數據作爲參數發送到相關方法,也許是一個Map! – NINCOMPOOP

回答

1

你所尋找的是一個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代替,您創建了一個反映您需要表示的實體的對象,在此情況下爲人:

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

,並從你的類

// 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設計更大的實體,並與其他類交換其值;只在它們中放入數值,根本沒有邏輯,並堅定你的方法來接收實體而不是接收單個值。

注:始終關閉ResultSet一旦你完成,始終使用名稱,而不是指標在getString(),可能利用這個千年的東西,像的結果集和RowMappers。

More info on DTO from MSDN

相關問題