Results 1 to 7 of 7

Thread: JNDI Oracle

  1. #1
    poajavaweb is offline Member
    Join Date
    Dec 2010
    Location
    Dallas
    Posts
    10
    Rep Power
    0

    Default JNDI Oracle

    Does some have the simplest configuration for a JNDI connection to oracle.

    I am using Tomcat 5.5. and I see to get "need to specify class anme in enviroment" error on the
    statment below

    Context envContext = (Context) initContext.lookup("java:/comp/env");


    Sample code:

    public class ConnectionPool implements Serializable {
    String message = "Not Connected";public void init() {
    Connection conn = null;
    ResultSet rst = null;
    Statement stmt = null;
    try {
    Context initContext = new InitialContext();
    Context envContext = (Context) initContext.lookup("java:/comp/env");
    OracleDataSource ds = (OracleDataSource) envContext.lookup("jdbc/db1");
    if (envContext == null) throw new Exception("Error: No Context");
    if (ds == null) throw new Exception("Error: No DataSource");
    if (ds != null) conn = ds.getConnection(); if (conn != null) {
    message = "Got Connection " + conn.toString() + ", ";
    stmt = conn.createStatement();
    rst = stmt.executeQuery("SELECT 'Success obtaining connection' FROM DUAL");
    }
    if (rst.next()) message = rst.getString(1);

    rst.close();
    rst = null;
    stmt.close();
    stmt = null;
    conn.close(); // Return to connection pool
    conn = null; // Make sure we don't close it twice
    .etc etc


    As a second question can JNDI be used from a applet?

    Thanks

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    As a second question can JNDI be used from a applet?
    No, because the applet runs in a JVM in the web browser, away from the server right.

  3. #3
    poajavaweb is offline Member
    Join Date
    Dec 2010
    Location
    Dallas
    Posts
    10
    Rep Power
    0

    Default

    So if I change it be a servlet the configuation given would work. Exactly what is the java:/comp/env do?

    Thanks

  4. #4
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    the java:/comp/env is a kind of a namespace or prefix that tomcat has invented to be the area in JNDI where data sources configured in the context.xml or server.xml should be found. Generally JNDI is a key-value kind of storage mechanism, one could bind something to any kind of path/name thing in theory. they just use this convention for their datasources.

    see also the example : The Apache Tomcat 5.5 Servlet/JSP Container - JNDI Datasource HOW-TO

    also, it might be helpful to just obtain a "DataSource" reference, instead of specific OracleDataSource

    Java Code:
    DataSource ds = (DataSource) envContext.lookup("jdbc/db1");

  5. #5
    poajavaweb is offline Member
    Join Date
    Dec 2010
    Location
    Dallas
    Posts
    10
    Rep Power
    0

    Default

    One last question.

    I could change my java program to be a web service / servlet and that could use the jndi above.

    or

    How do developer hide the oracle userid and password in a applet.

    Thanks

  6. #6
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    Yes, using a web service and the servlet with JNDI lookup is probably better.

    I guess in theory it might be possible to have the oracle JDBC jar file stuffed into the classpath of the applet, where the applet would use standard JDBC Connection loading (e.g. wouldn't use the JNDI fetching a datasource).

    This likely would work if the oracle database was on the same web server for which the HTML that served the applet from. I think applets are limited in network communications only to the server that it was served from, so this would not work in a general deployment of having a database on some other server.

    Also, it likely is not a good idea to have the oracle datbase network port open to the internet, as it would need to be for an applet to connect to it.

    So yea, the web service calls from an applet to a webapplication that uses JDBC connection from JNDI datasource is likely the best approach (where the transport could be secured using HTTPS and some kind of authentication mechanism you could create between the applet and web service, that might be different from the oracle database username and password.

  7. #7
    poajavaweb is offline Member
    Join Date
    Dec 2010
    Location
    Dallas
    Posts
    10
    Rep Power
    0

    Default

    thanks a bunch TravisHein.


    In this forum do we mark a correct or such..

    Have a Merry Christmas

Similar Threads

  1. jndi what is used for?????????
    By javastuden in forum Advanced Java
    Replies: 1
    Last Post: 01-07-2010, 09:19 AM
  2. Replies: 0
    Last Post: 08-27-2008, 10:27 AM
  3. JNDI Related
    By Ganeshag777 in forum Advanced Java
    Replies: 0
    Last Post: 08-13-2008, 02:18 PM
  4. JNDI connection.
    By ganesh in forum Eclipse
    Replies: 0
    Last Post: 05-27-2008, 05:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •