线程“main”中的异常javax.naming.NoInitialContextException:无法实例化类

时间:2015-07-10 09:57:00

标签: java database oracle connection hashtable

数据库连接。我知道有多种方法可以解决这个问题,但我总是遇到这些问题。

java.naming.factory.internal这个想要实例化oracle.aurora.jndi.ojds.OjdsInternalContentFactory的事情,这就是失败:

Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: oracle.aurora.jndi.ojds.OjdsInitialContextFactory [Root exception is java.lang.ClassNotFoundException: oracle.aurora.jndi.ojds.OjdsInitialContextFactory]
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:674)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
    at javax.naming.InitialContext.init(InitialContext.java:244)
    at javax.naming.InitialContext.<init>(InitialContext.java:216)
    at DB.DBConnector.OpenConnection(DBConnector.java:32)
    at DB.DBConnector.OpenConnection(DBConnector.java:48)
    at DB.Start.main(Start.java:22)
Caused by: java.lang.ClassNotFoundException: oracle.aurora.jndi.ojds.OjdsInitialContextFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:72)
    at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:61)
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:672)
    ... 6 more

这是我的计划:

package DB;

import java.sql.*;
import java.util.Hashtable;

import javax.naming.*;
import javax.sql.DataSource;


public class DBConnector 
{
       private String username;
       private String password;
       Connection con = null;


       public void setUsername(String u)
       {
           username=u;
       }
       public void setPassword(String p)
       {
           password=p;
       }

       public Connection OpenConnection() throws Exception
       {
           Hashtable env = new Hashtable();
           env.put("java.naming.factory.initial", "oracle.aurora.jndi.ojds.OjdsInitialContextFactory");
           env.put(Context.PROVIDER_URL,"ojds://thin:VAULAB2:1521:TLMSMP");

           Context ctx = new InitialContext(env);
            try
            {
              DataSource ds = (DataSource) ctx; //new InitialContext().lookup( "DBconfig" );
              con = ds.getConnection(username,password);
              return con;             
            }
            finally
            {

            }
       }
       public Connection OpenConnection(String u, String p) throws Exception
       {
              username=u;
              password=p;
              return OpenConnection();
       }
       public void CloseConnection() throws Exception
       {
           if ( con != null )
                try { con.close(); } catch ( SQLException e ) { e.printStackTrace(); }
       }

       public DBConnector()
       {

       }
       public DBConnector(String _username,String _password)
       {
           setUsername(username);
           setPassword(password);
       }

1 个答案:

答案 0 :(得分:0)

ClassNotFoundException:oracle.aurora.jndi.ojds.OjdsInitialContextFactory

是你的问题,在类路径上找不到类,所以你需要安装它所带来的库。