This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Online test application

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am developing an online test website.
I am using Servlet with hibernate. The following codes generates only 1Q randomly selected from the database.
There should be 20 Questions randomly generated from database, without repetition. I tried with for loop , but unsuccessful.


OnlineExam.java



TestServlet.java




TestDao.java


 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not randomly select 20 questions from database and set it in the session so that each user can see the random questions at their end.

To get the random rows from database. The link http://viralpatel.net/blogs/2009/05/fetch-random-rows-from-database-mysql-oracle-ms-sql-postgresql-example.html might help.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello piyush

SELECT column FROM
( SELECT column FROM table
ORDER BY dbms_random.value )
WHERE rownum <= 10
fetches all the records whose rownum is less than or equal to 10. but i want only 5 records out of those.

Moreover if it fetches 20 records, a foreach tag will help in the following code?


 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh Sorry. I didn't knew that oracle has rownum internally. Random numbers is generated by the database. I am moving forward to the codings. Thanks
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now i am stuck with converting that subquery to either HQL or criteria
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from HQL and Criteria, You can also use native SQL for your query in hibernate.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, i have used native SQL. It works fine. Now I am storing user answers in a collection, and then will compare it with the data(correct answers) stored in database.
 
Telemart Network
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice programme. Online test application is good one to take tests easily & also accurately.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think sql query for randomly generating rows, i.e works slower. So i am using Random class for generating random ids, then storing it into a List. But, how to get the records as per the ids in the list? There are about 90 random ids in the list. Is there any way in which i dont need to run the query 90 times?
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Solved, in Restrctions. class there is a in method which takes 2 args, string property and the collection.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic