Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problems in getting database connection from data source registerd with JNDI

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problems in getting database connection from data source registerd with JNDI

    Dear Forum Members,

    I have created JDBC Connection pool using Admin Console tool of Sun Java System Application Server. I have done using the menu path Common Tasks->Resources->JDBC->Connection Pools. I am using JavaDB as back-end database for my JDBC application.

    The JNDI name of the connection pool I have created is SalesDBPool.
    The resource type is javax.sql.DataSource.
    Datasource class name is org.apache.derby.jdbc.ClientDataSource.

    With reference to the newly created connection pool SalesDBPool, I have also created JDBC Resource using the menu path Common Tasks->Resources->JDBC->JDBC Resources. The JNDI name of the created JDBC resource is : jdbc/SalesDB.

    I have checked whether the settings I have made are correct or not by pressing the Ping push button of the Connection Pool SalesDBPool and the system responded with the message "Ping Succeeded".
    With this background, that is, keeping the Sun Java System Application Server running in the background, I tried to create a Java Class using NetBeans IDE. The source code of the program is given below:

    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.naming.*;
    import javax.sql.*;
    import java.sql.*;

    public class DataSourceApp {
    public static void main(String[] args) {
    try {
    Context ctx = new InitialContext();
    DataSource ds = (DataSource) ctx.lookup("jdbc/SalesDB");
    Connection con = ds.getConnection();
    DatabaseMetaData dbmd = con.getMetaData();
    System.out.println("Database Product Name: "+dbmd.getDatabaseProductName());
    } catch (NamingException ex) {
    Logger.getLogger(DataSourceApp.class.getName()).lo g(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
    Logger.getLogger(DataSourceApp.class.getName()).lo g(Level.SEVERE, null, ex);
    }
    }
    }

    When I compiled the above program, no errors were reported and the compilation was successful. When I tried to execute the above program, I received the following runtime error message:
    SEVERE: null
    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
    at javax.naming.spi.NamingManager.getInitialContext(N amingManager.java:645)
    at javax.naming.InitialContext.getDefaultInitCtx(Init ialContext.java:288)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx (InitialContext.java:325)
    at javax.naming.InitialContext.lookup(InitialContext. java:392)
    at DataSourceApp.main(DataSourceApp.java:21)
    BUILD SUCCESSFUL (total time: 0 seconds)

    I also tried changing the string in the lookup() method by putting "java:comp/env/jdbc/salesDB"; still I got the same error method.

    Then I tried adding the following two statements before calling the InitalContext() method:
    java.util.Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.j ndi.fscontext.RefFSContextFactory");
    env.put(Context.PROVIDER_URL,"java:comp/env/jdbc/SalesDB");
    Context ctx = new InitialContext(env);
    But, even this did not work; I got the different run-time error message as stated below:
    javax.naming.InvalidNameException: java:comp/env/jdbc/SAPDEV [Root exception is java.net.MalformedURLException: unknown protocol: java]
    at com.sun.jndi.fscontext.FSContextFactory.getFileNam eFromURLString(FSContextFactory.java:119)
    at com.sun.jndi.fscontext.RefFSContextFactory.createC ontext(RefFSContextFactory.java:41)
    at com.sun.jndi.fscontext.RefFSContextFactory.createC ontextAux(RefFSContextFactory.java:47)
    at com.sun.jndi.fscontext.FSContextFactory.getInitial Context(FSContextFactory.java:49)
    at javax.naming.spi.NamingManager.getInitialContext(N amingManager.java:667)
    at javax.naming.InitialContext.getDefaultInitCtx(Init ialContext.java:288)
    at javax.naming.InitialContext.init(InitialContext.ja va:223)
    at javax.naming.InitialContext.<init>(InitialContext. java:197)
    at DataSourceApp.main(DataSourceApp.java:23)
    Caused by: java.net.MalformedURLException: unknown protocol: java
    at java.net.URL.<init>(URL.java:574)
    at java.net.URL.<init>(URL.java:464)
    at java.net.URL.<init>(URL.java:413)
    at com.sun.jndi.fscontext.FSContextFactory.getFileNam eFromURLString(FSContextFactory.java:117)

    I request any of our forum members to kindly provide a solution to solve this issue.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Problems in getting database connection from data source registerd with JNDI

    This is how we define JNDI datasources.

    In the application context XML file.

              <Resource name="jdbc/someName" auth="Container" type="javax.sql.DataSource" 
              	url="jdbc:mysql://127.0.0.1/databaseNameHere" 
              	driverClassName="com.mysql.jdbc.Driver" 
              	username="dbUsername" password="dbPassword"
              	maxActive="20" maxIdle="10" maxWait="100"/>

    After this we use the following code to get the datasource in Java.

            try {
                final Context initialContext = new InitialContext();
                dataSource = (DataSource) initialContext.lookup("java:comp/env/jdbc/someName");
            } catch (NamingException e) {
                // Log something here
            }

    Thats about it. Hope any of that helps.

    // Json

  3. #3
    Junior Member
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Re: Problems in getting database connection from data source registerd with JNDI

    Dear Sir,

    I tried the solution offered by you. Still, I am getting same run-time error. Let me clarify what I did with respect to the solution you offered.

    I created an XML file web.xml which is placed in the same classpath as my Java source file (.java) and complied .class file. The listing of the XML is pasted here:

    <?xml version="1.0" encoding="UTF-8"?>

    <!--
    Document : web.xml
    Created on : 19 September, 2009, 9:18 AM
    Author : Admin
    Description:
    Purpose of the document follows.
    -->

    <Resource name="jdbc/__sapdev" auth="Container"
    type="javax.sql.DataSource" driverClassName="org.apache.derby.jdbc.ClientDrive r"
    url="jdbc:derby://localhost:1527/F:/RANGA/SAPDEV;create=true"
    username="rangarajan" password="dellpc" maxActive="20" maxIdle="10"
    maxWait="-1"/>

    <resource-ref>
    <description>Connection Pool for database stored directory path F:/RANGA/SAPDEV</description>
    <res-ref-name>jdbc/__sapdev</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    </resource-ref>

    My java source code is pasted here:

    package learnjava;

    import java.sql.*;
    import java.util.Properties;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.naming.*;
    import javax.sql.DataSource;

    /**
    *
    * @author Admin
    */
    public class SAPConnection {

    public static void main(String[] args) {
    try {
    Properties props = new Properties();
    props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
    Context ctx = new InitialContext(props);
    DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/__sapdev");
    Connection con = ds.getConnection();
    } catch (NamingException ex) {
    Logger.getLogger(SAPConnection.class.getName()).lo g(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
    Logger.getLogger(SAPConnection.class.getName()).lo g(Level.SEVERE, null, ex);
    }
    }
    }

    I have created JBDC Resource in Sun GlassFish Enerprise Server v2.1 with JNDI name as "jdbc/__sapdev".
    The poolname for this JDBC Resource is "sapdev". For the JNDI name "sapdev",
    the Resource Type is: javax.sql.DataSource
    the Datasource class name is: org.apache.derby.jdbc.ClientDataSource
    I have made sure that the GlassFish Enterprise server is running in the background.

    Now, what I am supposed to do to avoid the run time error.

    I thank you so much for your sincere efforts to offer solution for the problems I posted in this forum.

    With best wishes and regards,

    K. Rangarajan.

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Problems in getting database connection from data source registerd with JNDI

    No, the resource does not go in your web.xml, it goes in your applications context.xml file. So in $CATALINA_HOME/conf/Catalina/localhost/myApplicationName.xml

    // Json

  5. The Following User Says Thank You to Json For This Useful Post:

    rangarajank (September 19th, 2009)

  6. #5
    Junior Member
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problems in getting database connection from data source registerd with JNDI

    Thanks a lot Sir! I will revert back I encounter any problem

Similar Threads

  1. If you have any .NET problems
    By antony_t in forum The Cafe
    Replies: 1
    Last Post: August 26th, 2009, 10:49 AM
  2. ha-jndi client is servlet
    By supriya ramjee in forum Web Frameworks
    Replies: 0
    Last Post: July 31st, 2009, 02:02 AM
  3. cant get rid of http connection
    By kartik in forum Java Networking
    Replies: 1
    Last Post: July 21st, 2009, 03:09 AM
  4. Problems in setting classpath
    By missyati in forum Java Theory & Questions
    Replies: 3
    Last Post: June 30th, 2009, 12:43 AM