2

I have a class (SQLRequests) which connects to a SQL database and gets certain information from a table. These are stored in a resultSet (rsUpdate and rsNew). Below is the method, I've added a bit of code to make sure that correct data is being pulled out.

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

I want to send this information in an email method in a different class (EmailSender) but I can't work out how to add this information into it.

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

Hope that makes sense

2
  • format your code properly
    – exexzian
    Jul 30, 2013 at 13:03
  • 1
    What do you want to send ? You can send the necessary data to the relevant method as a parameter , perhaps a Map !
    – AllTooSir
    Jul 30, 2013 at 13:05

1 Answer 1

1

What you are looking for is a DTO: Data Transfer Object.

DTO is a design pattern used to reduce the redundancy of calls between two layers (or two methods...), by using an object containing all the fields needed, passed as parameter to avoid making multiple calls or having huge constructors on the destination method.

For example, if you are querying for a person details, and want to call a method to print them, you now would do:

// 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

and somewhere else

private static void printPersonDetail(String something){
    System.out.println(something);
}

With DTO instead, you create an object reflecting the entity you need to represent, in this case the person:

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

and from your 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

and somewhere else

private static void printPersonDetail(PersonDTO person){
    System.out.println(person.getFirstName());
    System.out.println(person.getLastName());
    System.out.println(person.getAge());
}

This is a trivial example but I hope will help you get the idea. Use DTOs to design bigger entities, and exchange their values with other classes; put only values in them and no logic at all, and firm your methods to receive the entities instead of receiving the single values.

Note: always close resultset once you're done, always use names instead of indexes in getString(), possibly use something of this millennium, like Spring's Resultsets and RowMappers.

More info on DTO from MSDN

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.