英文:
How do I store values from a database into a collection?
问题
public String getDataOfPersons() {
try {
String query = "select * from persons";
rs = st.executeQuery(query);
System.out.println("Records from persons database");
List<Person> personList = new ArrayList<>(); // Create a collection to store objects
while (rs.next()) {
String name = rs.getString("name");
String age = rs.getString("age");
System.out.println("Name: " + name + " " + "age: " + age);
Person person = new Person(name, age); // Create a Person object
personList.add(person); // Add the object to the collection
}
return personList; // Return the collection of Person objects
} catch (Exception ex) {
System.out.println(ex);
}
return null;
}
在这段代码中,我从MySQL数据库中读取数据,并将每条记录的姓名和年龄存储为一个对象,并将这些对象添加到一个集合中(List
英文:
public String getDataOfpersons() {
try {
String query = "select * from persons";
rs = st.executeQuery(query);
System.out.println("Records from persons database");
while(rs.next()) {
String name = rs.getString("name");
String age = rs.getString("age");
System.out.println("Name: " + name + " " + "age: " + age);
}
}catch(Exception ex) {
System.out.println(ex);
}
return null;
}
So I have this part of code and I am reading it from mysql database and I am wondering how could I store object to collection (Name and age) ?
答案1
得分: 1
首先,创建一个具有“name”和“age”字段的Person类。还需添加相应的getter和setter方法。
然后按以下方式修改您的代码:
// 修改返回类型
public List<Person> getDataOfpersons() {
// 创建一个列表以存储数值
List<Person> personList = new ArrayList<>();
try {
String query = "select * from persons";
rs = st.executeQuery(query);
System.out.println("从persons数据库获取记录");
while (rs.next()) {
Person person = new Person();
String name = rs.getString("name");
String age = rs.getString("age");
person.setName(name);
person.setAge(age);
personList.add(person);
System.out.println("姓名:" + name + " 年龄:" + age);
}
} catch(Exception ex) {
System.out.println(ex);
}
return personList;
}
英文:
First, create a Person class with "name" and "age" fields. Also add the corresponding getters and setters.
Then modify modify your code like this:
// change the return type
public List<Person> getDataOfpersons() {
// create a list to store values
List<Person> personList = new ArrayList<>();
try {
String query = "select * from persons";
rs = st.executeQuery(query);
System.out.println("Records from persons database");
while (rs.next()) {
Person person = new Person();
String name = rs.getString("name");
String age = rs.getString("age");
person.setName(name);
person.setAge(age);
personList.add(person);
System.out.println("Name: " + name + " " + "age: " + age);
}
} catch(Exception ex) {
System.out.println(ex);
}
return personList;
}
答案2
得分: 0
不确定这是否符合您的要求,且不改变方法的现有签名。
public String getDataOfpersons() {
try {
Map<String, String> map = new HashMap<>();
String query = "select * from persons";
rs = st.executeQuery(query);
System.out.println("来自 persons 数据库的记录");
while(rs.next()) {
String name = rs.getString("name");
String age = rs.getString("age");
//System.out.println("姓名:" + name + " 年龄:" + age);
map.put("姓名", name);
map.put("年龄", age);
System.out.println(map);
}
} catch(Exception ex) {
System.out.println(ex);
}
return null;
}
英文:
Don't know if this is what u looking for without changing the existing signature of the method
public String getDataOfpersons() {
try {
Map<String,String> map = new HashMap<>();
String query = "select * from persons";
rs = st.executeQuery(query);
System.out.println("Records from persons database");
while(rs.next()) {
String name = rs.getString("name");
String age = rs.getString("age");
//System.out.println("Name: " + name + " " + "age: " + age);
map.put("Name",name);
map.put("age",age);
System.out.println(map);
}
}catch(Exception ex) {
System.out.println(ex);
}
return null;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论