0 Replies Latest reply on Jan 12, 2011 10:16 AM by softwareenganeer

    Unable to create the InitialContext object for JNDI service

    softwareenganeer

      Hi,

       

      I am new to JNDI, Just I want to use the "Connection Pool" which is created by JBOSS server but unfortunatly I am getting the following error:

       

       

      Exception in thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

       

      NOTE: I am using Jboss studio2.0 so that I didn't add any external jar files


      My code is as follows:

      1. "mysql-ds.xml" file is as follows, I copied this file to "jboss-as\server\default\deploy"  folder

       

      <datasources>

        <local-tx-datasource>

          <jndi-name>jdbc/mysql1</jndi-name>

          <use-java-context>false</use-java-context>

          <connection-url>jdbc:mysql://<hostname>/diners</connection-url>

          <driver-class>com.mysql.jdbc.Driver</driver-class>

          <user-name>diners</user-name>

          <password>diners</password>

          <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>

              <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->

          <metadata>

             <type-mapping>mySQL</type-mapping>

          </metadata>

        </local-tx-datasource>

      </datasources>

       

      2. "jndi.properties" file is as follows, This file is available in my classpath

       

      ### JBossNS properties

      java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory

      java.naming.provider.url=jnp://localhost:1099

      java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces

       

      3."DataSourceTest.java" file as follows:

      package com.pojo;

      import java.sql.Connection;

      import java.sql.ResultSet;

      import java.sql.SQLException;

      import java.sql.Statement;

       

      import javax.naming.InitialContext;

      import javax.naming.NamingException;

      import javax.sql.DataSource;

       

       

      public class DataSourceTest {

      public static void main(String[] args) throws Exception {

      testDataSource();

      }

       

      private static void testDataSource()

      throws NamingException, SQLException {

      final String sql = "select version()";

      InitialContext ic = new InitialContext();

      DataSource ds = (DataSource) ic.lookup("jdbc/mysql1");

      Connection con = null;

      Statement stmt = null;

      ResultSet rs = null;

      try {

      con = ds.getConnection();

      stmt = con.createStatement();

      rs = stmt.executeQuery(sql);

      while(rs.next()) {

          System.out.println("Query '" + sql + "' returned " + rs.getString(1));

      }

      } finally {

      if(rs != null) rs.close();

      if(stmt != null) stmt.close();

      if(con != null) con.close();

      }

      }

      }

       

       

      Thanks in advance!!!!!!!!!!!!!!!!!

       

       

      Regards,

      BAJI