Javax.naming.NoInitialContextException the wrong solution

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.