Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector problem

 
bill dimitry
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I have problem with a Vector. I get 26 records from a DB and enter them into a Vector. I've checked the ResultSet values and they are OK.
I start printing values (from the first object) when the ResultSet reaches 3 (�First Print Name (Code)�). The values printed are from the current object (from row 3 up) not from the position 0 and up to 23.
When I try to print the values second time (�Second Print Name(Code)�) only one (the last record�s) values are printed 26 times (Vector�s size).
It looks like every time a new object(Course) is entered into the Vector it replaces all other objects in it.

Any idea?

Thanks,
Bill

CODE:

ResultSet rs = getRecords();
Course c = new Course();
Course cr = new Course();
Vector<Course> coll = new Vector<Course>();
String code = null;
String course = null;
String level = null;
try {
while (rs.next()) {
code =rs.getString("courseCode");
course = rs.getString("courseName");
level = rs.getString("courseLevel");
System.out.println("Get Cluster: " + course);
c.setCourseCode(code);
c.setCourseName(course);
c.setCourseLevel(level);
coll.addElement(c);
if((rs.getRow()-1)>2){
cr = coll.elementAt(rs.getRow()-3);
System.out.println("First Print Name: "+cr.getCourseName());
System.out.println("First Print Code: "+cr.getCourseCode());
}
}
} catch (SQLException ex) {
System.out.println(ex.toString());
}
Enumeration e = coll.elements();
Course c1 = new Course();
while (e.hasMoreElements()) {
c1 = (Course) e.nextElement();
System.out.println("Second Print Name: "+c1.getCourseName());
System.out.println("Second Print Code: "+c1.getCourseCode());
}
 
chen weiwei
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI!
You must new Course in while(){this...} try it ;
MY test code:

[ May 28, 2008: Message edited by: chen weiwei ]
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Bill and Chen,

Welcome to Javaranch.

Bill,Whenver you are posting some code then always enclose it with CODE tag.

Chen has pointed out correctly. If you don't create the object of Course class every time then there will be a multiple copy of same object into your vector.

Second,

I didn't get the use of this.



Do you want to print the values only or want to keep the track of those values by storing them somewhere? What is the use of cr(Course) object?
[ May 29, 2008: Message edited by: Vishal Pandya ]
 
bill dimitry
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys!
It works now.

Sorry for the CODE tag. I didn't know.
I'll use it next time.

Kind Regards,
Bill
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome Bill.

When i read your name Bill Dimitry , a hollywood character came in my mind. But i couldn't figured out it.

Hmm... Who's that?
 
Campbell Ritchie
Sheriff
Posts: 48635
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could only find one Bill Dimitry on Google-at JavaRanch.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not Bill Dimitry actually .Only Dimitry!

I don't remember in which film i heard 'Dimitry'.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vishal Pandya:
Not Bill Dimitry actually .Only Dimitry!

I don't remember in which film i heard 'Dimitry'.


http://www.imdb.com/find?s=char;q=dimitry
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<hijack>
The most memorable Dmitri to me is the Soviet Premier Kissoff in Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb. Since he is neither seen nor heard, only addressed by that name by Peter Sellers on the telephone, he doesn't show up in IMDB, but Peter Sellers' telephone monologue does (search for "Kissoff"). A little excerpt:
I'm sorry, too, Dmitri... I'm very sorry... *All right*, you're sorrier than I am, but I am as sorry as well... I am as sorry as you are, Dmitri! Don't say that you're more sorry than I am, because I'm capable of being just as sorry as you are... So we're both sorry, all right?

<end-of-hijack>
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ilja and Ulf.
 
Campbell Ritchie
Sheriff
Posts: 48635
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent film, Dr Stangelove, but please, no more about Dmitri or I shall send us all off to Meaningless Drivel.
 
bill dimitry
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I'm glad that my 'controversial' username is causing the debate.

By the way, don't go to the Meaningless Drivel (what's that?) yet. I have more problems.
Check this post

Kind Regards,
Bill
 
Campbell Ritchie
Sheriff
Posts: 48635
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bill dimitry:
Hi guys,
I'm glad that my 'controversial' username is causing the debate.
Meaningless Drivel (what's that?).
I think people were overreacting about the name [ ], and as for what's Meaningless Drivel:



You don't want to know!!

It's actually where people have friendly chats about anything non-computer related.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic