Results 1 to 7 of 7
Thread: JNDI Oracle
- 12-16-2010, 03:17 PM #1
Member
- Join Date
- Dec 2010
- Location
- Dallas
- Posts
- 10
- Rep Power
- 0
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
- 12-16-2010, 03:46 PM #2
No, because the applet runs in a JVM in the web browser, away from the server right.As a second question can JNDI be used from a applet?
- 12-16-2010, 03:49 PM #3
Member
- Join Date
- Dec 2010
- Location
- Dallas
- Posts
- 10
- Rep Power
- 0
So if I change it be a servlet the configuation given would work. Exactly what is the java:/comp/env do?
Thanks
- 12-16-2010, 03:52 PM #4
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");
- 12-16-2010, 03:55 PM #5
Member
- Join Date
- Dec 2010
- Location
- Dallas
- Posts
- 10
- Rep Power
- 0
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
- 12-16-2010, 04:18 PM #6
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.
- 12-16-2010, 04:29 PM #7
Member
- Join Date
- Dec 2010
- Location
- Dallas
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
jndi what is used for?????????
By javastuden in forum Advanced JavaReplies: 1Last Post: 01-07-2010, 08:19 AM -
why oracle index can't work correctly in oracle?
By wwwlife in forum JDBCReplies: 0Last Post: 08-27-2008, 09:27 AM -
JNDI Related
By Ganeshag777 in forum Advanced JavaReplies: 0Last Post: 08-13-2008, 01:18 PM -
JNDI connection.
By ganesh in forum EclipseReplies: 0Last Post: 05-27-2008, 04:10 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks