File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    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
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert DataSource to otherdatasource