• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using ResultSet - which is a more effecient code?

 
smee bond
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am new to Java Ranch and this is my first post.
I am using the following code snippet to retrieve data from a ResultSet, store them in objects and finally store the objects in an arraylist. There are two ways in which I can do it (Type1 and Type2). Could you tell me which one is more effecient? In other words, which one is a better performer?

//Attributes
private int petID = 0;
private String petName = new String();

Type1:

while(rs.next()){

PetView myObj = new PetView();

myObj.petID = rs.getInt("PetID");
myObj.petName = rs.getString("petName");

petArrayList.add(myObj);
}

Type2:

while(rs.next()){

PetView myObj = new PetView();

setPetID(rs.getInt("PetID"), myObj);
setPetName(rs.getString("petName"), myObj);

petArrayList.add(myObj);

}

//Set methods
public void setPetID(int id, PetView myObj){
myObj.petID = id;
}

public void setPetName(String name, PetView myObj){
myObj.petName = name;
}
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

Type2 seems very odd to me. The setters methods belong on the PetView.

Public variables like those build tight "coupling" between classes. You can't change the internal structure of PetView without breaking code that uses them. But if we add setters to PetView:

then the internals are more private and we can change them without breaking users.

There is some (rather advanced perhaps) goodness about objects that can't be changed once they are made, so it might be even better to remove the setters and pass the fields in the constructor:
 
smee bond
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your valuable suggestions.
As per your suggestion (1), I changed my code as follows. I am keen on using setter methods because I want to learn to use them. Please let me know if this is more user-friendly. Thanks!

public class PetView{

//Attributes
private int petID = 0;
private String petName = new String();

public ArrayList createArrayList(ResultSet rs, ArrayList petArrayList){

while(rs.next()){
PetView myObj = new PetView();
myObj.setPetID(rs.getInt("PetID"));
myObj.setPetName(rs.getString("PetName"));
petArrayList.add(myObj);
}
}

//Set methods
public void setPetID(int id){
petID = id;
}

public void setPetName(String name){
petName = name;
}

}//End of class
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, that's perfectly reasonable.

I have to admit that the suggestion to avoid setters and do the job in the constructor is rather advanced for now, but keep it in mind - you may see it again one day.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic