Re: Trouble querying for unique records
"Justin" <justin.lottes@gmail.com> wrote in message
news:1162194799.489509.56310@k70g2000cwa.googlegroups.com...
I am trying to populate a JComboBox via unique records from a mysql
database. I first tried to use the DISTINCT command in the sql
statement, but that does nothing. I took the exact same statement and
typed it in the MySQL command prompt and it returned the appropriate
results. So after playing with that for a while, and trying to figure
it out, I gave up, and set out on creating my own unique record filter.
Code is pasted below:
int counter = 0;
for(int i = 0; i < queryResults.getSize(); i++){
boolean test = true;
for(int j = 0; j < i; j++){
if(String.valueOf(queryResults.getElementAt(i)).equals(problemReturn[j])){
test = false;
}
}
if(test){
problemReturn[counter++] =
String.valueOf(queryResults.getElementAt(i));
}
}
for(int i = 0; i < counter; i++){
problem.addItem(problemReturn[i]);
}
"queryResults" is a DefaultListModel with the data the queryReturned
stored inside it. "problemReturn" is an String array that stores just
the unique values found in the query. Both, the List Model and String
Array are temporary variables existing in just this method.
I copied this code into two different areas of the class, changing just
the name of problemReturn to etiologyReturn. There are two different
buttons that call the method containing the problemReturn variable. If
buttonA is pressed and the JComboBox components are compared to that if
buttonB is pressed, they are different, even though the same SQL
statement is ran. Furthermore, the method with the etiologyReturn
variable works fine all the time, even though the code was copied and
pasted. Any ideas on this to try to preferreably get the DISTINCT
command to work in my SQL statement, or a better way of finding unique
values in an array would be greatly appreciated.
I think you'd be better off figuring out what's wrong with your SQL rather
than writing a lot of application code to accomplish the same effect. SQL is
a powerful language and I'd be inclined to let it do the work whenever
possible. SQL is not that difficult in most cases, as long as you think in
terms of sets.
However, having said that, I don't see any information in your question that
would allow me to even guess at what SQL you need to accomplish what you are
trying to do.
It should be possible to help you if you give some information about the
tables involved, including the primary keys and a few rows of sample data
from each table. However, this may not be the best place to ask such a
question. The main mysql mailing list would be a more appropriate place
since we would not be talking about Java code any more.
--
Rhino