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.