users@glassfish.java.net

Unable to sort out issues of JNDI JDBC Connectivity

From: <glassfish_at_javadesktop.org>
Date: Mon, 21 Sep 2009 21:07:32 PDT

I want to create a stand-alone JDBC application with Java SE using Swing technologies and JNDI technology. The purpose of using JNDI technology is to avoid change of Java Source Code every time I move the database to different location.

I use JavaDB to store the data tables and the location of the database is D:\E-DRIVE\SAPDEV. Tomorrow, if I move this database to C:\SAPDEV or any network drive, I do not want to change the Java Source code. That is why I want to JNDI which, if I am not wrong, helps developers to avoid manual change of Java source code whenever the database location is changed. Changes have to be made in the JNDI Name which contains all relevant information about the database in order to get connection no matter where the database SAPDEV is stored; it can be D:\E-DRIVE or C:\E-DRIVE.

To implement my intention, I started developing Java application as per the steps mentioned below:

Step 1:

To proceed, first, I sought the help of Sun Java System Application Server Admin Console. I created JNDI object for Connection Pool using the menu path Common Tasks->Resources->JDBC->Connection Pools.
JNDI Name : ABAPRPY
        Resource Type : javax.sql.DataSource
        Datasource class : org.apache.derby.jdbc.ClientDataSource
        Description : ABAP Program Repository
        
The Connection Pool creation also has options for Advanced Settings and Additional Settings relevant to database I created in D:\E-DRIVE\SAPDEV and I made all those relevant settings.

To confirm whether the above settings are correct, I pressed the Ping push button which is available in the General tab of the connection pool creation screen. The system responded with the message Ping Succeeded.

Step 2:

I created a JDBC Resource using the menu path Common Tasks->Resources->JDBC->JDBC Resources.
        JNDI Name : jdbc/SAPDEV
        Pool Name : ABAPRPY
        Description : Database Connection for SAPDEV database
        Status : Enabled
        
Step 3:

I have made sure that [b]Sun Java System Application Server[/b] is up and running in the background with JavaDB server. I created a Java Program making sure the following JAR files are included in the classpath:

        appserv-admin.jar
                appserv-ee.jar
                appserv-rt.jar
                javaee.jar
                fscontext.jar
   Plus, the lib directory of JDK 1.6

Source code of the program is as follows: I used NetBeans IDE to create my project file.

import javax.naming.*;
import org.apache.derby.jdbc.ClientDataSource;

/**
 *
 */
public class DBConnection {
    public static void main(String[] args) {
        try {
            Context ctx = new InitialContext();
            ClientDataSource ds = (ClientDataSource) ctx.lookup("ABAPRPY");
        } catch (NamingException ex) {
            System.err.println("Error: "+ex.getClass()+" "+ex.getMessage());
        }
    }
}

When I attempted to compile the above source in NetBeans IDE ,no compilation error reported. But while executing the program, I got the following run-time error message:

init:
deps-jar:
Compiling 1 source file to D:\E-DRIVE\RANGA\java\LearnJava\build\classes
compile-single:
run-single:
Error: class javax.naming.NameNotFoundException ABAPRPY not found

Now, I want to come out of this situation; at the same time, I want to preserve the settings I have made in the Sun Java System Application Server Admin Console. That is, I want to programmatically access the data source registered with JNDI whose settings have been made in Sun Java System Application Server thriugh Admin Console utility.

I request dear forum members to provide me an appropriate solution.

Thanks and regards,

KI want to create a stand-alone JDBC application with Java SE using Swing technologies and JNDI technology. The purpose of using JNDI technology is to avoid change of Java Source Code every time I move the database to different location.

I use JavaDB to store the data tables and the location of the database is D:\E-DRIVE\SAPDEV. Tomorrow, if I move this database to C:\SAPDEV or any network drive, I do not want to change the Java Source code. That is why I want to JNDI which, if I am not wrong, helps developers to avoid manual change of Java source code whenever the database location is changed. Changes have to be made in the JNDI Name which contains all relevant information about the database in order to get connection no matter where the database SAPDEV is stored; it can be D:\E-DRIVE or C:\E-DRIVE.

To implement my intention, I started developing Java application as per the steps mentioned below:

Step 1:

To proceed, first, I sought the help of Sun Java System Application Server Admin Console. I created JNDI object for Connection Pool using the menu path Common Tasks->Resources->JDBC->Connection Pools.
JNDI Name : ABAPRPY
        Resource Type : javax.sql.DataSource
        Datasource class : org.apache.derby.jdbc.ClientDataSource
        Description : ABAP Program Repository
        
The Connection Pool creation also has options for Advanced Settings and Additional Settings relevant to database I created in D:\E-DRIVE\SAPDEV and I made all those relevant settings.

To confirm whether the above settings are correct, I pressed the Ping push button which is available in the General tab of the connection pool creation screen. The system responded with the message Ping Succeeded.

Step 2:

I created a JDBC Resource using the menu path Common Tasks->Resources->JDBC->JDBC Resources.
        JNDI Name : jdbc/SAPDEV
        Pool Name : ABAPRPY
        Description : Database Connection for SAPDEV database
        Status : Enabled
        
Step 3:

I have made sure that Sun Java System Application Server is up and running in the background with JavaDB server. I created a Java Program making sure the following JAR files are included in the classpath:

        appserv-admin.jar
appserv-ee.jar
appserv-rt.jar
javaee.jar
fscontext.jar
        Plus, the lib directory of JDK 1.6
Source code of the program is as follows: I used NetBeans IDE to create my project file.

import javax.naming.*;
import org.apache.derby.jdbc.ClientDataSource;

/**
 *
 */
public class DBConnection {
    public static void main(String[] args) {
        try {
            Context ctx = new InitialContext();
            ClientDataSource ds = (ClientDataSource) ctx.lookup("ABAPRPY");
        } catch (NamingException ex) {
            System.err.println("Error: "+ex.getClass()+" "+ex.getMessage());
        }
    }
}

When I attempted to compile the above source in NetBeans IDE ,no compilation error reported. But while executing the program, I got the following run-time error message:

init:
deps-jar:
Compiling 1 source file to D:\E-DRIVE\RANGA\java\LearnJava\build\classes
compile-single:
run-single:
Error: class javax.naming.NameNotFoundException ABAPRPY not found
Now, I want to come out of this situation; at the same time, I want to preserve the settings I have made in the Sun Java System Application Server Admin Console. That is, I want to programmatically access the data source registered with JNDI whose settings have been made in Sun Java System Application Server Admin Console.

I request dear forum members to provide me an appropriate solution.

Thanks and Regards,

Abitha Rangarajan.
[Message sent by forum member 'abith74' (abith74_at_rediffmail.com)]

http://forums.java.net/jive/thread.jspa?messageID=365003