[英]NoInitialContextException Error Tomcat
I've been trying to find a solution here but I cant... I have the following code and i get this error. 我一直试图在这里找到一个解决方案,但我不能......我有以下代码,我得到这个错误。
Am I missing something? 我错过了什么吗? Thank you :) 谢谢 :)
Code 码
package src;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Wrapper;
import java.util.Hashtable;
import java.util.Properties;
import java.io.*;
import javax.*;
import javax.activation.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import oracle.jdbc.pool.OracleDataSource;
public class TestServlet {
@SuppressWarnings("unused")
public static void main(String[] argv) throws SQLException, NamingException {
Context initialContext = new InitialContext();
if ( initialContext == null){System.out.println("initialContext null");}
else {System.out.println("initialContext");}
// Get DataSource
Context environmentContext = (Context)initialContext.lookup("java:/comp/env");
if ( environmentContext == null){System.out.println("envContext null.");}
else {System.out.println("envContext");}
DataSource ds = (DataSource)environmentContext.lookup("jdbc/testdb");
System.out.println("\n -------- Oracle JDBC Connection Testing ------");
try {
Connection jdbcConnection = ((Statement) ds).getConnection();
OracleDataSource ods = ((Wrapper) ds).unwrap(OracleDataSource.class);
jdbcConnection.close();
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
String message = "You are connected!";
System.out.println(message);
}
}
context.xml 的context.xml
<Context>
<Resource name="jdbc/testdb"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="dba01"
password="qvE-g7Cacontext.xml"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:oracle:thin:@10.0.1.6:1521:xe"/>
</Context>
Error 错误
Exception in thread "main" 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(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at src.TestServlet.main(TestServlet.java:34)
Please let me know if you need more information! 如果您需要更多信息,请告诉我们!
You need an initial context factory. 您需要一个初始上下文工厂。 For Tomcat it is org.apache.naming.java.javaURLContextFactory
: 对于Tomcat,它是org.apache.naming.java.javaURLContextFactory
:
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.naming.java.javaURLContextFactory");
So is it the code deployed on the server? 那么部署在服务器上的代码是什么? It seems you try to access the Context using standard java as you call static main (so I guess you are calling TeestServlet.main from your runtime). 当您调用static main时,您似乎尝试使用标准java访问Context(因此我猜您从运行时调用TeestServlet.main)。 The context will be setup by the web server (Tomcat), so it is availble only after you deploy your web application to it. 上下文将由Web服务器(Tomcat)设置,因此只有在将Web应用程序部署到Web服务器之后才能使用它。
Please note that driverClassName that you are using is incorrect. 请注意,您使用的driverClassName不正确。 It should be "oracle.jdbc.OracleDriver" if you are trying to connect to Oracle Database. 如果您尝试连接到Oracle数据库,它应该是“oracle.jdbc.OracleDriver”。
<Context>
<Resource name="jdbc/testdb"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="dba01"
password="qvE-g7Cacontext.xml"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:oracle:thin:@10.0.1.6:1521:xe"/>
</Context>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.