Can you create a MySql DataSource in a Context?

I am trying to learn how to use datasources to get connections, and I am successful in doing this for the MysqlDataSource in this way:

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 instantiate class: 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.

[1713 byte] By [mamgeorgea] at [2007-11-27 5:47:52]
# 1
Yes, you can.I was not using an App Server because I was 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 15:32:46 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...