aspose file tools*
The moose likes Beginning Java and the fly likes Using ResultSet - which is a more effecient code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using ResultSet - which is a more effecient code?" Watch "Using ResultSet - which is a more effecient code?" New topic
Author

Using ResultSet - which is a more effecient code?

smee bond
Ranch Hand

Joined: Sep 29, 2007
Posts: 34
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

Joined: Jan 29, 2003
Posts: 8791
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:


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
smee bond
Ranch Hand

Joined: Sep 29, 2007
Posts: 34
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

Joined: Jan 29, 2003
Posts: 8791
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Using ResultSet - which is a more effecient code?
 
Similar Threads
Geting an SQL Exception while passing the resultset to a bean
retrieving data from a databse into an excelsheet
input.readObject(); cannot resolve symbol
Returning a 0 value error
ArrayList