• Post Reply Bookmark Topic Watch Topic
  • New Topic

JDBC/MSSQL attempting to create objects with the rs data then add to arraylist  RSS feed

 
Celyn Hunter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. New here.

I am trying to use the rs.next() loop to each time add the data to create a new object and then add that object to an array.

The data received is correct. However, each time I run the loop, it changes the object, meaning that every object in the array is the same as the last one.

This way of creating new objects and adding to arrays works in ordinary loops, so I can't see what is going wrong when I do it the same in an rs.next() loop.

The following is the code:



Thanks
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a single Topic object, created on line 8.
All you are doing is changing the values inside that single object and then re-adding it to the List.

What you want to do is create a new one each time round the loop, which means move line 8 inside the while loop.

By the way, you ought to be using a PreparedStatement for that query.
 
Brian Tkatch
Bartender
Posts: 598
26
Linux Notepad Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to make 2 side comments on your query.

1) It uses dynamic SQL, which is open to SQL Injection. As Dave Tolls pointed out, please use PreparedStatement.
2) Please do not use SELECT *, as that can cause issues should the column list ever change. Instead, specify the columns you are trying to retrieve. As an added benefit, it is a bit self-documenting as well.
 
Celyn Hunter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou for your quick reply.

I will definitely change it to a prepared statement, thankyou.

I shifted the creator into the while loop but the problem persists in the same way.

I have played around with this, and created the following generic code, which as far as I can see, is exactly the same but works.



Yet it doesn't work inside rs.next()?

 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us the modified code with the object creation inside the while loop.
Also, how are you testing this?  Are you able to run it standalone against the database?

For your Sys.out call (line 16) do you see different values?
 
Celyn Hunter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am basically testing this using sysout lines of code. I am using Eclipse and when I try to debug it claims "The source attachment does not contain the source for file SQLServerResultSet.class".

I am not quite sure what you mean by "run standalone". I am still learning.



At present, there are 2 appropriate rows from the database, running the program results as follows: (ignore catName)

--------------
Topic Object: Topic [category=2, subCategory=2, catName=null, subName=Lists]
--------------
al arraylist: [Topic [category=2, subCategory=2, catName=null, subName=Lists]]
--------------
Topic Object: Topic [category=2, subCategory=3, catName=null, subName=Strings]
--------------
al arraylist: [Topic [category=2, subCategory=3, catName=null, subName=Strings], Topic [category=2, subCategory=3, catName=null, subName=Strings]]

As you can see, 2 different results are made, but the second one overwrites the first one.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK.
What does your Topic class look like?

Are the category and subcategory attributes static, for example?
 
Celyn Hunter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohhhhhhhhhhhhhhhhhhhhhhhhhh!!!

I had all the variables as 'protected static ...'

Based on your question I removed them and tried again and YAAAAAYYY! It works correctly.

I hadn't thought to question that, I guess I better try to understand properly what static means, I guess I have been glossing over that.

Thankyou so much.


 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Making an attribute static in a class means there is only ever one of them, no matter how many instances of a class are instantiated.
In your case that meant, even though you were creating multiple Topic objects, whenever you called setCategory it was changing the value of the single category variable associated with the class.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A good rule of thumb for beginning programmers is, If there isn't a very good reason to make something static, don't.  In particular:

* Because all my code is in main() is not a good reason.  Get your code out of main(), see MainIsAPain (that's a link).

* Because my IDE suggested it is not a good reason.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!