java - 查询数据库并生成 ArrayList 对象
问题描述
嗨,我有一个问题,我无法在这里解决它是:
编写getPeople ()方法的代码,以便它构造并返回 从数据库检索到的Person类型对象的ArrayList 。
java.sql.Connection类型的对象已被初始化。SQL 数据库包含一个包含 3 列的 people 表: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 程序中查询数据库。基本上,我很难解决这个问题。
你能帮我吗?
解决方案
您更新的代码看起来好多了。您仍然移动了代码获取Connection
. 我把它移回来,稍微调整了一下。获取id
,first_name
并last_name
从ResultSet
. 然后实例化一个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 ---------- */
}
推荐阅读
- android - 在onBackPressed之外的活动中获取当前片段?
- azure - PnP Powershell Add-PnPOffice365GroupToSite Authorization_RequestDenied Azure Runbook
- pandas - 大熊猫每行的意思是大小为 5 的块
- java - 在 Spring RestController 中将 POJO 显式转换为 JSON 的最佳方法
- css - 需要帮助在 Bootstrap 主题的下拉菜单中添加背景颜色
- c - 等待信号 MEX 功能
- c# - 以编程方式将系列添加到 XAML 中定义的 OxyPlot
- laravel - 从主机 virtualbox 连接 localhost
- android - mkdirs 在某些设备中返回 false
- sql-server - 空记录集值 - sql server 2017