Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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("
 -------- 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>
    <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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
82 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need an initial context factory. For Tomcat it is org.apache.naming.java.javaURLContextFactory:

        System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                  "org.apache.naming.java.javaURLContextFactory");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...