Today, learning to use a Nutz framework, wrote a HelloWorld applet, when using Jndi configuration data source, wrote a test class, and in the main method called Jndi to obtain a database connection, but error message, the following:
Javax.naming.NoInitialContextException:Need to specify class name in environment or system property, or as an applet para meter, or in an application resource file:java.naming.factory.initial.
What is the problem, broadly speaking, is the context of the initial exception! I looked at my configuration.
Steps:
1. Add code to the Conf/context.xml file in the Tomcat directory
<resource name= "Jdbc/test"
Auth= "Container"
Type= "Javax.sql.DataSource"
Driverclassname= "Net.sourceforge.jtds.jdbc.Driver"
Url= "Jdbc:sqlserver://localhost:1433;databasename=nutztest"
Username= "SA"
password= "SA"
Maxactive= "100"///connection pool maximum database connections, set to 0 means no limit
maxidle= "30"//MAX Idle, set to 0 for unlimited
maxwait= "10000"/>//Maximum connection wait time. If the timeout is received, the exception will be given. Set to 1 means no limit
2. Put the Jtds-1.2.5jar package, Nutz.jar into the project
3. Write Test class
public class test{
Public Connection getconnection () {
try {
Context CTX = new InitialContext ();
DataSource ds = (DataSource) ctx.lookup ("Jdbc/test");
conn = Ds.getconnection ();
} catch (Namingexception e) {
E.printstacktrace ();
} catch (SQLException e) {
E.printstacktrace ();
}
Return conn;
}
public static void Main (string[] args) {
System.out.println (Newtest (). getconnection ());
}
}
4. Run tomcat--to run the test code and throw an exception:
Javax.naming.NoInitialContextException:Need to specify class name in environment or system property, or as an applet para meter, 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)
later see the forum reply to know the reason
When using Jndi to connect to the database with the main function to test the total error, InitialContext is to be in the context of the application server.
How do you do in a normal app!
Put it in Tomcat and run to see it! Cannot be tested directly with the main function, only in Tomcat or servlet, JSP display
Javax.naming.NoInitialContextException the wrong solution