Why string is not printing correctly? - java

I have created a user type in oracle
create or replace type my_friend_list
is
table of
varchar2(100);
Now i have written a procedure which has a output parameter like this :
PROCEDURE friends_diff_prc
(
my_name IN VARCHAR2,
friend_i IN my_friend_list ,
good_friend_o OUT my_friend_list ,
best_friend_o OUT my_friend_list ,
isSuccess OUT NUMBER
);
I run it on SQLDeveloper And it is running correctly .
And it is giving me list both of my good friends and best friends.
But when i am calling it through JAVA Class which is extending StoredProcedure.
best_friend list is correcly coming but for the good_friend list it is printing
Ljava.lang.Object;#24342434,
Ljava.lang.Object;#243a243a,
Ljava.lang.Object;#24402440,
Ljava.lang.Object;#24462446
My java code to fetch this output arraylist is like this:
List<String> goodfriends = Arrays.asList((String[]) results.get("good_friend_o");
List<String> bestfriends = Arrays.asList((String[]) results.get("best_friend_o");
Why it is not giving correct result for good_friends ?
Thanks in advance.

Your Getting the answer as Array of String, and then storing in a String of List.. You cannot store the String[] into String straightly.
can you post the, java code for fetching and printing...

Try this code. This will print the strings
System.out.println("Printing goodfriends");
for (Object object : goodfriends) {
Object[] goodfriendsString = (Object[]) object;
for (Object object2 : objLocationVO) {
System.out.println(object2);
}
}

Related

Need help to convert data in an ArrayList to Strings and Pass each of them as String parameters to a function

Suppose I have an ArrayList 'slist' that returns an arraylist of session id's .
I want to pass these session ids one by one as Strings to a function
say , getTranscript(String sessID) // The sessID is the sessionID that is returned from the ArrayList 'slist' .
I have session IDs returned from :-
ArrayList slist = WgSession.getSessionList(wgname); //Dont worry //about wgname
Now i want to pass 'slist' as Strings to the function getTranscript(String 'whatever is returned from the arraylist'); so i can get the transcript for each sessionID that is present in 'slist'
Please help ,
It would be good if CODE is given ! THANKS !
If getTranscript is void:
for (String s : slist) {
getTranscript(s);
}
If getTranscript returns a string and you would like to save it:
ArrayList<String> transcripts = new ArrayList<String>();
for (String s : slist) {
transcripts.add(getTranscript(s));
}
for(String tmp : slist) {
//do stuff
getTranscript(tmp);
}
?

How to know the type of a native SQL query result in Hibernate?

I have a native SQL query in Hibernate. I get the query result by :
List l = query.list().
I know that each element of the list corresponds to a line of the result table. But what are the types of those elements ?
Java tells me they are of type : Object. Ok but I want more. Because I want to print the results in the Eclipse console. But for that, I have to know the types, I have to know what this list exactly contains.
Here is the result table of my query I get in SL Developer :
And know, I want to print all that data in Eclipse console
In Eclipse, I use Query query = session.createSQlQuery("my query");
List l = query.list();`
For information, here is the SQL query code :
The Object in the list is an array of Objects (one per column).
You have to iterate (that's for each row) then use the array of columns:
final List<Object> l = query.list();
for (final Object row : l) {
final Object[] columns = (Object[]) row;
// use columns[0], columns[1] etc
System.out.println(columns[0]);
}
If I've got your issue correct, try getClass().getName() methods, which'll give you runtime class of your object. More info here

Convert a List<Object> to List String

Query w=em.createQuery("SELECT ar.authorId.firstName,
count(ar) FROM Article ar
WHERE ar.categoryId.categoryText=:
categoryText GROUP BY ar.authorId");
w.setParameter("categoryText", categoryText);
List list1=w.getResultList();
System.out.println(list1.toString());
Whats the wrong. i get [[Ljava.lang.Object;#60a19573, [Ljava.lang.Object;#44a085e5]
I want to print the query like "name:"+firtname+"Articles:"+count(ar)
Implement toString() in that particular type SomeType
What you're getting back in your query is a List<Object[]> which is expected as per the spec. The first element in your array is the string ar.authorId.firstName and the second is a Long representing count(ar)
A String.format should work here for you:
for(Object[] objs : list1) {
System.out.println(String.format("name %s articles %s",objs[0],objs[1]);
}
for(Object objs : list1) {
System.out.println(String.valueOf(objs));
}
valueOf is an String method which helps to convert primitive data types to String.

How to get just the desired field from an array of sub documents in Mongodb using Java

I have just started using Mongo Db . Below is my data structure .
It has an array of skillID's , each of which have an array of activeCampaigns and each activeCampaign has an array of callsByTimeZone.
What I am looking for in SQL terms is :
Select activeCampaigns.callsByTimeZone.label,
activeCampaigns.callsByTimeZone.loaded
from X
where skillID=50296 and activeCampaigns.campaign_id= 11371940
and activeCampaigns.callsByTimeZone='PT'
The output what I am expecting is to get
{"label":"PT", "loaded":1 }
The Command I used is
db.cd.find({ "skillID" : 50296 , "activeCampaigns.campaignId" : 11371940,
"activeCampaigns.callsByTimeZone.label" :"PT" },
{ "activeCampaigns.callsByTimeZone.label" : 1 ,
"activeCampaigns.callsByTimeZone.loaded" : 1 ,"_id" : 0})
The output what I am getting is everything under activeCampaigns.callsByTimeZone while I am expecting just for PT
DataStructure :
{
"skillID":50296,
"clientID":7419,
"voiceID":1,
"otherResults":7,
"activeCampaigns":
[{
"campaignId":11371940,
"campaignFileName":"Aaron.name.121.csv",
"loaded":259,
"callsByTimeZone":
[{
"label":"CT",
"loaded":6
},
{
"label":"ET",
"loaded":241
},
{
"label":"PT",
"loaded":1
}]
}]
}
I tried the same in Java.
QueryBuilder query = QueryBuilder.start().and("skillID").is(50296)
.and("activeCampaigns.campaignId").is(11371940)
.and("activeCampaigns.callsByTimeZone.label").is("PT");
BasicDBObject fields = new BasicDBObject("activeCampaigns.callsByTimeZone.label",1)
.append("activeCampaigns.callsByTimeZone.loaded",1).append("_id", 0);
DBCursor cursor = coll.find(query.get(), fields);
String campaignJson = null;
while(cursor.hasNext()) {
DBObject campaignDBO = cursor.next();
campaignJson = campaignDBO.toString();
System.out.println(campaignJson);
}
the value obtained is everything under callsByTimeZone array. I am currently parsing the JSON obtained and getting only PT values . Is there a way to just query the PT fields inside activeCampaigns.callsByTimeZone .
Thanks in advance .Sorry if this question has already been raised in the forum, I have searched a lot and failed to find a proper solution.
Thanks in advance.
There are several ways of doing it, but you should not be using String manipulation (i.e. indexOf), the performance could be horrible.
The results in the cursor are nested Maps, representing the document in the database - a Map is a good Java-representation of key-value pairs. So you can navigate to the place you need in the document, instead of having to parse it as a String. I've tested the following and it works on your test data, but you might need to tweak it if your data is not all exactly like the example:
while (cursor.hasNext()) {
DBObject campaignDBO = cursor.next();
List callsByTimezone = (List) ((DBObject) ((List) campaignDBO.get("activeCampaigns")).get(0)).get("callsByTimeZone");
DBObject valuesThatIWant;
for (Object o : callsByTimezone) {
DBObject call = (DBObject) o;
if (call.get("label").equals("PT")) {
valuesThatIWant = call;
}
}
}
Depending upon your data, you might want to add protection against null values as well.
The thing you were looking for ({"label":"PT", "loaded":1 }) is in the variable valueThatIWant. Note that this, too, is a DBObject, i.e. a Map, so if you want to see what's inside it you need to use get:
valuesThatIWant.get("label"); // will return "PT"
valuesThatIWant.get("loaded"); // will return 1
Because DBObject is effectively a Map of String to Object (i.e. Map<String, Object>) you need to cast the values that come out of it (hence the ugliness in the first bit of code in my answer) - with numbers, it will depend on how the data was loaded into the database, it might come out as an int or as a double:
String theValueOfLabel = (String) valuesThatIWant.get("label"); // will return "PT"
double theValueOfLoaded = (Double) valuesThatIWant.get("loaded"); // will return 1.0
I'd also like to point out the following from my answer:
((List) campaignDBO.get("activeCampaigns")).get(0)
This assumes that "activeCampaigns" is a) a list and in this case b) only has one entry (I'm doing get(0)).
You will also have noticed that the fields values you've set are almost entirely being ignored, and the result is most of the document, not just the fields you asked for. I'm pretty sure you can only define the top-level fields you want the query to return, so your code:
BasicDBObject fields = new BasicDBObject("activeCampaigns.callsByTimeZone.label",1)
.append("activeCampaigns.callsByTimeZone.loaded",1)
.append("_id", 0);
is actually exactly the same as:
BasicDBObject fields = new BasicDBObject("activeCampaigns", 1).append("_id", 0);
I think some of the points that will help you to work with Java & MongoDB are:
When you query the database, it will return you the whole document of
the thing that matches your query, i.e. everything from "skillID"
downwards. If you want to select the fields to return, I think those will only be top-level fields. See the documentation for more detail.
To navigate the results, you need to know that a DBObjects are returned, and that these are effectively a Map<String,
Object> in Java - you can use get to navigate to the correct node,
but you will need to cast the values into the correct shape.
Replacing while loop from your Java code with below seems to give "PT" as output.
`while(cursor.hasNext()) {
DBObject campaignDBO = cursor.next();
campaignJson = campaignDBO.get("activeCampaigns").toString();
int labelInt = campaignJson.indexOf("PT", -1);
String label = campaignJson.substring(labelInt, labelInt+2);
System.out.println(label);
}`

How to convert string array of data object to array list of another data object in java

I have vehicle data object returning string array .I want to convert or cast to Arraylist BuyingDo data object.I have ArrayList objectList
I am getting value like this
VehicleDo list[]=data.getList()
I am converting like this
objectList=(ArrayList<BuyingDo)list
but its not working can anybody tell how to do this
Your question is a bit... well... unclear.
I think you mean either this:
List<VehicleDO> realList = Arrays.asList(list);
Or this:
List<BuyingDO> realList = new ArrayList<BuyingDO>(list.length);
for (VehicleDO vdo : list){
realList.add((BuyingDO)vdo);
}
Assuming of course that VehicleDO can be cast to BuyingDO.
Something like that?

Categories