JDBC DataSource NoInitialContextException


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: JDBC DataSource NoInitialContextException

  1. #1
    Join Date
    May 2007
    Posts
    4

    Red face Can MySQL DataSource be created 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]


    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.
    Last edited by mamgeorge; 05-31-2007 at 10:24 AM. Reason: clarity

  2. #2
    Join Date
    May 2007
    Posts
    4

    It can be done.

    The properties of the Context do not show an appropriate provider url for the hosting of the datasource. If you are avoiding an App server (for a blackbox demo, for example) create an RMI registry server, create the datasource (with its own URL), and bind the datasource to the context with the provider url of the RMI server:

    LocateRegistry.createRegistry( intPortRMI );
    //
    MysqlDataSource mysDataSource = new MysqlDataSource();
    mysDataSource.setUrl ( stringUrlMYS ); // setPort setServerName
    mysDataSource.setUser ( stringUser );
    mysDataSource.setPassword ( stringPass );
    DataSource dataSource = ( DataSource ) mysDataSource ;
    //
    Properties properties = new Properties();
    properties.put( Context.INITIAL_CONTEXT_FACTORY, stringFactRmi );
    properties.put( Context.PROVIDER_URL, stringUrlRmi );
    //
    InitialContext context = new InitialContext( properties );
    context.rebind( "anyLookupName" , mysDataSource );

Similar Threads

  1. Replies: 0
    Last Post: 03-27-2007, 09:56 AM
  2. Replies: 0
    Last Post: 11-22-2006, 10:06 AM
  3. JDBC configuration in Linux
    By manoj nahar in forum Database
    Replies: 0
    Last Post: 03-07-2002, 01:11 AM
  4. java jdbc connection on w2k, Oracle8i, JDK1.3
    By frest in forum oracle.general
    Replies: 0
    Last Post: 05-01-2001, 07:33 AM
  5. Re: How to set the DataSource to UserControl
    By Nick Koszykowski in forum VB Classic
    Replies: 0
    Last Post: 03-28-2000, 02:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center