Hello.
Can someone explain what this InitialContext(): is looking for?

public JDBCSchoolofdbDAO() {
        try {
            Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup(
                    ModelUtils.getResource("JavaCompEnv"));
            dataSource = (DataSource) envCtx.lookup(
                    ModelUtils.getResource("DataSource"));
        } catch (NamingException x) {
            ModelUtils.log(x);
            throw new InternalError(x.getMessage());
        }
    }
DAO=schoolAppModel.dao.JDBCSchoolofdbDAO



#/////////////////////////////////////////////////


# DAO=jsfdb.model.dao.JDBCSubscriberDAO


#/////////////////////////////////////////////////



JavaCompEnv=java:comp/env
DataSource=jdbc:mysql://localhost:3306/schoolofdb 
user=root
password=xxxxxxxx
pr 16, 2010 8:12:31 AM model.ModelUtils log
SEVERE: 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
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(NamingManager.java:645)
        at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
        at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
        at javax.naming.InitialContext.lookup(InitialContext.java:392)
        at model.dao.JDBCSchoolofdbDAO.<init>(JDBCSchoolofdbDAO.java:35)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at java.lang.Class.newInstance0(Class.java:355)
        at java.lang.Class.newInstance(Class.java:308)
        at model.ModelUtils.getNewUserDAO(ModelUtils.java:49)
        at view.LoginInfoBean.loginAction(LoginInfoBean.java:50)
        at view.SchoolJDesktopPane.login(SchoolJDesktopPane.java:157)
        at view.SchoolJDesktopPane.<init>(SchoolJDesktopPane.java:106)
        at view.Main.createJDesktopPane(Main.java:35)
        at view.Main.main(Main.java:30)
Exception in thread "main" java.lang.InternalError: 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 model.dao.JDBCSchoolofdbDAO.<init>(JDBCSchoolofdbDAO.java:41)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)

Dani AI

Generated

the stacktrace is telling you there is no JNDI provider available in your desktop JVM. In Java SE, new InitialContext() only works if you supply an initial context factory (via system props or jndi.properties) or you run inside a Java EE container. Otherwise you get NoInitialContextException complaining about java.naming.factory.initial. Also, java:comp/env exists only for components running in a container; a plain Swing app has no such context. Inside an app server, you bind JDBC resources under names like jdbc/SomeDS (optionally referenced as java:comp/env/jdbc/...). The JDBC URL you put under DataSource= is not a JNDI name. (docs.oracle.com)

If this is a desktop app, is right: skip JNDI and connect directly with JDBC. With MySQL Connector/J, a minimal connection looks like this:

// add mysql-connector-j to your classpath
try (Connection c = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/schoolofdb?user=root&password=secret")) {
    // use c...
}

DriverManager is the supported approach outside an app server. (dev.mysql.com)

If you really want JNDI from a desktop client against GlassFish, you must 1) create a JDBC connection pool and a JDBC resource named, say, jdbc/schoolofdb, and 2) tell your client which naming factory and ORB host/port to use (or include gf-client.jar, which carries these settings). Example:

Hashtable<String, String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
        "com.sun.enterprise.naming.SerialInitContextFactory");
env.put("org.omg.CORBA.ORBInitialHost", "localhost");
env.put("org.omg.CORBA.ORBInitialPort", "3700");
Context ctx = new InitialContext(env);
DataSource ds = (DataSource) ctx.lookup("jdbc/schoolofdb");

The key is that the name you look up (jdbc/...) must match the resource you configured in GlassFish. (glassfish.org)

Recommended Answers

All 7 Replies

AFAIK, you need to have a JNDI server running to make any sense of InitialContext. This isn't a problem with managed environments like a servlet container or an application server since all the configurations needed for an InitialContext lookup are already taken care of.

How are you exactly running this application? Standalone java program? A web application? If the latter, which server are you using? Creating a Datasource and publishing it as a JNDI resource requires a bit of configuration which is different for each application server.

If it's the former, I personally haven't worked with standalone Java applications which make a reference to a JNDI resource. However, might help you out.

Hows it going thanks for reply

I got an example that was MVC web app and I am trying to use that structure. I is confusing because I have to figure out what I don't need. I am building a JDesktopPane App which is trying to connect to a database I built.

thanks for the link I think it will help when I change this pseudo program to web app.

Hows it going thanks for reply

I got an example that was MVC web app and I am trying to use that structure. I is confusing because I have to figure out what I don't need. I am building a JDesktopPane App which is trying to connect to a database I built.

thanks for the link I think it will help when I change this pseudo program to web app.

The server is a personal GlassFish v3 Domain.

I don't under stand what my property value should be just to connect

#
 
#
JavaCompEnv=java:comp/env

If you are using Glassfish, you need to configure a datasource, publish it to the JNDI server and then access it using the specified JNDI name. for more details.

For standalone applications, creating a JNDI datasource seems like an overkill. For your desktop application which database are you using? Is is used in server mode or embedded mode? I can't recommend an appropriate approach unless I know this detail.

If you are using Glassfish, you need to configure a datasource, publish it to the JNDI server and then access it using the specified JNDI name. for more details.

For standalone applications, creating a JNDI datasource seems like an overkill. For your desktop application which database are you using? Is is used in server mode or embedded mode? I can't recommend an appropriate approach unless I know this detail.

The database is MySQL . I am not sure of the type. I believe it is embedded.

The database is MySQL . I am not sure of the type. I believe it is embedded.

Looking at the configuration in your first post, it doesn't seem like an embedded database to me; more like MySQL running in server mode. This setup won't work for a desktop application unless you plan on making your users run a MySQL server instance on their machines before using your application. For using embedded databases, google for 'java embedded database sample'.

On a related note, please don't ask help via PM's; since I don't have the time to render 1:1 help. Ask questions here on forums where everyone would have a look at your code and others would be benefited with the answers.

Thanks for the links I have a lot of reading to do
an embeded MySql may do the trick. I'll have to look into that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.