File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Trying to pull out Unique values from database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Trying to pull out Unique values from database" Watch "Trying to pull out Unique values from database" New topic
Author

Trying to pull out Unique values from database

Bal Sharma
Ranch Hand

Joined: Sep 19, 2001
Posts: 273
Hello Everybody:
I am trying to display unique value from Carrier, Origin and Destination to populate dynamically combo boxes. GUI part I have not done any thing yet. I added getDistinctValues method as below in Data class and trying to see unique information.

It compiles fine. But I am getting following message on DOS window.
A:\scjd\starting>javac suncertify\db\Data.java
A:\scjd\starting>java suncertify.db.Data
[Ljava.lang.Object;
I got only header on my DataOut file. That is it. What is heck �[Ljava.lang.Object;� ? Any help, clue, suggestion, comments please. Thanks in advance BK
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

I think it means you have an array with one element that is an object. I used Vector, which might not be the best choice, but if you look at the returnArray declaration you will see how I got the values into a String array.
getFieldDataArray is the same method as your getUniqueValues method
private String[] getFieldDataArray(String fieldName){
Vector fieldData = new Vector();
String value;
try{
DataInfo[] records = dataAccess.criteriaFind("All");
for (int dataInfoNumber = 0; dataInfoNumber < records.length; dataInfoNumber++){
value = getFieldData(fieldName, records[dataInfoNumber]);
if (!fieldData.contains(value)){
fieldData.add(value);
}
}
} catch (Exception e){ }
String[] returnArray = (String[])fieldData.toArray(new String[fieldData.size()]);
return returnArray;
}
Hope this is readable for you.
Thanks
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Bal Sharma
Ranch Hand

Joined: Sep 19, 2001
Posts: 273
Mark thanks for response. Still it is not working.
I do not know what is going on? This time I have following code.

I uncommented line 16 I got following output.
A:\scjd\starting>java suncertify.db.Data
SpeedyAir
PromptAir
RainvilleAir
BeethAir
[Ljava.lang.Object;
I commented line 16 now I have
A:\scjd\starting>java suncertify.db.Data
[Ljava.lang.Object;
It looks like to me line 15 is not working. It is not adding any element in vector. Any clue? Hope I am not being pain for you. Thaks BK

[This message has been edited by Bal Sharma (edited November 09, 2001).]
Bisi Adedokun
Greenhorn

Joined: Jul 25, 2001
Posts: 7
Hey,
I checked your code and re-wrote it using an implementation of Set, HashSet. It works fine with reduce line of codes. The Set does not allow dups so that makes it straight forward. See the code and the file output below:
public Set getUniqueValues(String fieldName)
throws suncertify.shared.DatabaseException {
Set uniques = new HashSet();
int size = getRecordCount();
for (int i = 1; i <= size; i++) {
DataInfo dataInfo = getRecord(i);
String[] values = dataInfo.getValues();
FieldInfo[] fields = dataInfo.getFields();
for (int j = 0; j < fields.length; j++) {
String name = fields[j].getName();
if (name.equals(fieldName)) {
uniques.add(values[j].trim());
}
}
}
return uniques;
}
The main method is basically same as yours except the loop construct is different:
public static void main(String[] args) throws java.io.IOException {
PrintWriter outFile = null;
Data d = new Data("g:\\suncertify\\data\\db.db");
try {
outFile = new PrintWriter(new FileWriter("g:\\suncertify\\data\\DataOut.txt"), true);
outFile.println("Unique values ");
outFile.println("==============");
outFile.println();
Set set = d.getUniqueValues("Carrier");
Iterator iter = set.iterator();
while (iter.hasNext()) {
outFile.print(iter.next());
outFile.println();
}
} catch (DatabaseException de) {
System.out.println(de.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
}
outFile.close();
}
Then the output file has four entries:
Unique values
==============
SpeedyAir
PromptAir
BeethAir
RainvilleAir
I hope this helps.
Bisi

Bisi
Bal Sharma
Ranch Hand

Joined: Sep 19, 2001
Posts: 273
Hey Bisi thanks, it is working. Still I am not sure why it did not work? while I changed into from set OR vector into String []. Like:
String[] returnArray = (String[])newValue.toArray();
return returnArray;
Who cares, right! I need distinct values. That is it. Thanks for guiding me. BK
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Trying to pull out Unique values from database