Dear Forum Members,
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. This Java application will be used in a standalone PC installed with Windows XP Professional with no LAN / WAN connection. Of course, Internet connection is available with the PC.
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. I want to use 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 only 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 placed under D:\E-DRIVE directory or C:\ directory of the hard disk. 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 screen has options for General, Advanced and Additional Settings tabs and I made all the settings relevant to the database I created in D:\E-DRIVE\SAPDEV.
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("java:comp/env/ABAPRPY");
} catch (Exception ex) {
Logger.getLogger(DBConnection.class.getName()).log (Level.SEVERE, null, ex);
}
}
}
When I attempted to compile the above program in NetBeans IDE ,no compilation error reported. But while executing the program, I got the following run-time error message:
SEVERE: null
javax.naming.NameNotFoundException: No object bound for java:comp/env/jdbc/SAPDEV [Root exception is java.lang.NullPointerException]
at com.sun.enterprise.naming.java.javaURLContext.look up(javaURLContext.java:224)
at com.sun.enterprise.naming.SerialContext.lookup(Ser ialContext.java:396)
at javax.naming.InitialContext.lookup(InitialContext. java:392)
at JNDI.Attrib.main(Attrib.java:23)
Caused by: java.lang.NullPointerException
at com.sun.enterprise.naming.java.javaURLContext.look up(javaURLContext.java:173)
... 3 more
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 using Connection Pool created in Sun Java System Application Server using Admin Console tool.
I request dear forum members to provide me an appropriate solution.
Thanks and regards,
K. Rangarajan