函数返回另一个类的变量

时间:2012-10-23 06:24:00

标签: java

我需要一个函数来取一个名字并返回与该名称相关的记录的详细信息。我该怎么办?我有所有的信息。在我的结果集中,但我需要一种方法将它传递给一个变量并返回java中的值。我在CentOS下使用eclipse-juno

1 个答案:

答案 0 :(得分:1)

如果您的Person课程的字段为NameAddressAge,我假设您要查找Person的所有详细信息给定他/她Name,您可以编写一个返回Person类对象的方法,如下所示:

//Assuming Person class is declared with all fields and/or appropriate setters-getters.
//create a method to get all the details of a particular person based on name.
public Person getPerson(String name){
   //get all the details from the database in a resultset rs
   //Create a new object Person class
   Person p = new Person();
   //set the details from the resultset to this newly created Person object
   p.setName(rs.getString("name");
   p.setAddress(rs.getString("address");
   p.setAge(rs.getInt("age");

   //now your Person object is ready with all the details that you need, you return it to callinh method

  return p;

}

您可以使用getter访问字段来使用此返回的Person对象。