I wrote a code which execute the query, stores the result set. I am storing the result set column-wise in different Arrays, using getArray(columnLabel) method.
The code is here :
Array imported_by = null;
Array imported_by_ad = null;
Array active_directory_identity_value = null;
while (rs.next()) {
imported_by = rs.getArray(column_id);
imported_by_ad = rs.getArray(column_id + 1);
active_directory_identity_value = rs.getArray(column_id + 2);
row_count++;
}
Now I would like to print the result set index-by-index that is what we usually do while printing List:
for(int i=0;i<=row_count;i++){
System.out.println(imported_by.get(i) + " " + imported_by_ad.get(i) + " "
+ active_directory_identity_value.get(i));
get(i) method cannot be used for object type Array, but is there any other method to store the resultset in some list and access/read it as much and whenever I can?
Let me know :)
Thanks.
Update: I also type cast the variable but unfortunately it is throwing exception now: The updated code is :
try {
int row_count = 0;
ArrayList<String> imported_by = null;
ArrayList<String> imported_by_ad = null;
ArrayList<String> active_directory_identity_value = null;
while (rs.next()) {
imported_by = (ArrayList<String>) rs.getArray(column_id);
imported_by_ad = (ArrayList<String>) rs.getArray(column_id + 1);
active_directory_identity_value = (ArrayList<String>) rs.getArray(column_id + 2);
row_count++;
}
}
and the exception is
Exception in thread "main" java.lang.UnsupportedOperationException at sun.jdbc.odbc.JdbcOdbcResultSet.getArray(JdbcOdbcResultSet.java:4395)
I have been able to store the result set using Lucene Document(org.apache.lucene.document.Document), storing the fields to the document.
This might not be the perfect solution but it does work perfectly :) The sample code is as below :
ResultSet rs = st.executeQuery(query);
rs = st.executeQuery(query);
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
Directory index = new RAMDirectory();
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer);
org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document();
try {
while (rs.next()) {
String imported_by = rs.getString(1);
Field field = new Field("imported_by", imported_by , Field.Store.YES, Field.Index.NOT_ANALYZED);
doc.add(field);
IndexWriter writer = new IndexWriter(index, config);
writer.addDocument(doc);
writer.close();
}
printDocument(doc);
} catch (IOException ex) {
Logger.getLogger(CIMTPFS_Roche.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(CIMTPFS_Roche.class.getName()).log(Level.SEVERE, null, ex);
}
}
That will just write the resultset to the Document and index it, later we can use the following function to get the string values from the Document :)
public void printDocument(org.apache.lucene.document.Document doc) {
List<IndexableField> fields = doc.getFields();
for (int i = 0; i < fields.size(); i++) {
System.out.println(fields.get(i).stringValue());
}
}
Hope it helps :)