Can JNDI store a datasource? It is supposed to...

I am trying to learn how to use JNDI to store a datasource.

If I use the DataSource as below (without JNDI), the path and process works:

connection = mysqlDataSource.getConnection();

However, I want to try to retrieve the connection via a Context,

and am unsuccessful in creating the initial association, like this:

MysqlDataSource dataSource =new MysqlDataSource();

dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/test" );

dataSource.setUser( stringUser );

dataSource.setPassword( stringPass );

//

String stringDataSourceMYS ="com.mysql.jdbc.jdbc2.optional.MysqlDataSource";

Properties properties =new Properties();

properties.put( Context.INITIAL_CONTEXT_FACTORY, stringDataSourceMYS );

properties.put( Context.PROVIDER_URL ,"jdbc:mysql://127.0.0.1:3306/test" );

//

Context context =new InitialContext( properties );

context.bind("jdbc/mySql" , dataSource );

It renders the following error:

javax.naming.NoInitialContextException:

Cannot instantiateclass: com.mysql.jdbc.jdbc2.optional.MysqlDataSource

[Root exception is java.lang.ClassCastException]

This follows the example in the JDBC optional doc, but does not work.

I do not think this is a path issue (since it works w/o using the context bind).

I do not know what I need to make it "bind" properly.

Can anyone help with this? Thank You.

[1747 byte] By [mamgeorgea] at [2007-11-27 6:02:18]
# 1

Context.PROVIDER_URL is the url of the JNDI provider url not the jdbc connection string.

Context.INITIAL_CONTEXT_FACTORY is the initial context factory class name not the jdbc driver class name.

Take a look at the jndi examples for your application server!

If you are running inside the app server you probobally just need to use

Context context = new InitialContext();

DigitalDreamera at 2007-7-12 16:43:32 > top of Java-index,Java Essentials,Java Programming...
# 2
Thank you for answering. I am not using an App Server; I am trying to use a small set of java code as a "blackbox". So, I tried using an RMI server to "host" the Context, and it worked.
mamgeorgea at 2007-7-12 16:43:32 > top of Java-index,Java Essentials,Java Programming...