新手尝试使用Java ArrayList存储从数据库获取的ResultSet

时间:2012-03-06 14:38:43

标签: java arraylist

我有一个使用JDBC与Java应用程序服务器通信的数据库服务器。我想将数据库ResultSet中的数据存储到Java变量中。

这是我的Java课程,HRPeople:

public class HRPeople {
    public int elements;
    public String[] FirstName;
    public String[] LastName;
    public String[] Email;
    public int[] Salary;
}

我目前使用此类来存储ResultSet中的数据,如下所示:

query = "SELECT first_name, last_name, email, salary FROM HR.Employees where rownum < 6";
rset = stmt.executeQuery(query);
while (rset.next()) {
    returnHRdata.FirstName[ii] = rset.getString("first_name");
    returnHRdata.LastName[ii]  = rset.getString("last_name");
    returnHRdata.Email[ii]     = rset.getString("email");
    returnHRdata.Salary[ii]    = rset.getInt("salary");
    ii = ii + 1;
}

上述场景的问题是原始数组要求我知道ResultSet中的行数,以便我可以正确初始化这些数组。所以我想要做的是使用ArrayList。我如何修改上述方案来做到这一点?

这是我最初的尝试(这是否接近)?上面显示的HRPeople.java文件是否在这种情况下使用过?

query = "SELECT first_name, last_name, email, salary FROM HR.Employees where rownum < 6";
rset = stmt.executeQuery(query);
List<HRPeople> returnHRdata = new ArrayList<HRPeople>();
while (rset.next()) {
   returnHRdata.FirstName = rset.getString("first_name");
   returnHRdata.LastName  = rset.getString("last_name");
   returnHRdata.Email     = rset.getString("email");
   returnHRdata.Salary    = rset.getInt("salary");
   returnHRdata.add;
}

更新1:

如果我添加以下代码,

return returnHRdata;

我收到以下错误(知道为什么?):

myClass.java:213: incompatible types
found   : java.util.List<HRPerson>
required: java.util.ArrayList<HRPerson>
    return returnHRdata;
           ^
1 error

6 个答案:

答案 0 :(得分:5)

你可能想先定义一个像这样的HRPerson:

public class HRPerson {
    public String firstName;
    public String lastName;
    public String email;
    public int salary;
}

然后您的主要代码如下:

query = "SELECT first_name, last_name, email, salary FROM HR.Employees where rownum < 6";
rset = stmt.executeQuery(query);
List<HRPerson> returnHRdata = new ArrayList<HRPerson>();
while (rset.next()) {
   HRPerson person = new HRPerson();
   person.firstName = rset.getString("first_name");
   person.lastName  = rset.getString("last_name");
   person.email     = rset.getString("email");
   person.salary    = rset.getInt("salary");
   returnHRdata.add(person);
}

答案 1 :(得分:1)

List<HRPeople> returnHRdata = new ArrayList<HRPeople>();
while (rset.next()) {
   HRPeople people = new HRPeople();
   people.FirstName = rset.getString("first_name");
   people.LastName = rset.getString("last_name");
   people.Email    = rset.getString("email");
   people.Salary    = rset.getInt("salary");
   returnHRdata.add(people);
}

您可以使用lowerCase字母作为字段的第一个字符,并使用getter和setter来访问它们,从而改进此代码。

答案 2 :(得分:1)

转换它:

public class HRPeople {
  public int elements;
    public String[] FirstName;
    public String[] LastName;
    public String[] Email;
    public int[] Salary;
}

为:

public class HRPerson {
    public String firstName;
    public String lastName;
    public String email;
    public int salary;
}

List<HRPerson> people = new ArrayList<HRPerson>();

现在应该很容易:

while (rset.next()) {
  HRPerson person = new HRPerson();
  returnHRdata.firstName = rset.getString("first_name");
  returnHRdata.lastName = rset.getString("last_name");
  returnHRdata.email = rset.getString("email");
  returnHRdata.salary = rset.getInt("salary");
  people.add(person);
}

答案 3 :(得分:0)

...关闭

while (rset.next()) {
   HRPeople person = new HRPeople();
   person.setFirstName(rset.getString("first_name"));
   person.setLastName(rset.getString("last_name"));
   person.setEmail(rset.getString("email"));
   person.setSalary(rset.getInt("salary"));
   returnHRdata.add(person);
}

您当然必须在HRPerson类上定义setXXXX方法。哦,是的,并做托马斯建议的。

答案 4 :(得分:0)

创建一个HRPeople类,它有firstname,lastname ....属性,以及declare getter,setters方法。

然后:

List<HRPeople> returnHRdata = new ArrayList<HRPeople>();
HRPeople people = null;
while (rset.next()) {
   people = new HRPeople();
   people.setFirstName( rset.getString("first_name"));
   people.setLastName (rset.getString("last_name"));
...
   returnHRdata.add(people);
}

答案 5 :(得分:0)

不是在对象中存储每个属性的数组,而是创建一个对象来描述表中的给定实体。

class HRPerson {
    String firstName;
    String lastName;
    String email;
    Integer salary;
}

创建此类型的列表,允许您存储结果。

List<HRPerson> hrPeople = new ArrayList<HRPerson>();

while(rset.next()) {
    HRPerson person = new HRPerson();
    person.firstName = rset.getString("first_name");
    person.lastName  = rset.getString("last_name");
    person.email     = rset.getString("email");
    person.salary    = rset.getInt("salary");

    hrPeople.add(person);
}

最后,通过为表格中的每一行创建新对象来填充它。