String dbName = ic.lookup("DataSourceName");???????? <!-- The env-entry-name element contains the name of an enterprise bean's environment entry. The name is a JNDI name relative to the java:comp/env context. The name must be unique within an enterprise bean.Used in: env-entryExample:<env-entry-name>minAmount</env-entry-name> -->
weblogic 中你可以使用如下代码,这是weblogic的例子代码, private Context getInitialContext() throws NamingException { try { // Get an InitialContext Properties h = new Properties(); h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); h.put(Context.PROVIDER_URL, url); return new InitialContext(h); } catch (NamingException ne) { log("We were unable to get a connection to the WebLogic server at "+url); log("Please make sure that the server is running."); throw ne; } } Context ctx = getInitialContext(); try { Object home = (AccountHome) ctx.lookup("java:comp/env/DataSourceName"); return (AccountHome) PortableRemoteObject.narrow(home, AccountHome.class); } catch (NamingException ne) { log("The client was unable to lookup the EJBHome. Please make sure " + "that you have deployed the ejb with the JNDI name " + "containerManaged.AccountHome on the WebLogic server at "+url); throw ne; }
private void setHome(String ejbName, Class ejbHome) throws Exception
{ try
{
Context ctx = new InitialContext();
Object ref = ctx.lookup(ejbName);
this.home = (EJBHome) PortableRemoteObject.narrow(ref, ejbHome);
}
catch(Exception ee)
{
System.out.println("EJB "+ejbName+" setHome "+" error is "+ee.getMessage());
}
} this.setHome("SessionEJB", SessionHome.class);
SessionRemote remote = ((SessionHome) home).create();
<!--
The env-entry-name element contains the name of an enterprise bean's
environment entry. The name is a JNDI name relative to the
java:comp/env context. The name must be unique within an enterprise bean.Used in: env-entryExample:<env-entry-name>minAmount</env-entry-name>
-->
如果你的容器是J2EE RI的话,那么你试试这样
try
{
Context ctx=new InitialContext();
String str=(String)ctx.lookup("java:comp/env/DataSourceName");
}
catch(Exception e){//catch exception}以上代码是根据你的部署描述符写的。
我用的是weblogic6.1 weblogic中的连接池和Data Source 已经设置好了
to wcarry21(开开) :我看不懂你程序的意思,能详细一点吗?
to: xioyoo(丁鹏) :非常谢谢!
能给我讲讲"java:comp/env/DataSourcename"含义吗?
ggyy(发誓:再也不丢钱包了) :也非常谢谢你,我想知道为什么你的和xioyoo(丁鹏) 的有点区别,加jdbc是什么意思?下班时结分~~~
笔误!
主要是经常采用:jdbc/XXXXX来标记是jdbc连接的
所以习惯了。
不好意思
java:comp/env context
private Context getInitialContext() throws NamingException { try {
// Get an InitialContext
Properties h = new Properties();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, url);
return new InitialContext(h);
} catch (NamingException ne) {
log("We were unable to get a connection to the WebLogic server at "+url);
log("Please make sure that the server is running.");
throw ne;
}
} Context ctx = getInitialContext(); try {
Object home = (AccountHome) ctx.lookup("java:comp/env/DataSourceName");
return (AccountHome) PortableRemoteObject.narrow(home, AccountHome.class); } catch (NamingException ne) {
log("The client was unable to lookup the EJBHome. Please make sure " +
"that you have deployed the ejb with the JNDI name " +
"containerManaged.AccountHome on the WebLogic server at "+url); throw ne;
}
谢谢后来的zombieLi(zombieLi)
我才刚刚开始学java,你讲的有些东西我还不懂,如:Properties
还有我怎么找不到jndi.properties文件啊?
算了,以后再问,再次多谢各位。