newbie attempt to use Java ArrayList to store Resu

2020-05-09 11:56发布

I have a database server communicating with a Java application server using JDBC. I want to store data from the database ResultSet into Java variables.

Here's my Java class, HRPeople:

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

I currently use this class to store data from ResultSet, as follows:

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;
}

The problem with the above scenario is that the primitive arrays require me to know the number of rows in the ResultSet so that I can properly initialize those arrays. So what I want to do is use an ArrayList instead. How would I modify the above scenario to do this?

Here's my initial attempt (is this close)? Is HRPeople.java file shown above even used in this scenario?

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;
}

UPDATE 1:

If I add to the code the following,

return returnHRdata;

I get the following error (any idea why?):

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

6条回答
ゆ 、 Hurt°
2楼-- · 2020-05-09 12:29

create a class HRPeople, which has firstname, lastname.... attributes, and declare getter, setters method.

then:

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);
}
查看更多
Rolldiameter
3楼-- · 2020-05-09 12:31

You probably want to first define an HRPerson like this:

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

Then your main code would look like:

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);
}
查看更多
▲ chillily
4楼-- · 2020-05-09 12:43

Instead of storing an array of each property in your object, make a single object to describe a given entity in the table.

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

Create a list of this type, allowing you to store the results.

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);
}

Finally, fill it by creating new objects for each row in your table.

查看更多
爷、活的狠高调
5楼-- · 2020-05-09 12:47

Convert this:

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

to:

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

and:

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

Now it should be easy:

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);
}
查看更多
倾城 Initia
6楼-- · 2020-05-09 12:48
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);
}

You can improve this code by using a lowerCase letter for your first char of your fields and using getters and setters to access them.

查看更多
孤傲高冷的网名
7楼-- · 2020-05-09 12:50

Close...

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);
}

You of course must define the setXXXX methods on the HRPerson class. Oh yeah, and do what Thomasz suggested.

查看更多
登录 后发表回答