I have been busy with a exercise where I need to compare a winning lottery number(which is generated) with existing lottery numbers in a database table.
I manage to loop through all the numbers in the database and subsequently find matches with the winning set of numbers but am struggling to pinpoint in which set of lottery numbers the match was found.
The numbers were placed individually in each database column.
Here I retrieve the values in each row.
ResultSet set = state.executeQuery("SELECT * FROM numberLotto");
while(set.next())
{
String[]row = new String[7];
row[0] = set.getString(1);
row[1] = set.getString(2);
row[2] = set.getString(3);
row[3] = set.getString(4);
row[4] = set.getString(5);
row[5] = set.getString(6);
row[6] = set.getString(7);
entries.add(row);
}
String[][]listOfNumbers = entries.toArray(new String[entries.size()][7]);
I then loop through listOfNumbers and compare all the values with the values of winningNumber.
Object[]winningNumber = lottoDrawString.toArray();
for (int i=0; i < listOfNumbers.length; i++)
{
for (int j=0; j < listOfNumbers[i].length; j++)
{
for(int k = 0; k < winningNumber.length; k++)
{
System.out.println(listOfNumbers[i][j]);
if(listOfNumbers[i][j].equals(winningNumber[k]))
{
System.out.println("There is a match " + listOfNumbers[i][j] + " and " + winningNumber[k]);
}
}
}
}
How can I retrieve the row where the match(es) were found?
kind regards
Why not just do
SELECT * FROM numberLotto WHERE FirstNumber = n1 AND SecondNumber = n2 AND ...
For that matter, why not just store the lotto numbers as a seven-digit string in the database?
One idea could be to add the line number as table field, retrieve it, and instead of creating and checking numbers, do it with an object, lets say Loterry.
class Lottery
{ int line, value;
}
You check value field and if it fits your check, return line. Hope it helps :)
You can accomplish the search without doing much "programming". Write a query to do it for you:
String query = "SELECT * FROM numberLotto WHERE number = " + winningNumber
The above assumes that your lottery numbers are stored in a field labeled "number"
. You may also use AND
or OR
clauses to search for multiple matches.