Anushka lakmal
Anushka lakmal

Reputation: 91

Forming Mail Body in Java

Currently working on a project where my DataRetrival class is to be set in the java Mail API body.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
//import com.mysql.jdbc.Statement;
public class DataRetrival {

    public List<Employee> javaData() {
        DatabaseConnect dc = new DatabaseConnect();
        Connection con;

        List<Employee> employees = new ArrayList<>();
        try {
            con = dc.connect();
            String query ="SELECT * FROM employee";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);

               while (rs.next())
                  {

                  Employee emp = new Employee();
                   emp.setID(rs.getInt("ID"));
                   emp.setEmployee_Number(rs.getString("Employee_Number"));
                   emp.setFirstName(rs.getString("FirstName"));
                   emp.setLastName(rs.getString("LastName"));
                   emp.setEmailAddress(rs.getString("EmailAddress"));
                   emp.setPdfName(rs.getString("PdfName"));
                   emp.setEmailAddress(rs.getString("Sup_EmailAddress")); 
                   employees.add(emp);


                   String employeeNumber = rs.getString("Employee_Number");
                   System.out.println(employeeNumber);

                  }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return employees;
    }

Created new instance like this.

DataRetrival dtr = new DataRetrival();

from this new instance or any other method, I want to fill the field of InterenetAddress.parse field which contains another class called JavaMail with the above field EmailAddress mentioned in code segment;

message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(Want get called Email_Adress));

Upvotes: 0

Views: 143

Answers (1)

Andreas
Andreas

Reputation: 159096

From comment:

i want to call javaData() and get getEmailAddress to another class

To call javaData():

DataRetrival dtr = new DataRetrival();
List<Employee> employees = dtr.javaData();

To get getEmailAddress:

for (Employee employee : employees) {
    String emailAddress = employee.getEmailAddress();
    // use value here
}

Both of the above constructs are core Java features, so I'd suggest you (re)read your Java guide on how to do method calls and how to iterate a list.

Upvotes: 1

Related Questions