【问题标题】:Query a database and generate an ArrayList object查询数据库并生成 ArrayList 对象
【发布时间】:2020-11-04 22:58:33
【问题描述】:

您好,我有一个问题在这里无法解决:

getPeople () 方法编写代码,使其构造并返回一个 从数据库中检索到的 Person 类型对象的 ArrayList

java.sql.Connection 类型的对象已被初始化。 SQL 数据库包含一个包含 3 列的人员表:id(自动递增整数) , first_name (varchar), last_name (varchar)。

使用 Connection 对象,从表中检索所有记录,并为每个 记录使用相应的字段来创建Person类型的对象。添加每个 Person 类型对象因此在您的 ArrayList 中创建。 返回结果列表。

这是必须完成的代码:

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql. *;
import java.util.ArrayList;
import java.util. *;

public class ExerciseImpl {

    public void runExercise (String [] argv) throws Exception {
    }

    public ArrayList <Person> getPeople () throws Exception {
        Connection conn = this.getCandidateConnection ();
        / * ---------- DO NOT CHANGE THE CODE ABOVE THIS LINE, IT WILL BE RESET ON RUN ---------- * /

        / **** Enter your code here **** /

        / * ---------- DO NOT CHANGE THE CODE BELOW THIS LINE, IT WILL BE RESET WHEN RUNNING ---------- * /
    }

   
}

class Person {
    public int id;
    public String firstName;
    public String lastName;

    Person (int id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

这是我想出的代码:

public class ExerciseImpl {

    public void runExercise (String [] argv) throws Exception {
    }

    public ArrayList <Person> getPeople () throws Exception {
          ResultSet resultSet=null;
       try( Connection conn = this.getCandidateConnection();
            Statement statement= conn.createStatement();){
           String selectSql = "SELECT id, first_name, last_name from people";
           resultSet = statement.executeQuery(selectSql);
 
    }catch (SQLException e){
           e.printStackTrace();
       }
 
        }

        / * ---------- DO NOT CHANGE THE CODE BELOW THIS LINE, IT WILL BE RESET WHEN RUNNING ---------- * /
    }

   
}

class Person {
    public int id;
    public String firstName;
    public String lastName;

    Person (int id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

老实说,我完成不了多少(我只是创建了没有正文的 getCandidateConnection () 方法) 因为我不知道怎么做 完成它,其余的,我不知道如何在 java 程序中查询数据库。 基本上,我很难解决这个问题。

你能帮帮我吗?

【问题讨论】:

  • 您更改了“请勿更改此行下方的代码,它将在运行时重置”行下方的代码?这是为了上课还是为了工作?你希望下一个任务做什么?

标签: java sql


【解决方案1】:

您更新后的代码看起来好多更好。您仍然移动到代码获取Connection 的位置。我把它移回来,稍微调整了一下。从ResultSet 获取idfirst_namelast_name。然后实例化一个Person 实例并将其添加到List。最后,返回List。比如,

public ArrayList<Person> getPeople() throws Exception {
    Connection conn = this.getCandidateConnection();
     /* ---------- DO NOT CHANGE THE CODE ABOVE THIS LINE, IT WILL BE RESET ON RUN ---------- */
    final String selectSql = "SELECT id, first_name, last_name " //
            + "FROM people ORDER BY last_name, first_name";
    ArrayList<Person> al = new ArrayList<>();
    try (Statement statement = conn.createStatement(); //
            ResultSet resultSet = statement.executeQuery(selectSql)) {
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String firstName = resultSet.getString("first_name");
            String lastName = resultSet.getString("last_name");
            al.add(new Person(id, firstName, lastName));
        }
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
    return al;
    /* ---------- DO NOT CHANGE THE CODE BELOW THIS LINE, IT WILL BE RESET WHEN RUNNING ---------- */
}

【讨论】:

  • 这很好,但是在 selectSql 中,我认为没有必要添加 ORDER BY last_name,first_name,因为他们没有要求在分配中排序,反正你的代码比我的好,我将对其进行测试然后验证它
  • 我从异常抛出中添加了 catch 方法,然后我添加了 getCandidateConnection() 方法,但我不知道如何完成 getCandidateConnection() 主体
猜你喜欢
  • 1970-01-01
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多