newbie attempt to use Java ArrayList to store ResultSet obtained from database

十年热恋 提交于 2019-12-31 07:04:11

问题


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

回答1:


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



回答2:


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.




回答3:


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



回答4:


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.




回答5:


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



回答6:


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.



来源:https://stackoverflow.com/questions/9585764/newbie-attempt-to-use-java-arraylist-to-store-resultset-obtained-from-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!