0

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));
  • 1
    Unclear what you're asking. Are you asking how to call the `javaData()` method? Are you asking how to loop over the `Employee` objects returned by that method? Are you asking how to call the `getEmailAddress()` method on such an `Employee` object? What *specifically* is stopping you? --- [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/q/284236/5221149) – Andreas Aug 27 '18 at 16:17
  • *FYI:* Your code is never closing any of the JDBC objects returned, so you are leaking resources like crazy. Use [try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) to correctly close `ResultSet`, `Statement`, and `Connection` objects. – Andreas Aug 27 '18 at 16:19
  • Sir, i want to call javaData() and get getEmailAddress to another class. – Anushka lakmal Aug 27 '18 at 16:24
  • @Andreas. um very new to the programming. if i know all why i waste resources. – Anushka lakmal Aug 27 '18 at 16:26

1 Answers1

1

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.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • It works, thanks, i know that i want to improve my self very much. i wanna ask one thing. if i iterate list as you are saying how can i set the String emailAddress into InternetAddress.parse(String)) – Anushka lakmal Aug 27 '18 at 16:48
  • @Anushkalakmal I don't understand your question. Are you truly asking me how to pass a parameter on a method call? You're doing it many times in the `javaData()` method implementation. – Andreas Aug 27 '18 at 16:52
  • @Anushkalakmal It seems that the task you're trying to do is beyond your current level of programming. I suggest you go back and try working with simpler tasks until your proficiency improves. Go through a Java tutorial to learn how method calls work, and how to pass parameters to method. StackOverflow is not a teaching site, and not a site for having other people write your code for you. If you don't understand the very basics, working with JDBC is way beyond where you should be at this point. – Andreas Aug 27 '18 at 16:57
  • i understand what you are saying. thanks for the help. i found a new job and they gave me that project. they also says that you put into deep water.so somehow i have to manage. – Anushka lakmal Aug 28 '18 at 02:46