• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ResultSet randomizing

 
Baba Bizlowsky
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody.
I have the following problem. I need to get a ResultSet from a database and then display all of its rows in random order. And I would like to do then in the most elegant way.
My best solution so far was to make an array of numbers from 1 to whatever the number of records in the ResultSet is, than jumble the array up with a simple mixing algorithm, and finally read the rows from the ResultSet sequentially using the number from this array and the ResultSet.absolute(int) method.
I don't think this is the most effective solution though, and I was wondering whether there was an easier way.
So, ideas anyone?
[ February 26, 2004: Message edited by: Baba Bizlowsky ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34071
331
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Baba,
I would copy the records from the resultset into another data structure (array, arraylist, ...) first, so you can close the resultset. You can use your mixing algorithm on this data structure and then loop through it to display the results.
 
SJ Adnams
Ranch Hand
Posts: 925
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you could find some to incorporate DBMS_RANDOM.RANDOM into your SQL?
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Baba,
I think SJ is recommending to use your DBs Random function to include a new columun in your output and then order by the random field:

You should then be able to iterate through your recordset in the normal way and end up with the records in random order.
Tom Blough
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic