Meaningless Drivel is fun!*
The moose likes JDBC and the fly likes Convert DataSource to otherdatasource Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Convert DataSource to otherdatasource" Watch "Convert DataSource to otherdatasource" New topic
Author

Convert DataSource to otherdatasource

sushi sri
Ranch Hand

Joined: Oct 27, 2009
Posts: 38
Hi,


i'm using appserver's datasource for my database access.
So, when i do jndi lookup i'm getting Datasource with ctx.lookup("ds");
Now i've change it to SQLServerDataSource.

How can we do this?

Thanks

Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3703
    
    5

You can cast it to SQLServerDataSource since DataSource is just an interface. In other words, all instantiated objects have a concrete class. The object is either a SQLServerDataSource or some other concrete object, regardless of the pointer class.

Keep in mind, you should check for class-safe compatibility by using the instanceof operator.


My Blog: Down Home Country Coding with Scott Selikoff
sushi sri
Ranch Hand

Joined: Oct 27, 2009
Posts: 38
I did like this earlier.

class TestdataSource impements DataSource
{
---
}

Datasource ds = new TestdataSource();
if( ds instanceof SQLServerdatasource)
SQLServerdatasource sds = (SQLServerdatasource)ds;
where SQLServerDataSource implements DataSource.

This is my requirement. when i did like this i got error as known classcastexception.
I know we can't compare interface to class. But thisis my requirement.

Need help
Thanks
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3703
    
    5

Why are you implementing your own data source, TestdataSource? You rarely make your own data source, other than as a wrapper.

As you have defined it TestdataSource is not an instance of SQLServerdatasource.
sushi sri
Ranch Hand

Joined: Oct 27, 2009
Posts: 38
Thanks for the response scott.

This is to implement the proxy design pattern which is my cleint requirement.
I've to proceed like this only for the SQLServerDataSource.

OK, if i do testdatasource extends SQLserverDataSource i can achieve want i want.(setting the applicationname).

But i'm having problem in accessing the datasource as specified earlier.
Which is the retrieval of sqlserverdatasource from appserver's datasource.
i changed the type of datasource to sqlserverdatasource and factory to sqlserverdatasourceobjectfactory from BasicDataSourceFactory.
But still having runtime problems which says "javax.naming.NamingException: Invalid DataSource reference"


2. if i can't get SQLServerDataSource from appaserver then, can we do this?

The datasource i'm getting with out any changes to type & factory values to sqlserver is basicdatasource.
Which is instance of basicdatasource.(org.apache.tomcat.dbcp.dbcp.BasicDataSource -- which extendsObject implements Datasource) .
If above is not possible then, can we change basicDataSource to SQLServerDataSource.
If so how can i do that?

Thanks for th



How can we do this?
Any suggestion or help required.

Thnaks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Convert DataSource to otherdatasource
 
Similar Threads
JNDI Lookup help in websphere 4.0
Not able to perform JNDI lookup in my servlet
could not to connect to my sql server by datasource
Coonection pooling in WAS6.1
Not able to perform JNDI lookup