aspose file tools*
The moose likes JDBC and the fly likes vector and resultset Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "vector and resultset" Watch "vector and resultset" New topic
Author

vector and resultset

Neel Chow
Ranch Hand

Joined: Jan 10, 2005
Posts: 55
I am writing a Type 3 JDBC Driver where network protocol is RMI.

I am invoking this remote method.
queryInf query = (queryInf)Naming.lookup("rmi://localhost:1099/queryService");

Vector queryResult = query.execute(schema,datasource,select,from);

The result is returned as a vector.

Now in my driver I need to implement the ResultSet interface(specifically next() and getString()) to find whether there is any more element and to get the element.

How can I approach to implement those method on a vector?
Is there anyway to convert a vector to a resultset?
or I have to manipulate the vector only ? If so how to maintain the state of the cursor position in a vector?

please help and let me know if you need any more information..
Thanks in advance...
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Avijit,
Take a look at Iterator. You can call vector.iterator() to get one. This implements next() for you so you can just delegate to the iterator.

You would still need to implement the rest of the methods on ResultSet though. For the getXXX() ones, you can call getNext() on the Iterator and then convert to the appropriate value.

Just curious - why are you writing your own driver? Most database vendors provide one.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Neel Chow
Ranch Hand

Joined: Jan 10, 2005
Posts: 55
I am writing this Driver for one of my application called Data Mediator.
So in other words you can say it is a application specific driver.
The data mediator application converts the data from various source into a local schema known to user. In fact the user gives query in the local schema even though he/she is not aware of the schema of the foreign database. There are some XML parsing involved in that.
I have used RMI for making the application disttributed. Now I am writing the driver so that by only changing the url one could select and change the schema and datasource.
the url is like
jdbc:mediator:localhost:1099:local:foreign

I hope I answered your question.
Thanks for your help.I will look into iterator. Please let me know if you have any more idea.
Thanks
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1373
Why don't u try thru Type 4 driver. Because Type 3 is expensive driver.
Neel Chow
Ranch Hand

Joined: Jan 10, 2005
Posts: 55
I believe for Type 4 driver we have to use different driver for different database. The type 4 driver converts the JDBC calls into the vendor specific database management system protocols so that client application can communicate directly with the database server.
But in my application I dont want that. It is a 3 tier application where client connects to the middle tier(mediator application) using RMI as net protocol.
The jdbc calls from the client is passed to the middle tier through RMI. The middle tier then takes care of the rest of the thing (connecting to the back end database, mediating between database etcetera)

Do you see my point ??
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I believe Jeanne is correct, you just need to implement an adapter (a nice example of this pattern is in Head First Design Patterns, but that's heading off topic)

You just need something like this:


The internal iterator is used to step forward, and the 'current' is used to hold the window on the current item, hopefully it mimc the RS behaviour implemented by an internal iterator. You could always use v.get(i), but I guess it depends on your application.

Dave
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Avijit,
That's a good reason for writing a driver. Very interesting!

One thing I would change in Dave's adapter: Pass in a List instead of a Vector. It makes the code more flexible because you can pass in an ArrayList in the future without changing the code.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Design to the interface, good point. My fault for following the instructions implicitly
Neel Chow
Ranch Hand

Joined: Jan 10, 2005
Posts: 55
"Pass in a List instead of a Vector"
I wish I could do that. I wish I could just pass in a resultset.
But the problem is in the mediator application ,I am getting the resultset from the database, storing the values in a vector , then I am doing some other operations on it(translating etc.)
That's why I am passing the vector.
I am working on Dave suggestion(iterator()).Seems like I got a lead now.
I will let you know in case I have any further problem.
Thanks for all the valuable advise.
Neel Chow
Ranch Hand

Joined: Jan 10, 2005
Posts: 55
Ahoy !!! It works....
one small thing....
boolean result = iter.hasNext();
if(result)
{
current = iter.next();
}
return result;

I think we are moving the position of the cursor two position ahead.
(I tried that and I got the all the even records missing..)
What I did is i just returned result ..
Am I right or there is something I am missing...
Thanks
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Jeannes point (and a good one) was to make this small change to the constructor:


A vector is a List so you can still pass in your Vector as normal.
I don't think the code above will cause it to skip the odd records. Iterator.hasNext() peeks at the next to see if it exists, Iterator.next() moves it forward. As long as you use current to return values in your getters and not iter.next() you should be fine.

[EDIT - formatted the code a bit]
[ March 08, 2005: Message edited by: David O'Meara ]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Avijit,
What method is the code you posted from? It may be a semantics issue of something getting called twice.

Dave's code example is exactly what I meant. Since you are only using the iterator() method of Vector, you can pass in any class interface that implements it. In this case, your main choices are Vector, List and Collection. I would avoid Vector because it is too specific. Collection has different (unordered) semantics, so it means something different than what you want. List is right in the middle, so the best choice here.
Neel Chow
Ranch Hand

Joined: Jan 10, 2005
Posts: 55
Hi David
You are right..I am using next() in my getters instead of the current....thats why i missed some elements..
I made the correction...thanks
 
wood burning stoves
 
subject: vector and resultset