如何将数据库中的值存储到集合中?

huangapple go评论95阅读模式
英文:

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 personList)。最终,将存有Person对象的集合返回。请确保你在代码中引入了正确的包并定义了Person类。

英文:
public String getDataOfpersons() {
try {
	
	String query = &quot;select * from persons&quot;;
	rs = st.executeQuery(query);
	System.out.println(&quot;Records from persons database&quot;);
	while(rs.next()) {
		String name = rs.getString(&quot;name&quot;);
		String age = rs.getString(&quot;age&quot;);
		System.out.println(&quot;Name: &quot; + name + &quot; &quot; + &quot;age: &quot; + 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&lt;Person&gt; getDataOfpersons() {
  // create a list to store values
  List&lt;Person&gt; personList = new ArrayList&lt;&gt;();
  try {

    String query = &quot;select * from persons&quot;;
    rs = st.executeQuery(query);
    System.out.println(&quot;Records from persons database&quot;);
    while (rs.next()) {
      Person person = new Person();
      String name = rs.getString(&quot;name&quot;);
      String age = rs.getString(&quot;age&quot;);
      person.setName(name);
      person.setAge(age);
      personList.add(person);
      System.out.println(&quot;Name: &quot; + name + &quot; &quot; + &quot;age: &quot; + 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&lt;String,String&gt; map = new HashMap&lt;&gt;();
        String query = &quot;select * from persons&quot;;
        rs = st.executeQuery(query);
        System.out.println(&quot;Records from persons database&quot;);
        while(rs.next()) {
            String name = rs.getString(&quot;name&quot;);
            String age = rs.getString(&quot;age&quot;);
            //System.out.println(&quot;Name: &quot; + name + &quot; &quot; + &quot;age: &quot; + age);
            map.put(&quot;Name&quot;,name);
            map.put(&quot;age&quot;,age);
           System.out.println(map);
        }

    }catch(Exception ex) {
        System.out.println(ex);
    }
    return null;
}

huangapple
  • 本文由 发表于 2020年10月4日 17:32:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64193033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定