名前を取得し、その名前に関連付けられたレコードの詳細を返す関数が必要です。どうすればよいですか? 私はすべての情報を持っています。私の結果セットでは、それを1つの変数に渡し、Javaで値を返す方法が必要です. CentOS で eclipse-juno を使用しています
1 に答える
1
Person
fields を持つクラスがName
あり、与えられた his/herのすべての詳細を見つけたいと仮定すると、Address
次のようにクラスのオブジェクトを返すメソッドを書くことができます:Age
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;
}
この返されたPerson
オブジェクトを使用して、ゲッターを使用してフィールドにアクセスできます。
于 2012-10-23T06:34:22.717 に答える