Want to create a small project for online exam using jsp. As of now I am unable to find, how can I
> Get random questions from mysql database
> Put the timer, so that the exam result is shown automatically after the specified time for exam is over

More queries to follow.

Dani AI

Generated

Below is a compact, practical pattern to solve both parts of your question — picking unique random questions from MySQL and enforcing a timed exam — with production-minded tips. This builds on ideas from and complements the earlier pointers without repeating them.

Query only the question IDs that match the exam filters (category, difficulty). Shuffle IDs on the server, pick the first N, then fetch those rows in one batch. That guarantees uniqueness, avoids repeated DB lookups, and scales much better than ordering the whole table randomly for every user.

List<Integer> ids = loadIdsForExam(conn, category); // SELECT id FROM questions WHERE ...
Collections.shuffle(ids);
List<Integer> pick = ids.subList(0, Math.min(ids.size(), numQuestions));
// then SELECT * FROM questions WHERE id IN (?,?,...) using a PreparedStatement

Use a client-side JavaScript countdown only for UI, and enforce time on the server. Record session start_time and allowed duration in the DB. Periodically autosave answers (AJAX) so network blips do not lose work. On submit (manual or timeout) the server must check the stored start_time + duration and grade only answers received before expiry.

var end = Date.now() + durationMs;
setInterval(function(){
  if (Date.now() >= end){ clearInterval(this); document.getElementById('examForm').submit(); }
}, 1000);

Practical cautions and tips: persist a per-session mapping (session_id -> question order) so refreshes cannot swap questions; store each answer with a timestamp; use PreparedStatements and transactions; randomize choice order per user to reduce cheating; avoid ORDER BY RAND() on large tables (use ID shuffling or reservoir sampling instead). These steps make the system robust, auditable, and resilient to client-side tampering.

Recommended Answers

All 5 Replies

take a look on the following tutorial
http://roseindia.net/jsp/online-quiz-application-jsp.shtml

How can you suggest so awful example?

> Get random questions from mysql database
Depending on database structure. If you have questions organized by unique numeric value, then run random generator for numbers and then collect data from database. Worst case scenario no IDs, then store questions in array list, run random generator and match generated numbers with position in array list

> Put the timer, so that the exam result is shown automatically after the specified time for exam is over
Not sure of this, but I guess it will be done with JavaScript

Depending on database structure. If you have questions organized by unique numeric value, then run random generator for numbers and then collect data from database.

How to generate unique random numbers? If I use Random class, what should I do to ensure that any question isn't repeated?

  • Create a collection of integers (Array - there are some shortcomings to using array, ArrayList, Vector etc.)
  • Provide a loop mechanism that will repeat same process desired number of times
  • Generate random number
  • Compare against collection that this number is not already in collection
  • Depending on result you will either add this number to collection if it doesn't exists or you will request new random number

^OK.Will revert back when I proceed further.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.