Calling a Random Value from a Database in JAVA

Asked By 0 points N/A Posted on -


I really need some Java Programmers to help me solve this problem. I have a database which has already data stored in it. I'm planning to get a random value in the database every time it calls it. My program is a guessing game like Hangaroo and random words from db should be get.

For example in my database I have 5 values in it and then when the program opens, it gets a random one from those 5 values. I only know how to get values from a db but not randomly.

What will I do to make my program get a random value?

Answered By 0 points N/A #99328

Calling a Random Value from a Database in JAVA


Hello Yannie,

The things you will be needed are a variable that generates a random number and a java switch case.

Step 1: Import the class that generates random numbers in various ranges.

Import java.util.Random;

Step 2: Initialize a variable that receives a random number.


Random randomNumber = new Random();

int x = randomNumber.nextInt(5); 


Variable x is needed/important in the next step to come up with a random value.

The number inside "nextInt(5)" which is 5 is the limit of the number to be generated. In the Example show the number to be generated is until 5. So the generated number will be 0-5.

Step 3: Use the Java Switch Case.


String value;

switch (x)


case 0:

value = value1; // value in the database


case 1:

value = value2; // value in the database


case 2:

value = value3; // value in the database


case 3:

value = value4; // value in the database


case 4:

value = value5; // value in the database





The variable x inside "switch (x)" is now random.

Hope my answer help you.

Login/Register to Answer

Related Questions